Skip to content

Commit 26a378b

Browse files
add debug comments for SettlerIntent txns
1 parent 42658e8 commit 26a378b

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

src/index.ts

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,28 +37,40 @@ export async function parseSwap({
3737
if (!isChainIdSupported(chainId)) {
3838
throw new Error(`chainId ${chainId} is unsupported…`);
3939
}
40-
40+
// This code extends the viem publicClient with a custom method called traceCall.
4141
const client = publicClient.extend((client) => ({
4242
async traceCall(args: { hash: Hash }) {
4343
return client.request<TraceTransactionSchema>({
44-
method: "debug_traceTransaction",
44+
method: "debug_traceTransaction", //replays the txn & returns execution data
45+
/**
46+
* The callTracer returns a tree of all internal calls made during execution, including:
47+
Contract-to-contract calls
48+
Internal ETH transfers
49+
Call depth, gas used, return values
50+
*/
4551
params: [args.hash, { tracer: "callTracer" }],
52+
//
4653
});
4754
},
4855
}));
4956

57+
// trace - ETH transfers, contract calls
58+
// transaction- from, to, value, input calldata
59+
// transactionReceipt - status, gas used, logs emitted
5060
const [trace, transaction, transactionReceipt] = await Promise.all([
5161
client.traceCall({ hash }),
5262
publicClient.getTransaction({ hash }),
5363
publicClient.getTransactionReceipt({ hash }),
5464
]);
5565

5666
const { from: taker, value, to } = transaction;
57-
58-
const isToERC4337 = to === ERC_4337_ENTRY_POINT.toLowerCase();
59-
67+
// The Issue: FOR SETTLERINTENT TXNS only - The taker address passed into calculateNativeTransfer is not the actual takers address, causing nativeAmountToTaker to be 0 when it shouldn't be
68+
// Potential Solution: we need to determine if the txn is a settlerIntent txn, if so we
69+
// can determine the taker from the to address in the last trace call.
70+
const actualTaker = trace.calls[trace.calls.length-1].to;
71+
//Scans the execution trace for any internal ETH transfers to the taker's address
6072
const nativeAmountToTaker = calculateNativeTransfer(trace, {
61-
recipient: taker,
73+
recipient: taker, // for Settler Intent txns we should use actualTaker
6274
});
6375

6476
if (transactionReceipt.status === "reverted") {
@@ -74,7 +86,9 @@ export async function parseSwap({
7486
publicClient,
7587
transactionReceipt,
7688
});
77-
89+
// if a Smart wallet, then the way in which we determine the taker is different than traditional EOA's.
90+
// The actual taker is the smart contract embedded in the wallet
91+
const isToERC4337 = to === ERC_4337_ENTRY_POINT.toLowerCase();
7892
if (isToERC4337) {
7993
if (!smartContractWallet) {
8094
throw new Error(

src/tests/index.test.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,15 +1503,15 @@ test("parse a swap on Optimism (wstETH for ETH) via Balancer pool", async () =>
15031503
address: "0x1F32b1c2345538c0c6f582fCB022739c4A194Ebb",
15041504
},
15051505
tokenOut: {
1506-
symbol: "WETH",
1507-
amount: "0.010698199301849867",
1508-
address: "0x4200000000000000000000000000000000000006",
1506+
symbol: "ETH",
1507+
amount: "0.010671015314389981",
1508+
address: NATIVE_TOKEN_ADDRESS,
15091509
},
15101510
});
15111511
});
15121512

15131513
// https://etherscan.io/tx/0x967cb342227a2541a845058862e70833d638bf5bb7ce229c6506466dbb43a004
1514-
test.only("parse a swap on Mainnet (TRG for SHITCOIN)", async () => {
1514+
test("parse a swap on Mainnet (TRG for SHITCOIN)", async () => {
15151515
const transactionHash =
15161516
"0x967cb342227a2541a845058862e70833d638bf5bb7ce229c6506466dbb43a004" as `0x${string}`;
15171517

0 commit comments

Comments
 (0)