Skip to content

Commit

Permalink
Improve the latency for history api (#1365)
Browse files Browse the repository at this point in the history
* Single call with aggregated data to Improve the latency

* Update packages

* Update packages
  • Loading branch information
yrong authored Jan 21, 2025
1 parent 01b2b53 commit 1688f1a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 27 deletions.
2 changes: 1 addition & 1 deletion 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.26",
"version": "0.1.27",
"description": "Snowbridge API client",
"license": "Apache-2.0",
"repository": {
Expand Down
25 changes: 9 additions & 16 deletions web/packages/api/src/history_v2.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import {
fetchToPolkadotTransfers,
fetchToEthereumTransfers,
fetchBridgeHubOutboundMessageAccepted,
fetchEthereumInboundMessageDispatched,
fetchBridgeHubInboundMessageReceived,
fetchMessageProcessedOnPolkadot,
} from "./subsquid"
import { fetchToPolkadotTransfers, fetchToEthereumTransfers } from "./subsquid"
import { forwardedTopicId, getEventIndex } from "./utils"

export enum TransferStatus {
Expand Down Expand Up @@ -158,7 +151,7 @@ export const toPolkadotHistory = async (): Promise<ToPolkadotTransferResult[]> =
nonce: outboundMessage.nonce,
},
}
let inboundMessageReceived = await fetchBridgeHubInboundMessageReceived(result.id)
let inboundMessageReceived = outboundMessage.toBridgeHubInboundQueue
if (inboundMessageReceived) {
result.inboundMessageReceived = {
extrinsic_index: "",
Expand All @@ -171,7 +164,7 @@ export const toPolkadotHistory = async (): Promise<ToPolkadotTransferResult[]> =
}
}

const assetHubMessageProcessed = await fetchMessageProcessedOnPolkadot(result.id)
const assetHubMessageProcessed = outboundMessage.toAssetHubMessageQueue
if (assetHubMessageProcessed) {
result.assetHubMessageProcessed = {
extrinsic_hash: "",
Expand All @@ -180,7 +173,7 @@ export const toPolkadotHistory = async (): Promise<ToPolkadotTransferResult[]> =
success: assetHubMessageProcessed.success,
sibling: 0,
}
if (!result.assetHubMessageProcessed.success) {
if (!assetHubMessageProcessed.success) {
result.status = TransferStatus.Failed
continue
}
Expand Down Expand Up @@ -220,7 +213,7 @@ export const toEthereumHistory = async (): Promise<ToEthereumTransferResult[]> =
success: true,
},
}
let bridgeHubXcmDelivered = await fetchMessageProcessedOnPolkadot(bridgeHubMessageId)
let bridgeHubXcmDelivered = transfer.toBridgeHubMessageQueue
if (bridgeHubXcmDelivered) {
result.bridgeHubXcmDelivered = {
block_timestamp: bridgeHubXcmDelivered.timestamp,
Expand All @@ -229,13 +222,13 @@ export const toEthereumHistory = async (): Promise<ToEthereumTransferResult[]> =
siblingParachain: 1000,
success: bridgeHubXcmDelivered.success,
}
if (!result.bridgeHubXcmDelivered.success) {
if (!bridgeHubXcmDelivered.success) {
result.status = TransferStatus.Failed
continue
}
}

let outboundQueueAccepted = await fetchBridgeHubOutboundMessageAccepted(transfer.id)
let outboundQueueAccepted = transfer.toBridgeHubOutboundQueue
if (outboundQueueAccepted) {
result.bridgeHubMessageQueued = {
block_timestamp: outboundQueueAccepted.timestamp,
Expand All @@ -244,7 +237,7 @@ export const toEthereumHistory = async (): Promise<ToEthereumTransferResult[]> =
}
}

let ethereumMessageDispatched = await fetchEthereumInboundMessageDispatched(transfer.id)
let ethereumMessageDispatched = transfer.toDestination
if (ethereumMessageDispatched) {
result.ethereumMessageDispatched = {
blockNumber: ethereumMessageDispatched.blockNumber,
Expand All @@ -257,7 +250,7 @@ export const toEthereumHistory = async (): Promise<ToEthereumTransferResult[]> =
nonce: ethereumMessageDispatched.nonce,
success: ethereumMessageDispatched.success,
}
if (!result.ethereumMessageDispatched.success) {
if (!ethereumMessageDispatched.success) {
result.status = TransferStatus.Failed
continue
}
Expand Down
52 changes: 44 additions & 8 deletions web/packages/api/src/subsquid.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,47 @@ export const fetchToPolkadotTransfers = async () => {
id
status
blockNumber
bridgedBlockNumber
channelId
destinationAddress
destinationBlockNumber
destinationParaId
forwardedBlockNumber
messageId
nonce
senderAddress
timestamp
tokenAddress
txHash
amount
toBridgeHubInboundQueue {
id
timestamp
txHash
channelId
nonce
messageId
}
toAssetHubMessageQueue {
id
success
timestamp
}
toDestination {
id
success
timestamp
}
}
}`
let result = await queryByGraphQL(query)
return result.transferStatusToPolkadots
return result?.transferStatusToPolkadots
}

export const fetchToEthereumTransfers = async () => {
let query = `query { transferStatusToEthereums(limit: ${graphqlQuerySize}, orderBy: blockNumber_DESC) {
id
status
blockNumber
bridgedBlockNumber
channelId
destinationAddress
destinationBlockNumber
forwardedBlockNumber
messageId
nonce
senderAddress
Expand All @@ -43,10 +55,34 @@ export const fetchToEthereumTransfers = async () => {
tokenAddress
txHash
amount
toAssetHubMessageQueue {
id
success
timestamp
}
toBridgeHubMessageQueue {
id
success
timestamp
}
toBridgeHubOutboundQueue {
id
timestamp
}
toDestination {
id
blockNumber
timestamp
txHash
success
messageId
nonce
channelId
}
}
}`
let result = await queryByGraphQL(query)
return result.transferStatusToEthereums
return result?.transferStatusToEthereums
}

export const fetchBridgeHubOutboundMessageAccepted = async (messageID: string) => {
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.26",
"version": "0.1.27",
"description": "Snowbridge contract type bindings",
"license": "Apache-2.0",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion web/packages/contracts/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@snowbridge/contracts",
"version": "0.1.26",
"version": "0.1.27",
"description": "Snowbridge contract source and abi.",
"license": "Apache-2.0",
"repository": {
Expand Down

0 comments on commit 1688f1a

Please sign in to comment.