UNPKG

flashloan-profit-calculator

Version:

A library for analyzing flashloan transactions and calculating profits

50 lines (49 loc) 2.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.calculateProfitByToken = calculateProfitByToken; const transfer_1 = require("./processors/transfer"); const addresses_1 = require("./constants/addresses"); /** * Calculate the total profit for each token */ function calculateProfitByToken() { const totalProfit = []; for (const token in transfer_1.revenueBalanceChanges) { let index = 0; // Calculate revenue const revenue = Object.values(transfer_1.revenueBalanceChanges[token]).reduce((sum, balance) => { if (balance.type === "Revenue" || balance.type === "Cost") { index++; return sum + balance.amount; } const address = Object.keys(transfer_1.revenueBalanceChanges[token])[index]; // get totalAddressParticipation let totalParticipation = 0; let totalToCount = 0; let totalFromCount = 0; for (const token in transfer_1.addressParticipation[address]) { totalParticipation += transfer_1.addressParticipation[address][token].participation; totalToCount += transfer_1.addressParticipation[address][token].toCount; totalFromCount += transfer_1.addressParticipation[address][token].fromCount; } // The address is a profit taker if it receives a token transfer without sending any tokens // i.e. It is not a swap if (totalParticipation % 2 === 1 && totalToCount !== totalFromCount && !transfer_1.revenueBalanceChanges[address] && address !== addresses_1.NULL_ADDRESS) { console.log(`${address} is a profit taker`); index++; return sum + balance.amount; } index++; return sum; }, 0n); // Calculate cost const cost = Object.values(transfer_1.costBalanceChanges[token]).reduce((sum, balance) => (balance.type === "Cost" ? sum + balance.amount : sum), 0n); const profit = revenue + cost; totalProfit.push({ token, profit }); } return totalProfit; }