Skip to content

Commit

Permalink
Estimate delivery time (#1256)
Browse files Browse the repository at this point in the history
* Estimate delivery time

* Fix comment

* Some polish

* Improve error message

* Decimal ceil rounded

* Update domain name

* Remove axios replace with node fetch

* Fix build contract-types
  • Loading branch information
yrong authored Jul 26, 2024
1 parent cd18d9d commit 0e5da06
Show file tree
Hide file tree
Showing 8 changed files with 4,659 additions and 3,525 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,6 @@ control/target/
web/packages/operations/.env.polkadot
web/packages/operations/.env.rococo
lodestar
db.sqlite*
.pnpm-store
deploy
2 changes: 2 additions & 0 deletions web/packages/api/src/environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export type Config = {
ASSET_HUB_URL: string
BRIDGE_HUB_URL: string
}
GRAPHQL_API_URL?: string
}

export type AddressType = "20byte" | "32byte" | "both"
Expand Down Expand Up @@ -477,6 +478,7 @@ export const SNOWBRIDGE_ENV: { [id: string]: SnowbridgeEnvironment } = {
ASSET_HUB_URL: "https://assethub-polkadot.api.subscan.io",
BRIDGE_HUB_URL: "https://bridgehub-polkadot.api.subscan.io",
},
GRAPHQL_API_URL: "https://data.snowbridge.network/graphql",
},
},
}
13 changes: 10 additions & 3 deletions web/packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ interface Config {
gateway: string
beefy: string
}
graphqlApiUrl?: string
}

interface AppContracts {
Expand Down Expand Up @@ -97,13 +98,19 @@ export const contextFactory = async (config: Config): Promise<Context> => {

const [relaychainApi, assetHubApi, bridgeHubApi] = await Promise.all([
ApiPromise.create({
provider: config.polkadot.url.relaychain.startsWith("http") ? new HttpProvider(config.polkadot.url.relaychain) : new WsProvider(config.polkadot.url.relaychain),
provider: config.polkadot.url.relaychain.startsWith("http")
? new HttpProvider(config.polkadot.url.relaychain)
: new WsProvider(config.polkadot.url.relaychain),
}),
ApiPromise.create({
provider: config.polkadot.url.assetHub.startsWith("http") ? new HttpProvider(config.polkadot.url.assetHub) : new WsProvider(config.polkadot.url.assetHub),
provider: config.polkadot.url.assetHub.startsWith("http")
? new HttpProvider(config.polkadot.url.assetHub)
: new WsProvider(config.polkadot.url.assetHub),
}),
ApiPromise.create({
provider: config.polkadot.url.bridgeHub.startsWith("http") ? new HttpProvider(config.polkadot.url.bridgeHub) : new WsProvider(config.polkadot.url.bridgeHub),
provider: config.polkadot.url.bridgeHub.startsWith("http")
? new HttpProvider(config.polkadot.url.bridgeHub)
: new WsProvider(config.polkadot.url.bridgeHub),
}),
])

Expand Down
27 changes: 25 additions & 2 deletions web/packages/api/src/status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Context } from "./index"
import { fetchBeaconSlot, fetchFinalityUpdate } from "./utils"
import { fetchBeaconSlot, fetchFinalityUpdate, fetchEstimatedDeliveryTime } from "./utils"
import { Relayer, SourceType } from "./environment"

export type OperatingMode = "Normal" | "Halted"
Expand Down Expand Up @@ -43,6 +43,7 @@ export type ChannelStatusInfo = {
inbound: number
previousOutbound: number
previousInbound: number
estimatedDeliveryTime?: number
}
toPolkadot: {
operatingMode: {
Expand All @@ -52,6 +53,7 @@ export type ChannelStatusInfo = {
inbound: number
previousOutbound: number
previousInbound: number
estimatedDeliveryTime?: number
}
}

Expand Down Expand Up @@ -119,7 +121,10 @@ export const bridgeStatusInfo = async (
)

// Beacon status
const [latestFinalizedBeaconBlock, latestBeaconBlock] = await Promise.all([fetchFinalityUpdate(context.config.ethereum.beacon_url), fetchBeaconSlot(context.config.ethereum.beacon_url, "head")])
const [latestFinalizedBeaconBlock, latestBeaconBlock] = await Promise.all([
fetchFinalityUpdate(context.config.ethereum.beacon_url),
fetchBeaconSlot(context.config.ethereum.beacon_url, "head"),
])
const latestBeaconBlockRoot = (
await context.polkadot.api.bridgeHub.query.ethereumBeaconClient.latestFinalizedBlockRoot()
).toHex()
Expand Down Expand Up @@ -227,12 +232,27 @@ export const channelStatusInfo = async (
await bridgeHubApiAt.query.ethereumOutboundQueue.nonce(channelId)
).toPrimitive() as number

let estimatedDeliveryTime: any
if (context.config.graphqlApiUrl) {
try {
estimatedDeliveryTime = await fetchEstimatedDeliveryTime(
context.config.graphqlApiUrl,
channelId
)
} catch (e: any) {
console.error("estimate api error:" + e.message)
}
}

return {
toEthereum: {
outbound: outbound_nonce_sub,
inbound: Number(inbound_nonce_eth),
previousOutbound: previous_outbound_nonce_sub,
previousInbound: Number(previous_inbound_nonce_eth),
estimatedDeliveryTime: Math.ceil(
Number(estimatedDeliveryTime?.toEthereumElapse?.elapse)
),
},
toPolkadot: {
operatingMode: {
Expand All @@ -242,6 +262,9 @@ export const channelStatusInfo = async (
inbound: inbound_nonce_sub,
previousOutbound: Number(previous_outbound_nonce_eth),
previousInbound: previous_inbound_nonce_sub,
estimatedDeliveryTime: Math.ceil(
Number(estimatedDeliveryTime?.toPolkadotElapse?.elapse)
),
},
}
}
14 changes: 14 additions & 0 deletions web/packages/api/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,17 @@ export const fetchFinalityUpdate = async (
attested_slot: Number(result?.data?.attested_header?.beacon?.slot),
}
}

export const fetchEstimatedDeliveryTime = async (graphqlUrl: string, channelId: string) => {
let response = await fetch(graphqlUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({
query: `query { toEthereumElapse(channelId:"${channelId}") { elapse } toPolkadotElapse(channelId:"${channelId}") { elapse } }`,
}),
})
let data = await response.json()
return data?.data
}
2 changes: 1 addition & 1 deletion web/packages/contract-types/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
"build": "typechain && tsc",
"build": "pnpm typechain && tsc",
"typechain": "typechain --target ethers-v6 '../../../contracts/out/?(IERC20.sol|IERC20Metadata.sol|IGateway.sol|BeefyClient.sol|WETH9.sol)/!(*.abi).json' --out-dir src"
},
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions web/packages/operations/src/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const monitor = async (): Promise<status.AllMetrics> => {
gateway: config.GATEWAY_CONTRACT,
beefy: config.BEEFY_CONTRACT,
},
graphqlApiUrl: process.env["GRAPHQL_API_URL"] || config.GRAPHQL_API_URL,
})

const bridgeStatus = await status.bridgeStatusInfo(context)
Expand Down
Loading

0 comments on commit 0e5da06

Please sign in to comment.