Skip to content

Commit

Permalink
CU-86dtqwfxk - Implement new method to get all available RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
raulduartep committed Jun 6, 2024
1 parent a34663a commit ee954cb
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/blockchain-service",
"comment": " Add new method to get rpc list",
"type": "patch"
}
],
"packageName": "@cityofzion/blockchain-service"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/bs-ethereum",
"comment": " Add new method to get rpc list",
"type": "patch"
}
],
"packageName": "@cityofzion/bs-ethereum"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/bs-neo-legacy",
"comment": " Add new method to get rpc list",
"type": "patch"
}
],
"packageName": "@cityofzion/bs-neo-legacy"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"changes": [
{
"packageName": "@cityofzion/bs-neo3",
"comment": " Add new method to get rpc list",
"type": "patch"
}
],
"packageName": "@cityofzion/bs-neo3"
}
6 changes: 6 additions & 0 deletions packages/blockchain-service/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,11 @@ export type BalanceResponse = {
amount: string
token: Token
}
export type RpcResponse = {
latency: number
url: string
height: number
}
export interface BlockchainDataService {
maxTimeToConfirmTransactionInMs: number
getTransaction(txid: string): Promise<TransactionResponse>
Expand All @@ -155,6 +160,7 @@ export interface BlockchainDataService {
getTokenInfo(tokenHash: string): Promise<Token>
getBalance(address: string): Promise<BalanceResponse[]>
getBlockHeight(): Promise<number>
getRpcList(): Promise<RpcResponse[]>
}
export interface BDSClaimable {
getUnclaimed(address: string): Promise<string>
Expand Down
37 changes: 36 additions & 1 deletion packages/bs-ethereum/src/RpcBDSEthereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import {
BlockchainDataService,
ContractResponse,
Network,
RpcResponse,
Token,
TransactionResponse,
TransactionsByAddressParams,
TransactionsByAddressResponse,
} from '@cityofzion/blockchain-service'
import { ethers } from 'ethers'
import { TOKENS } from './constants'
import { RPC_LIST_BY_NETWORK_TYPE, TOKENS } from './constants'

export class RpcBDSEthereum implements BlockchainDataService {
readonly #network: Network
Expand Down Expand Up @@ -85,4 +86,38 @@ export class RpcBDSEthereum implements BlockchainDataService {
const provider = new ethers.providers.JsonRpcProvider(this.#network.url)
return await provider.getBlockNumber()
}

async getRpcList(): Promise<RpcResponse[]> {
const list: RpcResponse[] = []

const promises = RPC_LIST_BY_NETWORK_TYPE[this.#network.type].map(url => {
// eslint-disable-next-line no-async-promise-executor
return new Promise<void>(async resolve => {
const timeout = setTimeout(() => {
resolve()
}, 5000)

try {
const provider = new ethers.providers.JsonRpcProvider(url)

const timeStart = Date.now()
const height = await provider.getBlockNumber()
const latency = Date.now() - timeStart

list.push({
url,
height,
latency,
})
} finally {
resolve()
clearTimeout(timeout)
}
})
})

await Promise.allSettled(promises)

return list
}
}
13 changes: 13 additions & 0 deletions packages/bs-ethereum/src/__tests__/BitqueryBDSEthereum.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,17 @@ describe('BitqueryBDSEthereum', () => {
)
})
})

it('Should be able to get a list of rpc - %s', async () => {
const list = await bitqueryBDSEthereum.getRpcList()
console.log(list)
expect(list.length).toBeGreaterThan(0)
list.forEach(rpc => {
expect(rpc).toEqual({
height: expect.any(Number),
latency: expect.any(Number),
url: expect.any(String),
})
})
}, 50000)
})
32 changes: 26 additions & 6 deletions packages/bs-ethereum/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,6 @@ export const TOKENS: Record<NetworkType, Token[]> = {

export const NATIVE_ASSETS = commom

export const DEFAULT_URL_BY_NETWORK_TYPE: Record<NetworkType, string> = {
mainnet: 'https://ethereum-mainnet-rpc.allthatnode.com',
testnet: 'https://ethereum-sepolia-rpc.publicnode.com',
custom: 'http://127.0.0.1:8545',
}

export const BITQUERY_MIRROR_URL = 'https://i4l7kcg43c.execute-api.us-east-1.amazonaws.com/production/'
export const BITQUERY_MIRROR_NETWORK_BY_NETWORK_TYPE: Record<Exclude<NetworkType, 'custom'>, BitqueryNetwork> = {
mainnet: 'ethereum',
Expand All @@ -34,3 +28,29 @@ export const GHOSTMARKET_CHAIN_BY_NETWORK_TYPE: Partial<Record<NetworkType, stri
}

export const DERIVATION_PATH = "m/44'/60'/0'/0/?"

export const RPC_LIST_BY_NETWORK_TYPE: Record<NetworkType, string[]> = {
mainnet: [
'https://ethereum-mainnet-rpc.allthatnode.com',
'https://eth.llamarpc.com',
'https://ethereum-rpc.publicnode.com',
'https://endpoints.omniatech.io/v1/eth/mainnet/public',
'https://rpc.flashbots.net',
'https://rpc.mevblocker.io',
],
testnet: [
'https://ethereum-sepolia-rpc.publicnode.com',
'https://endpoints.omniatech.io/v1/eth/sepolia/public',
'https://eth-sepolia.public.blastapi.io',
'https://eth-sepolia-public.unifra.io',
'https://1rpc.io/sepolia',
'https://eth-sepolia.api.onfinality.io/public',
],
custom: ['http://127.0.0.1:8545'],
}

export const DEFAULT_URL_BY_NETWORK_TYPE: Record<NetworkType, string> = {
mainnet: RPC_LIST_BY_NETWORK_TYPE.mainnet[0],
testnet: RPC_LIST_BY_NETWORK_TYPE.testnet[0],
custom: RPC_LIST_BY_NETWORK_TYPE.custom[0],
}
35 changes: 34 additions & 1 deletion packages/bs-neo-legacy/src/DoraBDSNeoLegacy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ import {
TransactionTransferAsset,
Token,
Network,
RpcResponse,
NetworkType,
} from '@cityofzion/blockchain-service'
import { api } from '@cityofzion/dora-ts'
import { TOKENS } from './constants'
import { RPC_LIST_BY_NETWORK_TYPE, TOKENS } from './constants'
import { rpc } from '@cityofzion/neon-js'

export class DoraBDSNeoLegacy implements BlockchainDataService, BDSClaimable {
Expand Down Expand Up @@ -173,4 +175,35 @@ export class DoraBDSNeoLegacy implements BlockchainDataService, BDSClaimable {
const rpcClient = new rpc.RPCClient(this.#network.url)
return await rpcClient.getBlockCount()
}

async getRpcList(): Promise<RpcResponse[]> {
const list: RpcResponse[] = []
const networkType = this.#network.type as Exclude<NetworkType, 'custom'>

const promises = RPC_LIST_BY_NETWORK_TYPE[networkType].map(url => {
// eslint-disable-next-line no-async-promise-executor
return new Promise<void>(async resolve => {
const timeout = setTimeout(() => {
resolve()
}, 5000)

try {
const rpcClient = new rpc.RPCClient(url)

const timeStart = Date.now()
const height = await rpcClient.getBlockCount()
const latency = Date.now() - timeStart

list.push({ url, latency, height })
} finally {
resolve()
clearTimeout(timeout)
}
})
})

await Promise.allSettled(promises)

return list
}
}
12 changes: 12 additions & 0 deletions packages/bs-neo-legacy/src/__tests__/BDSNeoLegacy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,16 @@ describe('BDSNeoLegacy', () => {
expect(unclaimed).toEqual(expect.any(String))
}
)

it.each([doraBDSNeoLegacy])('Should be able to get a list of rpc - %s', async (bdsNeo3: BlockchainDataService) => {
const list = await bdsNeo3.getRpcList()
expect(list.length).toBeGreaterThan(0)
list.forEach(rpc => {
expect(rpc).toEqual({
height: expect.any(Number),
latency: expect.any(Number),
url: expect.any(String),
})
})
})
})
28 changes: 24 additions & 4 deletions packages/bs-neo-legacy/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,29 @@ export const LEGACY_NETWORK_BY_NETWORK_TYPE: Record<Exclude<NetworkType, 'custom

export const NATIVE_ASSETS = commom

export const DEFAULT_URL_BY_NETWORK_TYPE: Record<Exclude<NetworkType, 'custom'>, string> = {
mainnet: 'http://seed9.ngd.network:10332',
testnet: 'http://seed5.ngd.network:20332',
export const DERIVATION_PATH = "m/44'/888'/0'/0/?"

export const RPC_LIST_BY_NETWORK_TYPE: Record<Exclude<NetworkType, 'custom'>, string[]> = {
mainnet: [
'http://seed9.ngd.network:10332',
'https://mainnet1.neo2.coz.io:443',
'https://mainnet2.neo2.coz.io:443',
'https://mainnet3.neo2.coz.io:443',
'http://seed1.ngd.network:10332',
'http://seed2.ngd.network:10332',
'http://seed3.ngd.network:10332',
'http://seed4.ngd.network:10332',
'http://seed5.ngd.network:10332',
],
testnet: [
'http://seed5.ngd.network:20332',
'http://seed1.ngd.network:20332',
'http://seed2.ngd.network:20332',
'https://testnet1.neo2.coz.io:443',
],
}

export const DERIVATION_PATH = "m/44'/888'/0'/0/?"
export const DEFAULT_URL_BY_NETWORK_TYPE: Record<Exclude<NetworkType, 'custom'>, string> = {
mainnet: RPC_LIST_BY_NETWORK_TYPE.mainnet[0],
testnet: RPC_LIST_BY_NETWORK_TYPE.testnet[0],
}
33 changes: 32 additions & 1 deletion packages/bs-neo3/src/RpcBDSNeo3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@ import {
ContractParameter,
ContractResponse,
Network,
RpcResponse,
Token,
TransactionResponse,
TransactionsByAddressParams,
TransactionsByAddressResponse,
} from '@cityofzion/blockchain-service'
import { rpc, u } from '@cityofzion/neon-core'
import { NeonInvoker, TypeChecker } from '@cityofzion/neon-dappkit'
import { TOKENS } from './constants'
import { RPC_LIST_BY_NETWORK_TYPE, TOKENS } from './constants'

export class RPCBDSNeo3 implements BlockchainDataService, BDSClaimable {
readonly _tokenCache: Map<string, Token> = new Map()
Expand Down Expand Up @@ -158,4 +159,34 @@ export class RPCBDSNeo3 implements BlockchainDataService, BDSClaimable {
const response = await rpcClient.getUnclaimedGas(address)
return u.BigInteger.fromNumber(response).toDecimal(this._claimToken.decimals)
}

async getRpcList(): Promise<RpcResponse[]> {
const list: RpcResponse[] = []

const promises = RPC_LIST_BY_NETWORK_TYPE[this._network.type].map(url => {
// eslint-disable-next-line no-async-promise-executor
return new Promise<void>(async resolve => {
const timeout = setTimeout(() => {
resolve()
}, 5000)

try {
const rpcClient = new rpc.RPCClient(url)

const timeStart = Date.now()
const height = await rpcClient.getBlockCount()
const latency = Date.now() - timeStart

list.push({ url, latency, height })
} finally {
resolve()
clearTimeout(timeout)
}
})
})

await Promise.allSettled(promises)

return list
}
}
14 changes: 13 additions & 1 deletion packages/bs-neo3/src/__tests__/BDSNeo3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('BDSNeo3', () => {
}
)

it.only.each([doraBDSNeo3])(
it.each([doraBDSNeo3])(
'Should be able to get transactions of address - %s',
async (bdsNeo3: BlockchainDataService) => {
const address = 'NPB3Cze4wki9J36nnrT45qmi6P52Bhfqph'
Expand Down Expand Up @@ -121,4 +121,16 @@ describe('BDSNeo3', () => {
expect(unclaimed).toEqual(expect.any(String))
}
)

it.each([rpcBDSNeo3])('Should be able to get a list of rpc - %s', async (bdsNeo3: BlockchainDataService) => {
const list = await bdsNeo3.getRpcList()
expect(list.length).toBeGreaterThan(0)
list.forEach(rpc => {
expect(rpc).toEqual({
height: expect.any(Number),
latency: expect.any(Number),
url: expect.any(String),
})
})
})
})
Loading

0 comments on commit ee954cb

Please sign in to comment.