flashloan-profit-calculator
Version:
A library for analyzing flashloan transactions and calculating profits
70 lines (69 loc) • 3.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.processTraceCall = processTraceCall;
const addresses_1 = require("../constants/addresses");
const transfer_1 = require("./transfer");
const weth_1 = require("../utils/weth");
const decoding_1 = require("../utils/decoding");
/**
* Process trace calls recursively to catch ETH transfers
*/
async function processTraceCall(calls) {
try {
if (!calls || calls.length === 0) {
console.log("No trace calls to process");
return;
}
for (const call of calls) {
try {
// Handle ETH transfers
if (call.value && BigInt(call.value) > 0n) {
console.log(`Processing ETH transfer: ${call.from} -> ${call.to}, value: ${call.value}`);
(0, transfer_1.processTransfer)(addresses_1.ETH_ADDRESS, call.from.toLowerCase(), call.to.toLowerCase(), BigInt(call.value));
}
// Handle WETH-like token operations
if (call.input) {
const isWeth = await (0, weth_1.isWethLikeToken)(call.to);
if (isWeth) {
const functionSignature = call.input.slice(0, 10);
// Handle deposit
if (functionSignature === "0xd0e30db0") {
console.log(`Processing WETH deposit: ${call.from} -> ${call.to}, value: ${call.value}`);
if (call.value) {
(0, transfer_1.processTransfer)(addresses_1.ETH_ADDRESS, call.from.toLowerCase(), call.to.toLowerCase(), BigInt(call.value));
}
}
// Handle withdraw
if (functionSignature === "0x2e1a7d4d") {
const amount = (0, decoding_1.decodeWithdrawInput)(call.input);
console.log(`Processing WETH withdraw: ${call.to} -> ${call.from}, amount: ${amount}`);
(0, transfer_1.processTransfer)(call.to.toLowerCase(), call.from.toLowerCase(), addresses_1.ETH_ADDRESS, amount);
}
// Handle transfer
if (functionSignature === "0xa9059cbb") {
const [to, amount] = (0, decoding_1.decodeTransferInput)(call.input);
console.log(`Processing WETH transfer: ${call.from} -> ${to}, amount: ${amount}`);
(0, transfer_1.processTransfer)(call.to.toLowerCase(), call.from.toLowerCase(), to.toLowerCase(), amount);
}
// Handle transferFrom
if (functionSignature === "0x23b872dd") {
const [from, to, amount] = (0, decoding_1.decodeTransferFromInput)(call.input);
console.log(`Processing WETH transferFrom: ${from} -> ${to}, amount: ${amount}`);
(0, transfer_1.processTransfer)(call.to.toLowerCase(), from.toLowerCase(), to.toLowerCase(), amount);
}
}
}
// Process nested calls
if (call.calls) {
await processTraceCall(call.calls);
}
}
catch (error) {
console.error(`Error processing individual trace call: ${error}`);
}
}
}
catch (error) {
console.error(`Error processing trace calls: ${error}`);
}
}