|
| 1 | +// tests/bridge.e2e.test.ts |
| 2 | +import { beforeAll, describe, expect, it } from "vitest"; |
| 3 | +import { execa } from "execa"; |
| 4 | +import { JsonRpcProvider } from "ethers"; |
| 5 | +import { Provider, Wallet } from "zksync-ethers"; |
| 6 | + |
| 7 | +const PK = process.env.TEST_PK ?? "0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80"; |
| 8 | +const L1_RPC = process.env.L1_RPC ?? "http://127.0.0.1:8012"; |
| 9 | +const L2_RPC = process.env.L2_RPC ?? "http://127.0.0.1:8011"; |
| 10 | +const CHAIN = process.env.CHAIN ?? "in-memory-node"; |
| 11 | + |
| 12 | +const DEPOSIT_ETH = "1"; |
| 13 | +const WITHDRAW_ETH = "0.001"; |
| 14 | + |
| 15 | +if (!PK) throw new Error("TEST_PK is not set – provide a funded private key"); |
| 16 | + |
| 17 | +// ──────────────────────────────────────────────────────────── |
| 18 | +// Helpers |
| 19 | +// ──────────────────────────────────────────────────────────── |
| 20 | +async function cli(args: string[], env: Record<string, string> = {}) { |
| 21 | + const { stdout } = await execa("node", ["bin/index.js", ...args], { env }); |
| 22 | + return stdout; |
| 23 | +} |
| 24 | + |
| 25 | +function extractHash(out: string) { |
| 26 | + const m = out.match(/Transaction hash:\s+(0x[a-f0-9]{64})/i); |
| 27 | + if (!m) throw new Error("Cannot find tx hash in CLI output\n" + out); |
| 28 | + return m[1]; |
| 29 | +} |
| 30 | + |
| 31 | +// ──────────────────────────────────────────────────────────── |
| 32 | +// Tests |
| 33 | +// ──────────────────────────────────────────────────────────── |
| 34 | +describe("bridge CLI", () => { |
| 35 | + let wallet: Wallet; |
| 36 | + let l1Provider: JsonRpcProvider; |
| 37 | + let l2Provider: Provider; |
| 38 | + |
| 39 | + let depositHash: string | undefined; |
| 40 | + let withdrawHash: string | undefined; |
| 41 | + |
| 42 | + beforeAll(() => { |
| 43 | + l1Provider = new JsonRpcProvider(L1_RPC); |
| 44 | + l2Provider = new Provider(L2_RPC); |
| 45 | + // eslint-disable-next-line @typescript-eslint/no-explicit-any |
| 46 | + wallet = new Wallet(PK, l2Provider, l1Provider as any); |
| 47 | + }); |
| 48 | + |
| 49 | + // ────────────────────────────────────────────────────────── |
| 50 | + // Deposit |
| 51 | + // ────────────────────────────────────────────────────────── |
| 52 | + it("sends a deposit L1 ➜ L2", { timeout: 90_000 }, async () => { |
| 53 | + const out = await cli([ |
| 54 | + "bridge", |
| 55 | + "deposit", |
| 56 | + "--chain", |
| 57 | + CHAIN, |
| 58 | + "--amount", |
| 59 | + DEPOSIT_ETH, |
| 60 | + "--private-key", |
| 61 | + PK, |
| 62 | + "--recipient", |
| 63 | + wallet.address, |
| 64 | + "--l1-rpc", |
| 65 | + L1_RPC, |
| 66 | + "--rpc", |
| 67 | + L2_RPC, |
| 68 | + ]); |
| 69 | + |
| 70 | + expect(out).toMatch(/Deposit sent:/); |
| 71 | + depositHash = extractHash(out); |
| 72 | + expect(depositHash).toMatch(/^0x[a-f0-9]{64}$/); |
| 73 | + }); |
| 74 | + |
| 75 | + // ────────────────────────────────────────────────────────── |
| 76 | + // Withdraw |
| 77 | + // ────────────────────────────────────────────────────────── |
| 78 | + it("sends a withdrawal L2 ➜ L1", { timeout: 90_000 }, async () => { |
| 79 | + const out = await cli([ |
| 80 | + "bridge", |
| 81 | + "withdraw", |
| 82 | + "--chain", |
| 83 | + CHAIN, |
| 84 | + "--amount", |
| 85 | + WITHDRAW_ETH, |
| 86 | + "--private-key", |
| 87 | + PK, |
| 88 | + "--recipient", |
| 89 | + wallet.address, |
| 90 | + "--l1-rpc", |
| 91 | + L1_RPC, |
| 92 | + "--rpc", |
| 93 | + L2_RPC, |
| 94 | + ]); |
| 95 | + |
| 96 | + expect(out).toMatch(/Withdraw sent:/); |
| 97 | + withdrawHash = extractHash(out); |
| 98 | + expect(withdrawHash).toMatch(/^0x[a-f0-9]{64}$/); |
| 99 | + }); |
| 100 | + |
| 101 | + // ────────────────────────────────────────────────────────── |
| 102 | + // Finalize |
| 103 | + // ────────────────────────────────────────────────────────── |
| 104 | + // TODO: need to figure out how to wait for the withdrawal to be ready for finalization |
| 105 | +}); |
0 commit comments