Skip to content

Commit

Permalink
Merge pull request #35 from tokenguardio/dapp-analytics-dev
Browse files Browse the repository at this point in the history
Release 3.2.4
  • Loading branch information
rrozek authored Aug 20, 2024
2 parents ad8d44d + 5c26f15 commit 86347b1
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
24 changes: 17 additions & 7 deletions src/components/node-api/chainstate.ts
Original file line number Diff line number Diff line change
@@ -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<number> {
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`);
}
}

0 comments on commit 86347b1

Please sign in to comment.