Skip to content

Commit

Permalink
fix: use logs if taker received found in logs
Browse files Browse the repository at this point in the history
  • Loading branch information
hzhu committed Jul 16, 2024
1 parent e019a40 commit ef27691
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 4 deletions.
22 changes: 19 additions & 3 deletions src/parseSwapV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ function extractNativeTransfer(trace: Trace, recipient: Address) {
function traverseCalls(calls: Trace[]) {
calls.forEach((call) => {
if (
call.to.toLowerCase() === recipient &&
call.to.toLowerCase() === recipient.toLowerCase() &&
fromHex(call.value, "bigint") > 0n
) {
totalTransferred = totalTransferred + fromHex(call.value, "bigint");
Expand Down Expand Up @@ -299,12 +299,28 @@ export async function parseSwapV2({
data: transaction.input,
});

const taker = args[0].recipient.toLowerCase() as Address;
const { 3: msgSender } = args;

const nativeTransferAmount = extractNativeTransfer(trace, taker);
const nativeTransferAmount = extractNativeTransfer(trace, msgSender);

if (nativeTransferAmount === "0") {
output = logs[logs.length - 1];
const takerReceived = logs.filter(
(log) => log.to.toLowerCase() === msgSender.toLowerCase()
);
if (takerReceived.length === 1) {
output = {
symbol: takerReceived[0].symbol,
amount: takerReceived[0].amount,
address: takerReceived[0].address,
};
} else {
// Unknown if this case actually happens. If it does, please file a bug report here: https://github.com/0xProject/0x-parser/issues/new/choose".
output = { symbol: "", amount: "", address: "" };
console.error(
"More than one `takerReceived` log. File a bug report here: https://github.com/0xProject/0x-parser/issues/new/choose"
);
}
} else {
output = {
symbol: NATIVE_SYMBOL_BY_CHAIN_ID[chainId],
Expand Down
64 changes: 63 additions & 1 deletion src/tests/parseSwapV2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
type Transport,
type Chain,
} from "viem";
import { arbitrum, base, mainnet, polygon } from "viem/chains";
import { arbitrum, base, mainnet, optimism, polygon } from "viem/chains";
import { test, expect } from "vitest";
import { parseSwapV2 } from "../index";
import { NATIVE_ASSET } from "../constants";
Expand Down Expand Up @@ -506,3 +506,65 @@ test("parse a gasless swap on on Arbitrum (ARB for ETH)", async () => {
},
});
});

// https://optimistic.etherscan.io/tx/0xdfd5180c9f84d8f7381f48550dd14df86dd704489c251a10a67bd3cfdb0ae626
test("parse a gasless swap on Optimism (USDC for OP) for executeMetaTxn", async () => {
const publicClient = createPublicClient({
chain: optimism,
transport: http(
`https://opt-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`
),
}) as PublicClient<Transport, Chain>;

const transactionHash =
"0xdfd5180c9f84d8f7381f48550dd14df86dd704489c251a10a67bd3cfdb0ae626";

const result = await parseSwapV2({
publicClient,
transactionHash,
});

expect(result).toEqual({
tokenIn: {
symbol: "USDC",
amount: "5",
address: "0x7F5c764cBc14f9669B88837ca1490cCa17c31607",
},
tokenOut: {
symbol: "OP",
amount: "2.744026666231502743",
address: "0x4200000000000000000000000000000000000042",
},
});
});

// https://bscscan.com/tx/0xdda12da1e32c3320082355c985d6f2c6559169989de51e3cc83123395516c057
test("parse a swap on BNB Chain (ETH for USDC) for execute", async () => {
const publicClient = createPublicClient({
chain: optimism,
transport: http(
`https://bnb-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`
),
}) as PublicClient<Transport, Chain>;

const transactionHash =
"0xdda12da1e32c3320082355c985d6f2c6559169989de51e3cc83123395516c057";

const result = await parseSwapV2({
publicClient,
transactionHash,
});

expect(result).toEqual({
tokenIn: {
symbol: "ETH",
amount: "0.728252933682622857",
address: "0x2170Ed0880ac9A755fd29B2688956BD959F933F8",
},
tokenOut: {
symbol: "USDC",
amount: "2260.511889276471849176",
address: "0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d",
},
});
});

0 comments on commit ef27691

Please sign in to comment.