Skip to content

Commit

Permalink
fix: sum total amount leaving the taker's account
Browse files Browse the repository at this point in the history
Some trades involve a sell tax, which needs to be accounted for. By including
both the output amount and the sell tax, we can calculate the total amount
leaving the user's wallet. Otherwise, only the tax is shown as the amount
leaving the wallet.

resolves #72
  • Loading branch information
hzhu committed Jan 21, 2025
1 parent 5de0021 commit d64f1e2
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
8 changes: 7 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
31 changes: 31 additions & 0 deletions src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export interface EnrichedLog {
amount: string;
address: Address;
decimals: number;
amountRaw: bigint;
}

export interface Trace {
Expand Down
3 changes: 2 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand Down

0 comments on commit d64f1e2

Please sign in to comment.