Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
277 changes: 277 additions & 0 deletions src/adapters/hyperbridge/index.ts
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",
Copy link
Member

@vrtnd vrtnd Dec 3, 2025

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 1000

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Member

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 to what do they mean?

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.

Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought isDeposit is not a strict requirement: #433 (comment)

Copy link
Member

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

Copy link
Member

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

Copy link
Contributor Author

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

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;
2 changes: 2 additions & 0 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ import teleswap from "./teleswap";
import agglayer from "./agglayer";
import fxrp from "./flare/fxrp";
import snowbridge from "./snowbridge";
import hyperbridge from "./hyperbridge";
import starkgate from "./starkgate";

export default {
Expand Down Expand Up @@ -198,6 +199,7 @@ export default {
agglayer,
fxrp,
snowbridge,
hyperbridge,
starkgate,
} as {
[bridge: string]: BridgeAdapter | AsyncBridgeAdapter;
Expand Down
11 changes: 10 additions & 1 deletion src/data/bridgeNetworkData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2696,4 +2696,13 @@ export default [
chains: ["Ethereum", "Starknet"],
destinationChain: "Starknet",
},
] as BridgeNetwork[];
{
id: 103,
displayName: "Hyperbridge",
bridgeDbName: "hyperbridge",
iconLink: "icons:hyperbridge",
largeTxThreshold: 10000,
url: "https://hyperbridge.network",
chains: ["Ethereum", "Arbitrum", "Base", "BSC", "Polygon", "Optimism", "Soneium", "Unichain"],
},
] as BridgeNetwork[];