-
Notifications
You must be signed in to change notification settings - Fork 408
kaspabridge added #438
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
Open
muhammadabubakar7526
wants to merge
9
commits into
DefiLlama:master
Choose a base branch
from
muhammadabubakar7526:kaspa-bridge
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+148
−0
Open
kaspabridge added #438
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
bc5a7f1
kaspabridge added
muhammadabubakar7526 7d2af28
Merge branch 'master' into kaspa-bridge
muhammadabubakar7526 01660cb
Merge branch 'master' into kaspa-bridge
muhammadabubakar7526 761f867
updated logo
muhammadabubakar7526 0b219b6
pull from master branch
muhammadabubakar7526 21a2f9b
pull from kaspa bridge branch to update the code
muhammadabubakar7526 1297c08
fixed starkgate data entery
muhammadabubakar7526 6e0b35d
fixed id order as mentioned in comment
muhammadabubakar7526 e85ac9d
logo link updated from global repo of defilama icons
muhammadabubakar7526 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
Some comments aren't visible on the classic Files Changed page.
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
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,130 @@ | ||
| import { BridgeAdapter } from "../../helpers/bridgeAdapter.type"; | ||
| import { Contract, providers, utils, BigNumber } from "ethers"; | ||
|
|
||
| // ---------- CONFIG ---------- | ||
|
|
||
| const CHAIN_RPC: Record<string, string> = { | ||
| kasplex: "https://evmrpc.kasplex.org", | ||
| bsc: "https://bsc-rpc.publicnode.com", | ||
| }; | ||
|
|
||
| const MAILBOX: Record<string, string> = { | ||
| kasplex: "0x01e6A1a37942b51b08B047DdfDf345507A818d4d", | ||
| bsc: "0x5f297B3A1e8154c4D58702F7a880b7631bBf5340", | ||
| }; | ||
|
|
||
| // Minimal ABI for Dispatch / Process events | ||
| const MAILBOX_ABI = [ | ||
| "event Dispatch(bytes32 indexed id, uint32 indexed destinationDomain, bytes32 recipientAddress, bytes messageBody)", | ||
| "event Process(bytes32 indexed id, bool success)", | ||
| ]; | ||
|
|
||
| // ---------- TYPES ---------- | ||
|
|
||
| export interface KasplexEvent { | ||
| txHash: string; | ||
| blockNumber: number; | ||
| from: string; | ||
| to: string; | ||
| token: string; | ||
| isDeposit: boolean; | ||
| amount: BigNumber; | ||
| } | ||
|
|
||
| // ---------- HELPERS ---------- | ||
|
|
||
| function decodeMessageBody(messageBody: string) { | ||
| try { | ||
| const abiCoder = new utils.AbiCoder(); | ||
| const decoded = abiCoder.decode(["address", "address", "address", "uint256"], messageBody); | ||
| return { | ||
| from: decoded[0] as string, | ||
| to: decoded[1] as string, | ||
| token: decoded[2] as string, | ||
| amount: BigNumber.from(decoded[3] as BigNumber), | ||
| }; | ||
| } catch (e) { | ||
| console.error("Failed to decode messageBody:", e); | ||
| return { | ||
| from: "0x0000000000000000000000000000000000000000", | ||
| to: "0x0000000000000000000000000000000000000000", | ||
| token: "0x0000000000000000000000000000000000000000", | ||
| amount: BigNumber.from(0), | ||
| }; | ||
| } | ||
| } | ||
|
|
||
| // ---------- MAIN ADAPTER FUNCTION ---------- | ||
|
|
||
| const BLOCK_CHUNK = 2_000; | ||
|
|
||
| export const getKaspaBridgeEvents = | ||
| (chain: string) => | ||
| async (fromBlock: number, toBlock: number): Promise<KasplexEvent[]> => { | ||
| const rpcUrl = CHAIN_RPC[chain]; | ||
| const mailboxAddress = MAILBOX[chain]; | ||
| if (!rpcUrl || !mailboxAddress) { | ||
| throw new Error(`Missing kaspabridge config for chain ${chain}`); | ||
| } | ||
|
|
||
| const provider = new providers.JsonRpcProvider(rpcUrl); | ||
| const mailbox = new Contract(mailboxAddress, MAILBOX_ABI, provider); | ||
| const events: KasplexEvent[] = []; | ||
|
|
||
| for (let start = fromBlock; start <= toBlock; start += BLOCK_CHUNK) { | ||
| const end = Math.min(start + BLOCK_CHUNK - 1, toBlock); | ||
|
|
||
| const [dispatchLogs, processLogs] = await Promise.all([ | ||
| provider.getLogs({ ...mailbox.filters.Dispatch(), fromBlock: start, toBlock: end }), | ||
| provider.getLogs({ ...mailbox.filters.Process(), fromBlock: start, toBlock: end }), | ||
| ]); | ||
|
|
||
| for (const log of dispatchLogs) { | ||
| const parsed = mailbox.interface.parseLog(log); | ||
| const decoded = decodeMessageBody(parsed.args["messageBody"] as string); | ||
|
|
||
| events.push({ | ||
| txHash: log.transactionHash, | ||
| blockNumber: log.blockNumber, | ||
| from: decoded.from, | ||
| to: decoded.to, | ||
| token: decoded.token, | ||
| isDeposit: true, | ||
| amount: decoded.amount, | ||
| }); | ||
| } | ||
|
|
||
| for (const log of processLogs) { | ||
| events.push({ | ||
| txHash: log.transactionHash, | ||
| blockNumber: log.blockNumber, | ||
| from: "0x0000000000000000000000000000000000000000", | ||
| to: "0x0000000000000000000000000000000000000000", | ||
| token: "0x0000000000000000000000000000000000000000", | ||
| isDeposit: false, | ||
| amount: BigNumber.from(0), | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return events; | ||
| }; | ||
|
|
||
| // ---------- BRIDGE ADAPTER ---------- | ||
|
|
||
| export async function setUp(): Promise<string[]> { | ||
| return Object.keys(CHAIN_RPC); | ||
| } | ||
|
|
||
| export async function build(): Promise<BridgeAdapter> { | ||
| const adapter: BridgeAdapter = {}; | ||
| const chains = await setUp(); | ||
|
|
||
| for (const chain of chains) { | ||
| adapter[chain] = getKaspaBridgeEvents(chain); | ||
| } | ||
|
|
||
| return adapter; | ||
| } | ||
|
|
||
| export default { isAsync: true, build }; | ||
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.
need to use defillama sdk (public sdk doesnt have old blocks)
since this bridge is 2 way u can do similar to arbitrum bridge
set kasplex as destination chain and track events only on bsc