From 5c26f153addbd296af913c02fcee5f3184caa874 Mon Sep 17 00:00:00 2001 From: rrozek Date: Wed, 21 Aug 2024 00:59:09 +0200 Subject: [PATCH] eth block height handling --- package.json | 2 +- src/components/node-api/chainstate.ts | 24 +++++++++++++++++------- 2 files changed, 18 insertions(+), 8 deletions(-) diff --git a/package.json b/package.json index 97356ae..8b36284 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "dashboard-creator-server", "private": false, - "version": "3.2.3", + "version": "3.2.4", "license": "MIT", "main": "src/server.ts", "engines": { diff --git a/src/components/node-api/chainstate.ts b/src/components/node-api/chainstate.ts index c55a9bc..4f7b45e 100644 --- a/src/components/node-api/chainstate.ts +++ b/src/components/node-api/chainstate.ts @@ -1,22 +1,32 @@ import { ApiPromise, WsProvider } from '@polkadot/api'; +import { ethers } from 'ethers'; const rpcEndpoints: { [key: string]: string } = { polkadot: 'wss://rpc.polkadot.io', kusama: 'wss://kusama-rpc.polkadot.io', 'aleph-zero': 'wss://rpc.azero.dev', moonbeam: 'wss://moonbeam.api.onfinality.io/public-ws', - 'arbitrum-one': 'wss://arbitrum-one.public.blastapi.io', + 'arbitrum-one': 'https://arbitrum.api.onfinality.io/public', }; -export async function getCurrentBlock(chain: string) { +export async function getCurrentBlock(chain: string): Promise { const endpoint = rpcEndpoints[chain]; if (!endpoint) { throw new Error(`RPC endpoint for chain ${chain} not found`); } - const wsProvider = new WsProvider(endpoint); - const api = await ApiPromise.create({ provider: wsProvider }); - - const currentBlock = await api.rpc.chain.getHeader(); - return currentBlock.number.toNumber(); + if (['polkadot', 'kusama', 'aleph-zero', 'moonbeam'].includes(chain)) { + // Substrate-based chains + const wsProvider = new WsProvider(endpoint); + const api = await ApiPromise.create({ provider: wsProvider }); + const currentBlock = await api.rpc.chain.getHeader(); + return currentBlock.number.toNumber(); + } else if (['arbitrum-one'].includes(chain)) { + // Ethereum-compatible chains (e.g., Arbitrum) + const provider = new ethers.JsonRpcProvider(endpoint); + const currentBlockNumber = await provider.getBlockNumber(); + return currentBlockNumber; + } else { + throw new Error(`Chain ${chain} is not supported`); + } }