|
| 1 | +import { describe, it } from 'bun:test'; |
| 2 | + |
| 3 | +// ANCHOR: imports |
| 4 | +import { JsonRpcProvider, Wallet } from 'ethers'; |
| 5 | +import { createEthersClient, createEthersSdk } from '../../../../src/adapters/ethers'; |
| 6 | + |
| 7 | +const L1_RPC = 'http://localhost:8545'; // e.g. https://sepolia.infura.io/v3/XXX |
| 8 | +const GW_RPC = 'http://localhost:3052'; // gateway chain RPC |
| 9 | +const SRC_L2_RPC = 'http://localhost:3050'; // source L2 RPC |
| 10 | +const DST_L2_RPC = 'http://localhost:3051'; // destination L2 RPC |
| 11 | +const PRIVATE_KEY = process.env.PRIVATE_KEY || ''; |
| 12 | +const TOKEN_SRC_ADDRESS = process.env.TOKEN_SRC_ADDRESS || ''; // ERC-20 token on source L2 |
| 13 | +// ANCHOR_END: imports |
| 14 | + |
| 15 | +describe('ethers interop guide', () => { |
| 16 | + |
| 17 | +it('sends ERC-20 across chains', async () => { |
| 18 | + await main(); |
| 19 | +}); |
| 20 | + |
| 21 | +}); |
| 22 | + |
| 23 | +// ANCHOR: main |
| 24 | +async function main() { |
| 25 | + if (!PRIVATE_KEY) throw new Error('Set PRIVATE_KEY in env'); |
| 26 | + |
| 27 | + const l1 = new JsonRpcProvider(L1_RPC); |
| 28 | + const l2Src = new JsonRpcProvider(SRC_L2_RPC); |
| 29 | + const l2Dst = new JsonRpcProvider(DST_L2_RPC); |
| 30 | + |
| 31 | + const signer = new Wallet(PRIVATE_KEY); |
| 32 | + const client = createEthersClient({ l1, l2: l2Src, signer }); |
| 33 | + const sdk = createEthersSdk(client, { |
| 34 | + interop: { gwChain: GW_RPC }, |
| 35 | + }); |
| 36 | + |
| 37 | + const me = (await signer.getAddress()) as `0x${string}`; |
| 38 | + const params = { |
| 39 | + actions: [ |
| 40 | + { |
| 41 | + type: 'sendErc20' as const, |
| 42 | + token: TOKEN_SRC_ADDRESS as `0x${string}`, |
| 43 | + to: me, |
| 44 | + amount: 1_000_000n, |
| 45 | + }, |
| 46 | + ], |
| 47 | + }; |
| 48 | + |
| 49 | + // Send the interop bundle on source L2 |
| 50 | + const handle = await sdk.interop.create(l2Dst, params); |
| 51 | + console.log('Source L2 tx:', handle.l2SrcTxHash); |
| 52 | + |
| 53 | + // Wait until the bundle proof is available on destination |
| 54 | + const finalizationInfo = await sdk.interop.wait(l2Dst, handle, { |
| 55 | + pollMs: 5_000, |
| 56 | + timeoutMs: 30 * 60_000, |
| 57 | + }); |
| 58 | + |
| 59 | + // Execute the bundle on destination L2 |
| 60 | + const result = await sdk.interop.finalize(l2Dst, finalizationInfo); |
| 61 | + console.log('Executed on destination:', result.dstExecTxHash); |
| 62 | +} |
| 63 | +// ANCHOR_END: main |
| 64 | + |
| 65 | +// ANCHOR: quote |
| 66 | +const quote = await sdk.interop.quote(l2Dst, params); |
| 67 | +// { |
| 68 | +// route: 'direct' | 'indirect', |
| 69 | +// approvalsNeeded: [{ token, spender, amount }], // ERC-20 approval needed |
| 70 | +// totalActionValue: 0n, |
| 71 | +// bridgedTokenTotal: 1_000_000n, |
| 72 | +// interopFee: { token: '0x000...', amount: bigint }, |
| 73 | +// l2Fee?: bigint |
| 74 | +// } |
| 75 | +// ANCHOR_END: quote |
| 76 | + |
| 77 | +// ANCHOR: prepare |
| 78 | +const plan = await sdk.interop.prepare(l2Dst, params); |
| 79 | +// { |
| 80 | +// route: 'direct' | 'indirect', |
| 81 | +// summary: InteropQuote, |
| 82 | +// steps: [{ key, kind, description, tx: TransactionRequest }] |
| 83 | +// } |
| 84 | +// ANCHOR_END: prepare |
| 85 | + |
| 86 | +// ANCHOR: create |
| 87 | +const handle = await sdk.interop.create(l2Dst, params); |
| 88 | +// { |
| 89 | +// kind: 'interop', |
| 90 | +// l2SrcTxHash: Hex, // tx hash on source L2 |
| 91 | +// stepHashes: Record<string, Hex>, |
| 92 | +// plan: InteropPlan |
| 93 | +// } |
| 94 | +// ANCHOR_END: create |
| 95 | + |
| 96 | +// ANCHOR: status |
| 97 | +const st = await sdk.interop.status(l2Dst, handle); |
| 98 | +// st.phase: 'SENT' | 'VERIFIED' | 'EXECUTED' | 'UNBUNDLED' | 'FAILED' | 'UNKNOWN' |
| 99 | +// ANCHOR_END: status |
| 100 | + |
| 101 | +// ANCHOR: wait |
| 102 | +const finalizationInfo = await sdk.interop.wait(l2Dst, handle, { |
| 103 | + pollMs: 5_000, // how often to poll (ms), default 5000 |
| 104 | + timeoutMs: 30 * 60_000, // max wait time (ms) |
| 105 | +}); |
| 106 | +// Returns InteropFinalizationInfo once bundle proof is available on destination |
| 107 | +// ANCHOR_END: wait |
| 108 | + |
| 109 | +// ANCHOR: finalize |
| 110 | +const result = await sdk.interop.finalize(l2Dst, finalizationInfo); |
| 111 | +// { bundleHash: Hex, dstExecTxHash: Hex } |
| 112 | +console.log('Executed on destination:', result.dstExecTxHash); |
| 113 | +// ANCHOR_END: finalize |
| 114 | + |
| 115 | +// ANCHOR: try-catch-create |
| 116 | +try { |
| 117 | + const handle = await sdk.interop.create(l2Dst, params); |
| 118 | + const finalizationInfo = await sdk.interop.wait(l2Dst, handle); |
| 119 | + const result = await sdk.interop.finalize(l2Dst, finalizationInfo); |
| 120 | + console.log('dstExecTxHash:', result.dstExecTxHash); |
| 121 | +} catch (e) { |
| 122 | + console.error('Interop failed:', e); |
| 123 | +} |
| 124 | +// ANCHOR_END: try-catch-create |
| 125 | + |
| 126 | +// ANCHOR: tryCreate |
| 127 | +const createResult = await sdk.interop.tryCreate(l2Dst, params); |
| 128 | +if (!createResult.ok) { |
| 129 | + console.error('Create failed:', createResult.error); |
| 130 | + return; |
| 131 | +} |
| 132 | +const handle = createResult.value; |
| 133 | +// ANCHOR_END: tryCreate |
0 commit comments