Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: sum total amount leaving the taker's account #73

Merged
merged 1 commit into from
Jan 21, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -86,7 +86,13 @@ export async function parseSwap({
(log) => log.from.toLowerCase() === taker.toLowerCase()
);

let input = fromTaker.length ? fromTaker[0] : logs[0];
let input = fromTaker.length
? fromTaker.reduce((acc, curr) => ({
...acc,
amount: formatUnits(acc.amountRaw + curr.amountRaw, curr.decimals),
amountRaw: acc.amountRaw + curr.amountRaw,
}))
: logs[0];

let output =
nativeAmountToTaker === "0"
31 changes: 31 additions & 0 deletions src/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -550,6 +550,37 @@ test("parse a gasless swap on Base (DEGEN for USDC) for SettlerMetaTxn", async (
});
});

// https://basescan.org/tx/0x3d032fdd216315c3dce7bcafeac0805ec18d27c7c9fdf43836cab7fb61332a6d
test("parse a swap on Base (KEIRA for ETH) for Settler", async () => {
const publicClient = createPublicClient({
chain: base,
transport: http(
`https://base-mainnet.g.alchemy.com/v2/${process.env.ALCHEMY_API_KEY}`
),
}) as PublicClient<Transport, Chain>;

const transactionHash =
"0x3d032fdd216315c3dce7bcafeac0805ec18d27c7c9fdf43836cab7fb61332a6d";

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

expect(result).toEqual({
tokenIn: {
symbol: "KEIRA",
amount: "27538.512122127777968652",
address: "0x710eEc215b3bB653d42fC6e70E0531eA13F51A7A",
},
tokenOut: {
symbol: "ETH",
amount: "0.035509880980229712",
address: "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE",
},
});
});

// https://arbiscan.io/tx/0xb2c05194e4ec9ae0f82098ec82a606df544e87c8d6b7726bbb4b1dcc023cb9d7
test("parse a gasless swap on on Arbitrum (ARB for ETH)", async () => {
const publicClient = createPublicClient({
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@ export interface EnrichedLog {
amount: string;
address: Address;
decimals: number;
amountRaw: bigint;
}

export interface Trace {
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -107,12 +107,13 @@ export async function transferLogs({
const decimals = results[midpoint + index].result as number;
const amount =
log.data === "0x" ? "0" : formatUnits(BigInt(log.data), decimals);
const amountRaw = log.data === "0x" ? 0n : BigInt(log.data);
const { address, topics } = log;
const { 1: fromHex, 2: toHex } = topics;
const from = getAddress(convertHexToAddress(fromHex));
const to = getAddress(convertHexToAddress(toHex));

return { to, from, symbol, amount, address, decimals };
return { to, from, symbol, amount, amountRaw, address, decimals };
})
.filter((log) => log.amount !== "0");