From fb218e5e50c59ac66eef36d33167fb1c772d5211 Mon Sep 17 00:00:00 2001 From: hzhu Date: Tue, 16 Jul 2024 20:27:51 -0700 Subject: [PATCH] fix: filter from logs from taker for initial input --- src/parseSwapV2.ts | 6 +++++- src/tests/parseSwapV2.test.ts | 31 +++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/parseSwapV2.ts b/src/parseSwapV2.ts index de59527..9519cf4 100644 --- a/src/parseSwapV2.ts +++ b/src/parseSwapV2.ts @@ -247,7 +247,11 @@ export async function parseSwapV2({ transactionReceipt, }); - let input = logs[0]; + const fromTaker = logs.filter( + (log) => log.from.toLowerCase() === taker.toLowerCase() + ); + + let input = fromTaker.length ? fromTaker[0] : logs[0]; let output = nativeTransferAmount === "0" diff --git a/src/tests/parseSwapV2.test.ts b/src/tests/parseSwapV2.test.ts index af87505..cc67317 100644 --- a/src/tests/parseSwapV2.test.ts +++ b/src/tests/parseSwapV2.test.ts @@ -538,6 +538,37 @@ test("parse a gasless swap on Optimism (USDC for OP) for executeMetaTxn", async }); }); +// https://optimistic.etherscan.io/tx/0x9abc33ffc67ff9348af9a29f109c4f27b36ee5bc80dd81ae3814e69309449a61 +test("parse a gasless swap on Optimism (USDC for OP) for execute", async () => { + const publicClient = createPublicClient({ + chain: optimism, + transport: http( + `https://opt-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}` + ), + }) as PublicClient; + + const transactionHash = + "0x9abc33ffc67ff9348af9a29f109c4f27b36ee5bc80dd81ae3814e69309449a61"; + + const result = await parseSwapV2({ + publicClient, + transactionHash, + }); + + expect(result).toEqual({ + tokenIn: { + symbol: "USDC", + amount: "5", + address: "0x7F5c764cBc14f9669B88837ca1490cCa17c31607", + }, + tokenOut: { + symbol: "OP", + amount: "2.73214427938472425", + address: "0x4200000000000000000000000000000000000042", + }, + }); +}); + // https://bscscan.com/tx/0xdda12da1e32c3320082355c985d6f2c6559169989de51e3cc83123395516c057 test("parse a swap on BNB Chain (ETH for USDC) for execute", async () => { const publicClient = createPublicClient({