-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from tokenguardio/dapp-analytics-dev
Release 3.2.4
- Loading branch information
Showing
2 changed files
with
18 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`); | ||
} | ||
} |