Skip to content

Commit

Permalink
fix: return null for smart contract wallet reverts
Browse files Browse the repository at this point in the history
  • Loading branch information
hzhu committed Aug 19, 2024
1 parent ad9fa18 commit bcd9730
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 33 deletions.
20 changes: 20 additions & 0 deletions src/tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -899,3 +899,23 @@ test("parse a swap on BNB Chain (USDT for BNB) with smart contract wallet", asyn
},
});
});

// https://basescan.org/tx/0x47b4e55bfaee712775a2181f0219db02647d1a2f97eeac6b4f6f2465aac64d86
test("gracefully handles a revert in a erc-4337 transaction", 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 = `0x47b4e55bfaee712775a2181f0219db02647d1a2f97eeac6b4f6f2465aac64d86`;

const result = await parseSwap({
publicClient,
transactionHash,
smartContractWallet: "0x3F6dAB60Cc16377Df9684959e20962f44De20988",
});

expect(result).toEqual(null);
});
82 changes: 49 additions & 33 deletions src/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,46 +125,62 @@ export function parseSmartContractWalletTx({
});

if (!output && nativeAmountToTaker !== "0") {
return {
tokenIn: {
address: input?.address,
amount: input?.amount,
symbol: input?.symbol,
},
tokenOut: {
address: NATIVE_TOKEN_ADDRESS,
amount: nativeAmountToTaker,
symbol: NATIVE_SYMBOL_BY_CHAIN_ID[chainId],
},
};
} else if (!input && nativeAmountFromTaker !== "0") {
if (input) {
return {
tokenIn: {
address: input.address,
amount: input.amount,
symbol: input.symbol,
},
tokenOut: {
address: NATIVE_TOKEN_ADDRESS,
amount: nativeAmountToTaker,
symbol: NATIVE_SYMBOL_BY_CHAIN_ID[chainId],
},
};
} else {
return null;
}
}

if (!input && nativeAmountFromTaker !== "0") {
const wrappedNativeAsset =
chainId === 56 ? "WBNB" : chainId === 137 ? "WMATIC" : "WETH";
const inputLog = logs.filter((log) => log.symbol === wrappedNativeAsset)[0];
return {
tokenIn: {
address: NATIVE_TOKEN_ADDRESS,
amount: inputLog?.amount,
symbol: NATIVE_SYMBOL_BY_CHAIN_ID[chainId],
},
tokenOut: {
address: output?.address,
amount: output?.amount,
symbol: output?.symbol,
},
};
} else {

const inputLog = logs.find((log) => log.symbol === wrappedNativeAsset);

if (inputLog) {
return {
tokenIn: {
address: NATIVE_TOKEN_ADDRESS,
amount: inputLog.amount,
symbol: NATIVE_SYMBOL_BY_CHAIN_ID[chainId],
},
tokenOut: {
address: output?.address,
amount: output?.amount,
symbol: output?.symbol,
},
};
} else {
return null;
}
}

if (input && output) {
return {
tokenIn: {
address: input?.address,
amount: input?.amount,
symbol: input?.symbol,
address: input.address,
amount: input.amount,
symbol: input.symbol,
},
tokenOut: {
address: output?.address,
amount: output?.amount,
symbol: output?.symbol,
address: output.address,
amount: output.amount,
symbol: output.symbol,
},
};
}

return null;
}

0 comments on commit bcd9730

Please sign in to comment.