Skip to content

Commit

Permalink
JS-API Layer fixes and improvements (#1171)
Browse files Browse the repository at this point in the history
* fix publish

* change metadata

* fix beacon client issues

* fix packages

* poll events

* sno-908: polling api

* use transfer instead of teleport
  • Loading branch information
alistair-singh authored Apr 24, 2024
1 parent 7f41605 commit 3f90b16
Show file tree
Hide file tree
Showing 19 changed files with 1,279 additions and 751 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/npm-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ jobs:
- name: Publish Contract Types
working-directory: web/packages/contract-types
run: |
pnpm publish
pnpm publish --no-git-checks --access public
- name: Publish API
working-directory: web/packages/api
run: |
pnpm publish
pnpm publish --no-git-checks --access public
24 changes: 12 additions & 12 deletions web/packages/api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@
"lint": "eslint ."
},
"devDependencies": {
"@types/node": "^18.13.0",
"@typescript-eslint/eslint-plugin": "^5.42.0",
"@typescript-eslint/parser": "^5.42.0",
"eslint": "^8.26.0",
"eslint-config-prettier": "^8.5.0",
"prettier": "^2.7.1",
"ts-node": "^10.9.1",
"@types/node": "^18.19.31",
"@typescript-eslint/eslint-plugin": "^5.62.0",
"@typescript-eslint/parser": "^5.62.0",
"eslint": "^8.57.0",
"eslint-config-prettier": "^8.10.0",
"prettier": "^2.8.8",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.1.6"
"typescript": "^5.4.5"
},
"dependencies": {
"ethers": "^6.9.0",
"@polkadot/api": "^10.12.1",
"@polkadot/types": "^10.12.1",
"@polkadot/api": "^10.12.6",
"@polkadot/keyring": "^12.6.2",
"@polkadot/types": "^10.12.6",
"@polkadot/util": "^12.6.2",
"@polkadot/util-crypto": "^12.6.2",
"@polkadot/keyring": "^12.6.2",
"@snowbridge/contract-types": "workspace:*",
"@typechain/ethers-v6": "^0.5.1",
"ethers": "^6.11.1",
"rxjs": "^7.8.1"
}
}
16 changes: 11 additions & 5 deletions web/packages/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
// import '@polkadot/api-augment/polkadot'
import { ApiPromise, WsProvider } from '@polkadot/api'
import { ethers } from 'ethers'
import { AbstractProvider, ethers } from 'ethers'
import { BeefyClient, BeefyClient__factory, IGateway, IGateway__factory } from '@snowbridge/contract-types'

interface Config {
ethereum: {
url: string
execution_url: string
beacon_url: string
}
polkadot: {
url: {
Expand Down Expand Up @@ -39,10 +40,10 @@ export class Context {
}

class EthereumContext {
api: ethers.WebSocketProvider
api: ethers.AbstractProvider
contracts: AppContracts

constructor(api: ethers.WebSocketProvider, contracts: AppContracts) {
constructor(api: ethers.AbstractProvider, contracts: AppContracts) {
this.api = api
this.contracts = contracts
}
Expand All @@ -67,7 +68,12 @@ class PolkadotContext {

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export const contextFactory = async (config: Config): Promise<Context> => {
const ethApi = new ethers.WebSocketProvider(config.ethereum.url)
let ethApi: AbstractProvider;
if(config.ethereum.execution_url.startsWith("http")) {
ethApi = new ethers.JsonRpcProvider(config.ethereum.execution_url)
} else {
ethApi = new ethers.WebSocketProvider(config.ethereum.execution_url)
}
const relaychainApi = await ApiPromise.create({
provider: new WsProvider(config.polkadot.url.relaychain),
})
Expand Down
28 changes: 28 additions & 0 deletions web/packages/api/src/query.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
import { ApiPromise } from '@polkadot/api'
import { BlockHash } from '@polkadot/types/interfaces'
import { Codec } from '@polkadot/types/types'
import { filter, firstValueFrom, take } from 'rxjs'

export const scanSubstrateEvents = async (
parachain: ApiPromise,
start: bigint,
scanBlocks: bigint,
filter: (blockNumber: bigint, blockHash: BlockHash, event: Codec) => Promise<boolean>): Promise<{
found: boolean
lastScannedBlock: bigint
events?: Codec
}> => {

const finalized = (await parachain.rpc.chain.getHeader(await parachain.rpc.chain.getFinalizedHead())).number.toBigInt()
const stopScan = start + scanBlocks
const end = finalized < stopScan ? finalized : stopScan

for (let blockNumber = start; blockNumber <= end; ++blockNumber) {
const blockHash = await parachain.rpc.chain.getBlockHash(blockNumber)
const events = await (await parachain.at(blockHash)).query.system.events()
for (const event of events as any) {
if (await filter(blockNumber, blockHash, event)) {
return { found: true, lastScannedBlock: blockNumber, events: events }
}
}
}

return { found: false, lastScannedBlock: end }
}

export const waitForMessageQueuePallet = async (
parachain: ApiPromise,
messageId: string | undefined,
Expand Down
13 changes: 10 additions & 3 deletions web/packages/api/src/status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IERC20__factory } from '@snowbridge/contract-types'
import { Context } from './index'
import { fetchBeaconSlot } from './utils'

export type OperatingMode = 'Normal' | 'Halted'
export type BridgeStatusInfo = {
Expand Down Expand Up @@ -45,13 +46,19 @@ export const bridgeStatusInfo = async (context: Context, options = {
const latestBeefyBlock = Number(await context.ethereum.contracts.beefyClient.latestBeefyBlock())
const latestPolkadotBlock = (await context.polkadot.api.relaychain.query.system.number()).toPrimitive() as number

const latestBeaconState = (await context.polkadot.api.bridgeHub.query.ethereumBeaconClient.latestExecutionState()).toPrimitive() as { blockNumber: number }
const latestEthereumBlock = await context.ethereum.api.getBlockNumber()
const latestBeaconBlockRoot = (await context.polkadot.api.bridgeHub.query.ethereumBeaconClient.latestFinalizedBlockRoot()).toHex()
let latestBeaconSlot = await fetchBeaconSlot(context.config.ethereum.beacon_url, latestBeaconBlockRoot)
let latestBeaconExecutionBlock = latestBeaconSlot.data.message.body.execution_payload?.block_number
while (latestBeaconExecutionBlock === undefined) {
latestBeaconSlot = await fetchBeaconSlot(context.config.ethereum.beacon_url, latestBeaconSlot.data.message.slot - 1)
latestBeaconExecutionBlock = latestBeaconSlot.data.message.body.execution_payload?.block_number
}

const beefyBlockLatency = latestPolkadotBlock - latestBeefyBlock
const beefyLatencySeconds = beefyBlockLatency * options.polkadotBlockTimeInSeconds

const beaconBlockLatency = latestEthereumBlock - latestBeaconState.blockNumber
const beaconBlockLatency = latestEthereumBlock - Number(latestBeaconExecutionBlock)
const beaconLatencySeconds = beaconBlockLatency * options.ethereumBlockTimeInSeconds

const ethereumOperatingMode = await context.ethereum.contracts.gateway.operatingMode()
Expand All @@ -75,7 +82,7 @@ export const bridgeStatusInfo = async (context: Context, options = {
inbound: inboundOperatingMode as OperatingMode,
outbound: ethereumOperatingMode === 0n ? 'Normal' : 'Halted' as OperatingMode,
},
latestEthereumBlockOnPolkadot: latestBeaconState.blockNumber,
latestEthereumBlockOnPolkadot: Number(latestBeaconExecutionBlock),
latestEthereumBlock: latestEthereumBlock,
blockLatency: beaconBlockLatency,
latencySeconds: beaconLatencySeconds,
Expand Down
Loading

0 comments on commit 3f90b16

Please sign in to comment.