|
| 1 | +import { describe, it, expect } from "vitest" |
| 2 | +import { encodeFunctionData, type PublicClient, type Hex } from "viem" |
| 3 | +import { getContractCallInputs, getContractCallInput, type HexString } from "@/utils" |
| 4 | +import { ABI as IntentGatewayV2ABI } from "@/abis/IntentGatewayV2" |
| 5 | +import { EvmChain } from "@/chains/evm" |
| 6 | + |
| 7 | +const GATEWAY = "0x000000000000000000000000000000000000ca11" |
| 8 | +const OTHER = "0x0000000000000000000000000000000000000bee" |
| 9 | +const TX_HASH = ("0x" + "ab".repeat(32)) as HexString |
| 10 | + |
| 11 | +const GRAFFITI = ("0x" + "00".repeat(32)) as Hex |
| 12 | +const USER = ("0x" + "11".repeat(32)) as Hex |
| 13 | +const TOKEN_IN = ("0x" + "aa".repeat(32)) as Hex |
| 14 | +const TOKEN_OUT = ("0x" + "bb".repeat(32)) as Hex |
| 15 | +const BENEFICIARY = ("0x" + "cc".repeat(32)) as Hex |
| 16 | + |
| 17 | +function makeOrderStruct(nonce: bigint) { |
| 18 | + return { |
| 19 | + user: USER, |
| 20 | + source: "0x45564d2d31" as Hex, // "EVM-1" |
| 21 | + destination: "0x45564d2d3130" as Hex, // "EVM-10" |
| 22 | + deadline: 1700000000n, |
| 23 | + nonce, |
| 24 | + fees: 0n, |
| 25 | + session: "0x0000000000000000000000000000000000000000" as Hex, |
| 26 | + predispatch: { assets: [], call: "0x" as Hex }, |
| 27 | + inputs: [{ token: TOKEN_IN, amount: 100n }], |
| 28 | + output: { |
| 29 | + beneficiary: BENEFICIARY, |
| 30 | + assets: [{ token: TOKEN_OUT, amount: 100n }], |
| 31 | + call: "0x" as Hex, |
| 32 | + }, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +function placeOrderCalldata(nonce: bigint): HexString { |
| 37 | + return encodeFunctionData({ |
| 38 | + abi: IntentGatewayV2ABI, |
| 39 | + functionName: "placeOrder", |
| 40 | + args: [makeOrderStruct(nonce) as any, GRAFFITI], |
| 41 | + }) as HexString |
| 42 | +} |
| 43 | + |
| 44 | +/** |
| 45 | + * Builds a stub PublicClient.extend / debug_traceTransaction pipeline that |
| 46 | + * surfaces a fixed CallTracerCall fixture to the code under test. |
| 47 | + */ |
| 48 | +function stubClient(trace: any): PublicClient { |
| 49 | + const request = async ({ method }: { method: string }) => { |
| 50 | + if (method === "debug_traceTransaction") return trace |
| 51 | + throw new Error(`unexpected method: ${method}`) |
| 52 | + } |
| 53 | + const client: any = { |
| 54 | + request, |
| 55 | + extend(decorator: (c: any) => any) { |
| 56 | + return { ...client, ...decorator(client) } |
| 57 | + }, |
| 58 | + } |
| 59 | + return client as PublicClient |
| 60 | +} |
| 61 | + |
| 62 | +describe("getContractCallInputs", () => { |
| 63 | + it("returns every call to the target in execution order", async () => { |
| 64 | + const trace = { |
| 65 | + from: "0xabc", |
| 66 | + to: GATEWAY, |
| 67 | + input: "0xtop", |
| 68 | + calls: [ |
| 69 | + { from: GATEWAY, to: OTHER, input: "0x01", calls: [] }, |
| 70 | + { from: "0xabc", to: GATEWAY, input: "0x02", calls: [] }, |
| 71 | + { |
| 72 | + from: "0xabc", |
| 73 | + to: OTHER, |
| 74 | + input: "0x03", |
| 75 | + calls: [{ from: OTHER, to: GATEWAY, input: "0x04", calls: [] }], |
| 76 | + }, |
| 77 | + ], |
| 78 | + } |
| 79 | + |
| 80 | + const inputs = await getContractCallInputs(stubClient(trace), TX_HASH, GATEWAY) |
| 81 | + |
| 82 | + expect(inputs).toEqual(["0xtop", "0x02", "0x04"]) |
| 83 | + }) |
| 84 | + |
| 85 | + it("returns an empty list when the target is not called", async () => { |
| 86 | + const trace = { |
| 87 | + from: "0xabc", |
| 88 | + to: OTHER, |
| 89 | + input: "0x00", |
| 90 | + calls: [{ from: "0xabc", to: OTHER, input: "0x01", calls: [] }], |
| 91 | + } |
| 92 | + |
| 93 | + const inputs = await getContractCallInputs(stubClient(trace), TX_HASH, GATEWAY) |
| 94 | + |
| 95 | + expect(inputs).toEqual([]) |
| 96 | + }) |
| 97 | + |
| 98 | + it("getContractCallInput (legacy wrapper) returns the first match", async () => { |
| 99 | + const trace = { |
| 100 | + from: "0xabc", |
| 101 | + to: OTHER, |
| 102 | + input: "0x00", |
| 103 | + calls: [ |
| 104 | + { from: "0xabc", to: GATEWAY, input: "0xfirst", calls: [] }, |
| 105 | + { from: "0xabc", to: GATEWAY, input: "0xsecond", calls: [] }, |
| 106 | + ], |
| 107 | + } |
| 108 | + |
| 109 | + const result = await getContractCallInput(stubClient(trace), TX_HASH, GATEWAY) |
| 110 | + |
| 111 | + expect(result).toBe("0xfirst") |
| 112 | + }) |
| 113 | +}) |
| 114 | + |
| 115 | +describe("EvmChain.getPlaceOrderCalldata", () => { |
| 116 | + function newChain(trace: any): EvmChain { |
| 117 | + const chain = Object.create(EvmChain.prototype) as EvmChain |
| 118 | + ;(chain as any).publicClient = stubClient(trace) |
| 119 | + return chain |
| 120 | + } |
| 121 | + |
| 122 | + it("returns the K-th placeOrder calldata in execution order", async () => { |
| 123 | + const order0 = placeOrderCalldata(1n) |
| 124 | + const order1 = placeOrderCalldata(2n) |
| 125 | + |
| 126 | + const trace = { |
| 127 | + from: "0xabc", |
| 128 | + to: "0xabc", |
| 129 | + input: "0x", |
| 130 | + calls: [ |
| 131 | + { from: "0xabc", to: GATEWAY, input: order0, calls: [] }, |
| 132 | + { from: "0xabc", to: OTHER, input: "0xnoise", calls: [] }, |
| 133 | + { from: "0xabc", to: GATEWAY, input: order1, calls: [] }, |
| 134 | + ], |
| 135 | + } |
| 136 | + |
| 137 | + const chain = newChain(trace) |
| 138 | + const first = await chain.getPlaceOrderCalldata(TX_HASH, GATEWAY, 0) |
| 139 | + const second = await chain.getPlaceOrderCalldata(TX_HASH, GATEWAY, 1) |
| 140 | + |
| 141 | + expect(first).toBe(order0) |
| 142 | + expect(second).toBe(order1) |
| 143 | + }) |
| 144 | + |
| 145 | + it("defaults occurrenceIndex to 0", async () => { |
| 146 | + const order0 = placeOrderCalldata(7n) |
| 147 | + const trace = { |
| 148 | + from: "0xabc", |
| 149 | + to: GATEWAY, |
| 150 | + input: order0, |
| 151 | + calls: [], |
| 152 | + } |
| 153 | + |
| 154 | + const chain = newChain(trace) |
| 155 | + const result = await chain.getPlaceOrderCalldata(TX_HASH, GATEWAY) |
| 156 | + expect(result).toBe(order0) |
| 157 | + }) |
| 158 | + |
| 159 | + it("throws when occurrenceIndex is out of range", async () => { |
| 160 | + const order0 = placeOrderCalldata(1n) |
| 161 | + const trace = { |
| 162 | + from: "0xabc", |
| 163 | + to: GATEWAY, |
| 164 | + input: order0, |
| 165 | + calls: [], |
| 166 | + } |
| 167 | + |
| 168 | + const chain = newChain(trace) |
| 169 | + await expect(chain.getPlaceOrderCalldata(TX_HASH, GATEWAY, 1)).rejects.toThrow(/out of range/) |
| 170 | + }) |
| 171 | + |
| 172 | + it("throws when the gateway is not called at all", async () => { |
| 173 | + const trace = { |
| 174 | + from: "0xabc", |
| 175 | + to: "0xabc", |
| 176 | + input: "0x", |
| 177 | + calls: [{ from: "0xabc", to: OTHER, input: "0xnoise", calls: [] }], |
| 178 | + } |
| 179 | + |
| 180 | + const chain = newChain(trace) |
| 181 | + await expect(chain.getPlaceOrderCalldata(TX_HASH, GATEWAY, 0)).rejects.toThrow(/Failed to extract placeOrder/) |
| 182 | + }) |
| 183 | +}) |
0 commit comments