Skip to content

Commit

Permalink
Single call with aggregated data to Improve the latency
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Jan 20, 2025
1 parent 4410757 commit c78b80d
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 24 deletions.
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

0 comments on commit c78b80d

Please sign in to comment.