@@ -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 - THE TAKER BEING PASSED IN IS INCORRECT AND SO NATIVE IS 0
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 (
0 commit comments