Skip to content

Commit

Permalink
Api version 0.1.5 (#1201)
Browse files Browse the repository at this point in the history
* remove muse

* run prettier

* update polkadot-api

* wait for finalized blocks

* fix out of order messages

* use number instead of bigint for block numbers

* update package versions
  • Loading branch information
alistair-singh authored May 20, 2024
1 parent 04ebfb8 commit ed0a361
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 157 deletions.
8 changes: 4 additions & 4 deletions web/packages/api/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snowbridge/api",
"version": "0.1.4",
"version": "0.1.5",
"description": "Snowbridge API client",
"license": "Apache-2.0",
"repository": {
Expand All @@ -13,7 +13,7 @@
"scripts": {
"build": "tsc",
"lint": "eslint .",
"format": "prettier . --write"
"format": "prettier src --write"
},
"devDependencies": {
"@types/node": "^18.19.31",
Expand All @@ -27,9 +27,9 @@
"typescript": "^5.4.5"
},
"dependencies": {
"@polkadot/api": "^11.0.2",
"@polkadot/api": "^11.0.3",
"@polkadot/keyring": "^12.6.2",
"@polkadot/types": "^11.0.2",
"@polkadot/types": "^11.0.3",
"@polkadot/util": "^12.6.2",
"@polkadot/util-crypto": "^12.6.2",
"@snowbridge/contract-types": "workspace:*",
Expand Down
11 changes: 4 additions & 7 deletions web/packages/api/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,7 @@ export const SNOWBRIDGE_ENV: { [id: string]: SnowbridgeEnvironment } = {
erc20tokensReceivable: {
WETH: "0xfff9976782d46cc05630d1f6ebab18b2324d6b14",
vETH: "0xc3d088842dcf02c13699f936bb83dfbbc6f721ab",
MUSE1: "0xb34a6924a02100ba6ef12af1c798285e8f7a16ee",
MUSE2: "0xc9f05326311bc2a55426761bec20057685fb80f7",
MUSE: "0xb34a6924a02100ba6ef12af1c798285e8f7a16ee",
},
},
{
Expand All @@ -170,8 +169,7 @@ export const SNOWBRIDGE_ENV: { [id: string]: SnowbridgeEnvironment } = {
erc20tokensReceivable: {
WETH: "0xfff9976782d46cc05630d1f6ebab18b2324d6b14",
vETH: "0xc3d088842dcf02c13699f936bb83dfbbc6f721ab",
MUSE1: "0xb34a6924a02100ba6ef12af1c798285e8f7a16ee",
MUSE2: "0xc9f05326311bc2a55426761bec20057685fb80f7",
MUSE: "0xb34a6924a02100ba6ef12af1c798285e8f7a16ee",
},
},
{
Expand All @@ -181,13 +179,12 @@ export const SNOWBRIDGE_ENV: { [id: string]: SnowbridgeEnvironment } = {
destinationIds: [],
paraInfo: {
paraId: 3369,
destinationFeeDOT: 4_000_000_000_000n,
destinationFeeDOT: 200_000_000_000n,
has20ByteAccounts: true,
decimals: 12,
},
erc20tokensReceivable: {
MUSE1: "0xb34a6924a02100ba6ef12af1c798285e8f7a16ee",
MUSE2: "0xc9f05326311bc2a55426761bec20057685fb80f7",
MUSE: "0xb34a6924a02100ba6ef12af1c798285e8f7a16ee",
},
},
],
Expand Down
22 changes: 14 additions & 8 deletions web/packages/api/src/query.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
import { ApiPromise } from "@polkadot/api"
import { BlockHash } from "@polkadot/types/interfaces"
import { Codec } from "@polkadot/types/types"
import { filter, firstValueFrom, take } from "rxjs"
import { concatMap, filter, firstValueFrom, take } from "rxjs"

export const scanSubstrateEvents = async (
parachain: ApiPromise,
start: bigint,
scanBlocks: bigint,
filter: (blockNumber: bigint, blockHash: BlockHash, event: Codec) => Promise<boolean>
start: number,
scanBlocks: number,
filter: (blockNumber: number, blockHash: BlockHash, event: Codec) => Promise<boolean>
): Promise<{
found: boolean
lastScannedBlock: bigint
lastScannedBlock: number
events?: Codec
}> => {
const finalized = (
await parachain.rpc.chain.getHeader(await parachain.rpc.chain.getFinalizedHead())
).number.toBigInt()
).number.toNumber()
const stopScan = start + scanBlocks
const end = finalized < stopScan ? finalized : stopScan

Expand All @@ -38,14 +38,20 @@ export const waitForMessageQueuePallet = async (
siblingParachain: number,
eventFilter: (event: Codec) => boolean,
options = {
scanBlocks: 10,
scanBlocks: 40,
}
): Promise<{ foundEvent?: Codec; allEvents: Codec; extrinsicSuccess: boolean }> => {
let extrinsicSuccess = false
let returnEvent = undefined

parachain.rpc.chain.subscribeFinalizedHeads
let receivedEvents = await firstValueFrom(
parachain.rx.query.system.events().pipe(
parachain.rx.rpc.chain.subscribeFinalizedHeads().pipe(
take(options.scanBlocks),
concatMap(async (header) => {
const api1 = await parachain.at(header.hash)
return await api1.query.system.events()
}),
filter((events) => {
let foundMessageQueue = false
let foundEvent = false
Expand Down
38 changes: 17 additions & 21 deletions web/packages/api/src/toEthereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,9 +317,9 @@ export type SendResult = {
messageDispatchSuccess?: boolean
}
polling?: {
bridgeHubMessageQueueProcessed: bigint
ethereumBeefyClient: bigint
ethereumMessageDispatched: bigint
bridgeHubMessageQueueProcessed: number
ethereumBeefyClient: number
ethereumMessageDispatched: number
}
}
failure?: {
Expand Down Expand Up @@ -619,7 +619,7 @@ export const trackSendProgressPolling = async (
result: SendResult,
options = {
beaconUpdateTimeout: 10,
scanBlocks: 600n,
scanBlocks: 600,
}
): Promise<{ status: "success" | "pending"; result: SendResult }> => {
const {
Expand All @@ -642,15 +642,11 @@ export const trackSendProgressPolling = async (
bridgeHubMessageQueueProcessed:
(
await bridgeHub.rpc.chain.getHeader(success.bridgeHub.submittedAtHash)
).number.toBigInt() + 1n,
).number.toNumber() + 1,
ethereumBeefyClient:
BigInt(
(await ethereum.api.getBlock(success.ethereum.submittedAtHash))?.number ?? 0n
) + 1n,
(await ethereum.api.getBlock(success.ethereum.submittedAtHash))?.number ?? 0 + 1,
ethereumMessageDispatched:
BigInt(
(await ethereum.api.getBlock(success.ethereum.submittedAtHash))?.number ?? 0n
) + 1n,
(await ethereum.api.getBlock(success.ethereum.submittedAtHash))?.number ?? 0 + 1,
}
}

Expand All @@ -675,7 +671,7 @@ export const trackSendProgressPolling = async (
return false
}
)
success.polling.bridgeHubMessageQueueProcessed = lastScannedBlock + 1n
success.polling.bridgeHubMessageQueueProcessed = lastScannedBlock + 1
if (!found) {
return { status: "pending", result }
}
Expand All @@ -691,20 +687,20 @@ export const trackSendProgressPolling = async (
) {
// Estimate the relaychain block
const blockGap =
(success.polling?.bridgeHubMessageQueueProcessed ?? 0n) -
(success.polling?.bridgeHubMessageQueueProcessed ?? 0) -
((
await bridgeHub.rpc.chain.getHeader(success.bridgeHub.submittedAtHash)
).number.toBigInt() +
1n)
).number.toNumber() +
1)
const relaychainSubmittedBlock =
(
await relaychain.rpc.chain.getHeader(success.relayChain.submittedAtHash)
).number.toBigInt() + blockGap
).number.toNumber() + blockGap
console.log("Waiting for message to be included by BEEFY light client.")
const NewMMRRootEvent = beefyClient.getEvent("NewMMRRoot")

const from = success.polling.ethereumBeefyClient
let to = BigInt((await ethereum.api.getBlockNumber()) ?? 0)
let to = (await ethereum.api.getBlockNumber()) ?? 0
if (from - to > options.scanBlocks) {
to = from + options.scanBlocks
}
Expand All @@ -718,7 +714,7 @@ export const trackSendProgressPolling = async (
Number(to.toString())
)
for (const { blockHash, blockNumber, args } of events) {
const relayChainBlock = args.blockNumber
const relayChainBlock = Number(args.blockNumber.toString())
if (relayChainBlock >= relaychainSubmittedBlock) {
success.ethereum.beefyBlockNumber = blockNumber
success.ethereum.beefyBlockHash = blockHash
Expand All @@ -732,7 +728,7 @@ export const trackSendProgressPolling = async (
)
}
}
success.polling.ethereumBeefyClient = to + 1n
success.polling.ethereumBeefyClient = to + 1
if (success.ethereum.beefyBlockNumber === undefined) {
return { status: "pending", result }
}
Expand All @@ -746,7 +742,7 @@ export const trackSendProgressPolling = async (
const InboundMessageDispatched = gateway.getEvent("InboundMessageDispatched")

const from = success.polling.ethereumMessageDispatched
let to = BigInt((await ethereum.api.getBlockNumber()) ?? 0)
let to = (await ethereum.api.getBlockNumber()) ?? 0
if (from - to > options.scanBlocks) {
to = from + options.scanBlocks
}
Expand All @@ -773,7 +769,7 @@ export const trackSendProgressPolling = async (
break
}
}
success.polling.ethereumMessageDispatched = to + 1n
success.polling.ethereumMessageDispatched = to + 1
if (success.ethereum.transferBlockNumber === undefined) {
return { status: "pending", result }
}
Expand Down
28 changes: 14 additions & 14 deletions web/packages/api/src/toPolkadot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,10 @@ export type SendResult = {
messageId: string
plan: SendValidationResult
polling?: {
bridgeHubBeaconBlock: bigint
bridgeHubMessageReceived: bigint
assetHubMessageProcessed: bigint
destinationMessageProcessed: bigint | undefined
bridgeHubBeaconBlock: number
bridgeHubMessageReceived: number
assetHubMessageProcessed: number
destinationMessageProcessed: number | undefined
}
}
failure?: {
Expand Down Expand Up @@ -478,7 +478,7 @@ export const trackSendProgressPolling = async (
result: SendResult,
options = {
beaconUpdateTimeout: 10,
scanBlocks: 600n,
scanBlocks: 600,
}
): Promise<{ status: "success" | "pending"; result: SendResult }> => {
const {
Expand All @@ -493,7 +493,7 @@ export const trackSendProgressPolling = async (
}

if (success.polling === undefined) {
let destinationMessageProcessed: bigint | undefined = undefined
let destinationMessageProcessed: number | undefined = undefined
if (
success.destinationParachain !== undefined &&
success.plan.success.destinationParaId in parachains
Expand All @@ -503,21 +503,21 @@ export const trackSendProgressPolling = async (
await parachains[success.plan.success.destinationParaId].rpc.chain.getHeader(
success.destinationParachain.submittedAtHash
)
).number.toBigInt() + 1n
).number.toNumber() + 1
}
success.polling = {
bridgeHubBeaconBlock:
(
await bridgeHub.rpc.chain.getHeader(success.bridgeHub.submittedAtHash)
).number.toBigInt() + 1n,
).number.toNumber() + 1,
bridgeHubMessageReceived:
(
await bridgeHub.rpc.chain.getHeader(success.bridgeHub.submittedAtHash)
).number.toBigInt() + 1n,
).number.toNumber() + 1,
assetHubMessageProcessed:
(
await assetHub.rpc.chain.getHeader(success.assetHub.submittedAtHash)
).number.toBigInt() + 1n,
).number.toNumber() + 1,
destinationMessageProcessed: destinationMessageProcessed,
}
}
Expand Down Expand Up @@ -565,7 +565,7 @@ export const trackSendProgressPolling = async (
return false
}
)
success.polling.bridgeHubBeaconBlock = lastScannedBlock + 1n
success.polling.bridgeHubBeaconBlock = lastScannedBlock + 1
if (!found) {
return { status: "pending", result }
}
Expand Down Expand Up @@ -616,7 +616,7 @@ export const trackSendProgressPolling = async (
return false
}
)
success.polling.bridgeHubMessageReceived = lastScannedBlock + 1n
success.polling.bridgeHubMessageReceived = lastScannedBlock + 1
if (!found) {
return { status: "pending", result }
}
Expand Down Expand Up @@ -667,7 +667,7 @@ export const trackSendProgressPolling = async (
return false
}
)
success.polling.assetHubMessageProcessed = lastScannedBlock + 1n
success.polling.assetHubMessageProcessed = lastScannedBlock + 1
if (!found) {
return { status: "pending", result }
}
Expand Down Expand Up @@ -710,7 +710,7 @@ export const trackSendProgressPolling = async (
return false
}
)
success.polling.destinationMessageProcessed = lastScannedBlock + 1n
success.polling.destinationMessageProcessed = lastScannedBlock + 1
if (!found) {
return { status: "pending", result }
}
Expand Down
2 changes: 1 addition & 1 deletion web/packages/contract-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snowbridge/contract-types",
"version": "0.1.4",
"version": "0.1.5",
"description": "Snowbridge contract type bindings",
"license": "Apache-2.0",
"repository": {
Expand Down
22 changes: 11 additions & 11 deletions web/packages/operations/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"start": "npx ts-node src/start.ts",
"cron": "npx ts-node src/cron.ts",
"server": "npx ts-node src/server.ts",
"format": "prettier . --write"
"format": "prettier src --write"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.62.0",
Expand All @@ -27,13 +27,15 @@
"typescript": "^5.4.5"
},
"dependencies": {
"@aws-sdk/client-cloudwatch": "^3.574.0",
"@ethersproject/abi": "^5.7.0",
"@ethersproject/bytes": "^5.7.0",
"@ethersproject/providers": "^5.7.2",
"@ethersproject/units": "^5.7.0",
"@polkadot/api": "^11.0.2",
"@fastify/rate-limit": "^9.1.0",
"@polkadot/api": "^11.0.3",
"@polkadot/keyring": "^12.6.2",
"@polkadot/types": "^11.0.2",
"@polkadot/types": "^11.0.3",
"@polkadot/types-codec": "^11.0.2",
"@polkadot/util": "^12.6.2",
"@polkadot/util-crypto": "^12.6.2",
Expand All @@ -42,22 +44,20 @@
"@types/keccak": "^3.0.4",
"@types/lodash": "^4.17.0",
"@types/node": "^18.19.31",
"@types/node-cron": "^3.0.11",
"@types/secp256k1": "^4.0.6",
"@types/seedrandom": "^3.0.8",
"axios": "^1.6.8",
"bitfield": "^4.2.0",
"dotenv": "^16.4.5",
"ethers": "^6.12.1",
"fastify": "^4.27.0",
"keccak": "^3.0.4",
"lodash": "^4.17.21",
"merkletreejs": "^0.2.32",
"node-cron": "^3.0.3",
"rlp": "^2.2.7",
"secp256k1": "^4.0.3",
"seedrandom": "^3.0.5",
"axios": "^1.6.8",
"fastify": "^4.27.0",
"@fastify/rate-limit": "^9.1.0",
"node-cron": "^3.0.3",
"@types/node-cron": "^3.0.11",
"@aws-sdk/client-cloudwatch": "^3.574.0",
"dotenv": "^16.4.5"
"seedrandom": "^3.0.5"
}
}
2 changes: 1 addition & 1 deletion web/packages/test-helpers/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"generateContracts": "npx ts-node src/generateContractInfo.ts",
"generateBeefyValidatorSet": "npx ts-node src/generateBeefyTestFixture.ts GenerateInitialSet",
"generateBeefyFinalProof": "npx ts-node src/generateBeefyTestFixture.ts GenerateProofs",
"format": "prettier . --write"
"format": "prettier src --write"
},
"devDependencies": {
"@typescript-eslint/eslint-plugin": "^5.42.0",
Expand Down
Loading

0 comments on commit ed0a361

Please sign in to comment.