-
Notifications
You must be signed in to change notification settings - Fork 408
feat: track hyperbridge evm activity #433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f7953fe
feat: track hyperbridge evm activity
royvardhan db4a7a9
Merge branch 'master' into hyperbridge
royvardhan 828cf55
track ismp events
royvardhan ca0c5cb
extract to and from using logs
royvardhan 58c80e6
remove getTokenFromReceipt to avoid warning
royvardhan 0614643
mark directing using ismp module
royvardhan fc32fed
Merge branch 'master' into hyperbridge
royvardhan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,277 @@ | ||
| import { BridgeAdapter, ContractEventParams, PartialContractEventParams } from "../../helpers/bridgeAdapter.type"; | ||
| import { getTxDataFromEVMEventLogs } from "../../helpers/processTransactions"; | ||
| import { Chain } from "@defillama/sdk/build/general"; | ||
| import { EventData } from "../../utils/types"; | ||
| import { getProvider } from "../../utils/provider"; | ||
| import { ethers } from "ethers"; | ||
|
|
||
| const TRANSFER_TOPIC = "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef"; | ||
|
|
||
| const ismpHostAddresses = { | ||
| ethereum: "0x792A6236AF69787C40cF76b69B4c8c7B28c4cA20", | ||
| arbitrum: "0xE05AFD4Eb2ce6d65c40e1048381BD0Ef8b4B299e", | ||
| base: "0x6FFe92e4d7a9D589549644544780e6725E84b248", | ||
| bsc: "0x24B5d421Ec373FcA57325dd2F0C074009Af021F7", | ||
| polygon: "0xD8d3db17C1dF65b301D45C84405CcAC1395C559a", | ||
| unichain: "0x2A17C1c3616Bbc33FCe5aF5B965F166ba76cEDAf", | ||
| soneium: "0x7F0165140D0f3251c8f6465e94E9d12C7FD40711", | ||
| optimism: "0x78c8A5F27C06757EA0e30bEa682f1FD5C8d7645d", | ||
| } as const; | ||
|
|
||
| type SupportedChains = keyof typeof ismpHostAddresses; | ||
|
|
||
| // Extract ISMP module addresses from events | ||
| // Returns a map of txHash -> Set of ISMP module addresses | ||
| const extractIsmpModuleAddresses = (events: EventData[]): Map<string, Set<string>> => { | ||
| const ismpModulesByTx = new Map<string, Set<string>>(); | ||
|
|
||
| for (const event of events) { | ||
| if (!ismpModulesByTx.has(event.txHash)) { | ||
| ismpModulesByTx.set(event.txHash, new Set<string>()); | ||
| } | ||
| const modules = ismpModulesByTx.get(event.txHash)!; | ||
|
|
||
| if (event.from) { | ||
| modules.add(event.from.toLowerCase()); | ||
| } | ||
|
|
||
| if (event.to) { | ||
| modules.add(event.to.toLowerCase()); | ||
| } | ||
| } | ||
|
|
||
| return ismpModulesByTx; | ||
| }; | ||
|
|
||
| // For each ISMP event, scan its tx for ERC20 Transfer logs and turn those into EventData. | ||
| const extractTransfersFromIsmpEvents = async ( | ||
| events: EventData[], | ||
| chain: Chain, | ||
| ismpModulesByTx: Map<string, Set<string>> | ||
| ): Promise<EventData[]> => { | ||
| const provider = getProvider(chain as string) as any; | ||
| const transfers: EventData[] = []; | ||
|
|
||
| for (const event of events) { | ||
| const ismpModules = ismpModulesByTx.get(event.txHash); | ||
| if (!ismpModules || ismpModules.size === 0) continue; | ||
|
|
||
| let txReceipt: any; | ||
| try { | ||
| txReceipt = await provider.getTransactionReceipt(event.txHash); | ||
| } catch (e) { | ||
| console.error(`Error fetching tx receipt for hyperbridge tx ${event.txHash} on ${chain}:`, e); | ||
| continue; | ||
| } | ||
|
|
||
| if (!txReceipt?.logs) continue; | ||
|
|
||
| for (const log of txReceipt.logs) { | ||
| if (!log.topics || log.topics.length < 3) continue; | ||
| if (log.topics[0] !== TRANSFER_TOPIC) continue; | ||
|
|
||
| const from = "0x" + log.topics[1].slice(26); | ||
| const to = "0x" + log.topics[2].slice(26); | ||
| const token = log.address; | ||
| const amount = ethers.BigNumber.from(log.data); | ||
|
|
||
| const fromLower = from.toLowerCase(); | ||
| const toLower = to.toLowerCase(); | ||
| const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"; | ||
|
|
||
| // Special Case for Token Gateway | ||
| // If `from` is zero address → deposit, if `to` is zero address → withdrawal | ||
| let isDeposit: boolean | null = null; | ||
| if (fromLower === ZERO_ADDRESS) { | ||
| isDeposit = true; // Mint/deposit | ||
| } else if (toLower === ZERO_ADDRESS) { | ||
| isDeposit = false; // Burn/withdrawal | ||
| } | ||
|
|
||
| // If zero address check didn't determine the type, check ISMP module addresses | ||
| if (isDeposit === null) { | ||
| const isFromIsmpModule = ismpModules.has(fromLower); | ||
| const isToIsmpModule = ismpModules.has(toLower); | ||
|
|
||
| // If transfer is FROM an ISMP module → withdrawal (isDeposit = false) | ||
| // If transfer is TO an ISMP module → deposit (isDeposit = true) | ||
| if (isToIsmpModule) { | ||
| isDeposit = true; | ||
| } else if (isFromIsmpModule) { | ||
| isDeposit = false; | ||
| } else { | ||
| isDeposit = false; | ||
| } | ||
| } | ||
|
|
||
| transfers.push({ | ||
| txHash: event.txHash, | ||
| blockNumber: event.blockNumber, | ||
| from, | ||
| to, | ||
| token, | ||
| amount, | ||
| isDeposit, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return transfers; | ||
| }; | ||
|
|
||
| const constructParams = (chain: SupportedChains) => { | ||
| const ismpHost = ismpHostAddresses[chain]; | ||
|
|
||
| return async (fromBlock: number, toBlock: number) => { | ||
| const postRequestEventParams = { | ||
| target: ismpHost, | ||
| topic: "PostRequestEvent(string,string,address,bytes,uint256,uint256,bytes,uint256)", | ||
| abi: [ | ||
| "event PostRequestEvent(string source, string dest, address indexed from, bytes to, uint256 nonce, uint256 timeoutTimestamp, bytes body, uint256 fee)", | ||
| ], | ||
| logKeys: { | ||
| blockNumber: "blockNumber", | ||
| txHash: "transactionHash", | ||
| }, | ||
| argKeys: { | ||
| from: "from", | ||
| to: "to", | ||
| }, | ||
| }; | ||
|
|
||
| const postRequestHandledParams = { | ||
| target: ismpHost, | ||
| topic: "PostRequestHandled(bytes32,address)", | ||
| abi: ["event PostRequestHandled(bytes32 indexed commitment, address relayer)"], | ||
| logKeys: { | ||
| blockNumber: "blockNumber", | ||
| txHash: "transactionHash", | ||
| }, | ||
| }; | ||
|
|
||
| const postResponseEventParams = { | ||
| target: ismpHost, | ||
| topic: "PostResponseEvent(string,string,address,bytes,uint256,uint256,bytes,bytes,uint256,uint256)", | ||
| abi: [ | ||
| "event PostResponseEvent(string source, string dest, address indexed from, bytes to, uint256 nonce, uint256 timeoutTimestamp, bytes body, bytes response, uint256 responseTimeoutTimestamp, uint256 fee)", | ||
| ], | ||
| logKeys: { | ||
| blockNumber: "blockNumber", | ||
| txHash: "transactionHash", | ||
| }, | ||
| argKeys: { | ||
| from: "from", | ||
| to: "to", | ||
| }, | ||
| }; | ||
|
|
||
| const postResponseHandledParams = { | ||
| target: ismpHost, | ||
| topic: "PostResponseHandled(bytes32,address)", | ||
| abi: ["event PostResponseHandled(bytes32 indexed commitment, address relayer)"], | ||
| logKeys: { | ||
| blockNumber: "blockNumber", | ||
| txHash: "transactionHash", | ||
| }, | ||
| }; | ||
|
|
||
| // ========== ISMP Host Get Request Events ========== | ||
|
|
||
| const getRequestEventParams = { | ||
| target: ismpHost, | ||
| topic: "GetRequestEvent(string,string,address,bytes[],uint256,uint256,uint256,bytes,uint256)", | ||
| abi: [ | ||
| "event GetRequestEvent(string source, string dest, address indexed from, bytes[] keys, uint256 height, uint256 nonce, uint256 timeoutTimestamp, bytes context, uint256 fee)", | ||
| ], | ||
| logKeys: { | ||
| blockNumber: "blockNumber", | ||
| txHash: "transactionHash", | ||
| }, | ||
| argKeys: { | ||
| from: "from", | ||
| }, | ||
| }; | ||
|
|
||
| const getRequestHandledParams = { | ||
| target: ismpHost, | ||
| topic: "GetRequestHandled(bytes32,address)", | ||
| abi: ["event GetRequestHandled(bytes32 indexed commitment, address relayer)"], | ||
| logKeys: { | ||
| blockNumber: "blockNumber", | ||
| txHash: "transactionHash", | ||
| }, | ||
| }; | ||
|
|
||
| // ========== ISMP Host Timeout Events ========== | ||
|
|
||
| const postRequestTimeoutHandledParams = { | ||
| target: ismpHost, | ||
| topic: "PostRequestTimeoutHandled(bytes32,string)", | ||
| abi: ["event PostRequestTimeoutHandled(bytes32 indexed commitment, string dest)"], | ||
| logKeys: { | ||
| blockNumber: "blockNumber", | ||
| txHash: "transactionHash", | ||
| }, | ||
| }; | ||
|
|
||
| const postResponseTimeoutHandledParams = { | ||
| target: ismpHost, | ||
| topic: "PostResponseTimeoutHandled(bytes32,string)", | ||
| abi: ["event PostResponseTimeoutHandled(bytes32 indexed commitment, string dest)"], | ||
| logKeys: { | ||
| blockNumber: "blockNumber", | ||
| txHash: "transactionHash", | ||
| }, | ||
| }; | ||
|
|
||
| const getRequestTimeoutHandledParams = { | ||
| target: ismpHost, | ||
| topic: "GetRequestTimeoutHandled(bytes32,string)", | ||
| abi: ["event GetRequestTimeoutHandled(bytes32 indexed commitment, string dest)"], | ||
| logKeys: { | ||
| blockNumber: "blockNumber", | ||
| txHash: "transactionHash", | ||
| }, | ||
| }; | ||
|
|
||
| const ismpEventParams = [ | ||
| postRequestEventParams, | ||
| postRequestHandledParams, | ||
| postResponseEventParams, | ||
| postResponseHandledParams, | ||
| getRequestEventParams, | ||
| getRequestHandledParams, | ||
| postRequestTimeoutHandledParams, | ||
| postResponseTimeoutHandledParams, | ||
| getRequestTimeoutHandledParams, | ||
| ]; | ||
|
|
||
| const ismpEvents = await getTxDataFromEVMEventLogs( | ||
| "hyperbridge", | ||
| chain as Chain, | ||
| fromBlock, | ||
| toBlock, | ||
| ismpEventParams as ContractEventParams[] | ||
| ); | ||
|
|
||
| // Extract ISMP module addresses from events | ||
| const ismpModulesByTx = extractIsmpModuleAddresses(ismpEvents); | ||
|
|
||
| // Extract transfers and classify them as deposits/withdrawals | ||
| const transferEvents = await extractTransfersFromIsmpEvents(ismpEvents, chain as Chain, ismpModulesByTx); | ||
| return transferEvents; | ||
| }; | ||
| }; | ||
|
|
||
| const adapter: BridgeAdapter = { | ||
| bsc: constructParams("bsc"), | ||
| soneium: constructParams("soneium"), | ||
| optimism: constructParams("optimism"), | ||
| ethereum: constructParams("ethereum"), | ||
| arbitrum: constructParams("arbitrum"), | ||
| base: constructParams("base"), | ||
| polygon: constructParams("polygon"), | ||
| unichain: constructParams("unichain"), | ||
| }; | ||
|
|
||
| export default adapter; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing from and to in all events
please check by running tests
npm run test hyperbridge 1000There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If from and to denotes the transfer direction then I don't think it fits what we are doing here.
These are messaging layer events, and inside these events the transfer of tokens can happen from any dapp integrating Hyperbridge.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A dapp at Chain A can dispatch a GetRequest to get some data from Chain B, and when GetResponse is received, there may or may not be events of transfer.
In simple terms, a dapp can integrate Hyperbridge, use the messaging functions to execute certain operations, and in these operations, there can be transfer events.
So if we look at these messaging layer events, we will find the transfer events.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
but there is from and to in event abi
event PostResponseEvent(string source, string dest, address indexed from, bytes towhat do they mean?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The events in this PR are all ISMP (Hyperbridge's messaging protocol) events, and the from and to addresses in the event signatures are the contract addresses that send or receive the message.
Our methodology for tracking volume on Hyperbridge is to sum all token Transfer events from any transaction that emits any ISMP event. The core idea is that any token Transfer event found in a transaction that emits an ISMP event was triggered by the processing or dispatch of a cross-chain message on the Hyperbridge protocol.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
all of your events are missing the isDeposit param. can we figure out which event is related to deposit (user -> smart contract) and which is the opposite?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought
isDepositis not a strict requirement: #433 (comment)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we still need to track direction of transfers
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i suggest using api if possible
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Have added the logic to parse the direction of transfers. Please check dear ser. @vrtnd