@swapper-finance/sdk
Version:
JavaScript SDK form Swapper
42 lines (38 loc) • 1.5 kB
text/typescript
import { ContractReceipt, ethers } from "ethers"; // Import necessary types from ethers
const getTokenAmountOut = async (
contractReceipt: ContractReceipt, // The receipt after transaction
tokenAddress: string, // The address of the token being transferred
receiverAddress: string, // The address that receives the tokens
): Promise<string> => {
// Loop through the logs to find the correct token transfer event
for (const log of contractReceipt.logs) {
// Check if this is a Transfer event
const topics = log.topics || [];
const isCallsExecutedEvent =
topics[0] ===
ethers.utils.id("CallsExecuted(address,address,uint256,address,uint256)");
if (isCallsExecutedEvent) {
try {
const tokenReceiver = ethers.utils.getAddress(
"0x" + topics[1]?.substring(26),
);
const tokenOut = ethers.utils.defaultAbiCoder
.decode(["address", "uint256", "address", "uint256"], log.data)[2]
.toString();
if (
tokenReceiver.toLowerCase() === receiverAddress.toLowerCase() &&
tokenOut.toLowerCase() === tokenAddress.toLowerCase()
) {
const tokenOutReceived = ethers.utils.defaultAbiCoder
.decode(["address", "uint256", "address", "uint256"], log.data)[3]
.toString();
return tokenOutReceived;
}
} catch (err) {
console.error("Error parsing log:", err);
}
}
}
return "0";
};
export default getTokenAmountOut;