Skip to content

Commit

Permalink
History data from subsquid indexer (#1362)
Browse files Browse the repository at this point in the history
* History api from subsquid

* Cleanup

* Add timestamp

* Update libraries to be consistent with UI

* Update package.json for test

* Add bridgeHubXcmDelivered into result

* Add legacy transfer history back
  • Loading branch information
yrong authored Jan 17, 2025
1 parent 086c50c commit 7bd8974
Show file tree
Hide file tree
Showing 16 changed files with 942 additions and 1,927 deletions.
2 changes: 1 addition & 1 deletion web/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "@snowbridge/web",
"private": true,
"engines": {
"node": "^20 || ^18",
"node": ">=20",
"pnpm": ">=8"
},
"scripts": {
Expand Down
14 changes: 7 additions & 7 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.25",
"version": "0.1.26-alpha.1",
"description": "Snowbridge API client",
"license": "Apache-2.0",
"repository": {
Expand All @@ -27,14 +27,14 @@
"typescript": "^5.4.5"
},
"dependencies": {
"@polkadot/api": "^14.2.1",
"@polkadot/keyring": "^13.2.2",
"@polkadot/types": "^14.2.1",
"@polkadot/util": "^13.2.2",
"@polkadot/util-crypto": "^13.2.2",
"@polkadot/api": "^14.3.1",
"@polkadot/keyring": "^13.2.3",
"@polkadot/types": "^14.3.1",
"@polkadot/util": "^13.2.3",
"@polkadot/util-crypto": "^13.2.3",
"@snowbridge/contract-types": "workspace:*",
"@typechain/ethers-v6": "^0.5.1",
"ethers": "^6.13.3",
"ethers": "^6.13.5",
"rxjs": "^7.8.1"
}
}
269 changes: 269 additions & 0 deletions web/packages/api/src/history_v2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
import {
fetchToPolkadotTransfers,
fetchToEthereumTransfers,
fetchBridgeHubOutboundMessageAccepted,
fetchEthereumInboundMessageDispatched,
fetchBridgeHubInboundMessageReceived,
fetchMessageProcessedOnPolkadot,
} from "./subsquid"
import { forwardedTopicId, getEventIndex } from "./utils"

export enum TransferStatus {
Pending,
Complete,
Failed,
}

export type TransferInfo = {
when: Date
sourceAddress: string
beneficiaryAddress: string
tokenAddress: string
destinationParachain?: number
destinationFee?: string
amount: string
}

export type ToPolkadotTransferResult = {
id: string
status: TransferStatus
info: TransferInfo
submitted: {
blockHash: string
blockNumber: number
logIndex: number
transactionHash: string
transactionIndex: number
channelId: string
messageId: string
nonce: number
parentBeaconSlot?: number
}
beaconClientIncluded?: {
extrinsic_index: string
extrinsic_hash: string
event_index: string
block_timestamp: number
beaconSlot: number
beaconBlockHash: string
}
inboundMessageReceived?: {
extrinsic_index: string
extrinsic_hash: string
event_index: string
block_timestamp: number
messageId: string
channelId: string
nonce: number
}
assetHubMessageProcessed?: {
extrinsic_hash: string
event_index: string
block_timestamp: number
success: boolean
sibling: number
}
}

export type ToEthereumTransferResult = {
id: string
status: TransferStatus
info: TransferInfo
submitted: {
extrinsic_index: string
extrinsic_hash: string
block_hash: string
account_id: string
block_num: number
block_timestamp: number
messageId: string
bridgeHubMessageId: string
success: boolean
relayChain?: {
block_hash: string
block_num: number
}
}
bridgeHubXcmDelivered?: {
extrinsic_hash: string
event_index: string
block_timestamp: number
siblingParachain: number
success: boolean
}
bridgeHubChannelDelivered?: {
extrinsic_hash: string
event_index: string
block_timestamp: number
channelId: string
success: boolean
}
bridgeHubMessageQueued?: {
extrinsic_hash: string
event_index: string
block_timestamp: number
}
bridgeHubMessageAccepted?: {
extrinsic_hash: string
event_index: string
block_timestamp: number
nonce: number
}
ethereumBeefyIncluded?: {
blockNumber: number
blockHash: string
transactionHash: string
transactionIndex: number
logIndex: number
relayChainblockNumber: number
mmrRoot: string
}
ethereumMessageDispatched?: {
blockNumber: number
blockHash: string
transactionHash: string
transactionIndex: number
logIndex: number
messageId: string
channelId: string
nonce: number
success: boolean
}
}

export const toPolkadotHistory = async (): Promise<ToPolkadotTransferResult[]> => {
const ethOutboundMessages = await fetchToPolkadotTransfers()
const results: ToPolkadotTransferResult[] = []
for (const outboundMessage of ethOutboundMessages) {
const result: ToPolkadotTransferResult = {
id: outboundMessage.id,
status: TransferStatus.Pending,
info: {
when: new Date(outboundMessage.timestamp),
sourceAddress: outboundMessage.senderAddress,
beneficiaryAddress: outboundMessage.destinationAddress,
tokenAddress: outboundMessage.tokenAddress,
destinationParachain: outboundMessage.destinationParaId,
destinationFee: "",
amount: outboundMessage.amount,
},
submitted: {
blockHash: "",
blockNumber: outboundMessage.blockNumber,
logIndex: 0,
transactionHash: outboundMessage.txHash,
transactionIndex: 0,
channelId: outboundMessage.channelId,
messageId: outboundMessage.messageId,
nonce: outboundMessage.nonce,
},
}
let inboundMessageReceived = await fetchBridgeHubInboundMessageReceived(result.id)
if (inboundMessageReceived) {
result.inboundMessageReceived = {
extrinsic_index: "",
extrinsic_hash: "",
event_index: getEventIndex(inboundMessageReceived.id),
block_timestamp: inboundMessageReceived.timestamp,
messageId: inboundMessageReceived.messageId,
channelId: inboundMessageReceived.channelId,
nonce: inboundMessageReceived.nonce,
}
}

const assetHubMessageProcessed = await fetchMessageProcessedOnPolkadot(result.id)
if (assetHubMessageProcessed) {
result.assetHubMessageProcessed = {
extrinsic_hash: "",
event_index: getEventIndex(assetHubMessageProcessed.id),
block_timestamp: assetHubMessageProcessed.timestamp,
success: assetHubMessageProcessed.success,
sibling: 0,
}
if (!result.assetHubMessageProcessed.success) {
result.status = TransferStatus.Failed
continue
}

result.status = TransferStatus.Complete
}

results.push(result)
}
return results
}

export const toEthereumHistory = async (): Promise<ToEthereumTransferResult[]> => {
const allTransfers = await fetchToEthereumTransfers()
const results: ToEthereumTransferResult[] = []
for (const transfer of allTransfers) {
let bridgeHubMessageId = forwardedTopicId(transfer.id)
const result: ToEthereumTransferResult = {
id: transfer.id,
status: TransferStatus.Pending,
info: {
when: new Date(transfer.timestamp),
sourceAddress: transfer.senderAddress,
tokenAddress: transfer.tokenAddress,
beneficiaryAddress: transfer.destinationAddress,
amount: transfer.amount,
},
submitted: {
extrinsic_index: "",
extrinsic_hash: transfer.txHash,
block_hash: "",
account_id: transfer.senderAddress,
block_num: transfer.blockNumber,
block_timestamp: transfer.timestamp,
messageId: transfer.id,
bridgeHubMessageId,
success: true,
},
}
let bridgeHubXcmDelivered = await fetchMessageProcessedOnPolkadot(bridgeHubMessageId)
if (bridgeHubXcmDelivered) {
result.bridgeHubXcmDelivered = {
block_timestamp: bridgeHubXcmDelivered.timestamp,
event_index: getEventIndex(bridgeHubXcmDelivered.id),
extrinsic_hash: "",
siblingParachain: 1000,
success: bridgeHubXcmDelivered.success,
}
if (!result.bridgeHubXcmDelivered.success) {
result.status = TransferStatus.Failed
continue
}
}

let outboundQueueAccepted = await fetchBridgeHubOutboundMessageAccepted(transfer.id)
if (outboundQueueAccepted) {
result.bridgeHubMessageQueued = {
block_timestamp: outboundQueueAccepted.timestamp,
event_index: getEventIndex(outboundQueueAccepted.id),
extrinsic_hash: "",
}
}

let ethereumMessageDispatched = await fetchEthereumInboundMessageDispatched(transfer.id)
if (ethereumMessageDispatched) {
result.ethereumMessageDispatched = {
blockNumber: ethereumMessageDispatched.blockNumber,
blockHash: "",
transactionHash: ethereumMessageDispatched.txHash,
transactionIndex: 0,
logIndex: 0,
messageId: ethereumMessageDispatched.messageId,
channelId: ethereumMessageDispatched.channelId,
nonce: ethereumMessageDispatched.nonce,
success: ethereumMessageDispatched.success,
}
if (!result.ethereumMessageDispatched.success) {
result.status = TransferStatus.Failed
continue
}
result.status = TransferStatus.Complete
}
results.push(result)
}
return results
}
2 changes: 2 additions & 0 deletions web/packages/api/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,5 @@ export * as assets from "./assets"
export * as environment from "./environment"
export * as subscan from "./subscan"
export * as history from "./history"
export * as historyV2 from "./history_v2"
export * as subsquid from "./subsquid"
Loading

0 comments on commit 7bd8974

Please sign in to comment.