flashloan-profit-calculator
v1.0.0
Published
A library for analyzing flashloan transactions and calculating profits
Maintainers
Readme
Flashloan Profit Calculator
This tool analyzes Ethereum transaction logs to calculate the profit made from flashloan arbitrage or other DeFi operations.
Features
- Calculate profit directly from a transaction hash
- Analyze token transfers (ERC20) from logs
- Track ETH transfers from trace data
- Fetch token metadata for proper display
- CLI interface for easy usage
Project Structure
The codebase is organized in a modular way:
tokenTypes.ts- Type definitions and constantstransferProcessor.ts- Process transfers and update balancesprofitCalculator.ts- Calculate profit from balance changesindex.ts- Main API entry pointscli.ts- Command-line interface
Setup
- Clone this repository
- Install dependencies:
npm install - Create a
.envfile with:ALCHEMY_API_KEY=your_alchemy_api_key
Usage
Command Line Interface
Analyze a transaction:
npx ts-node cli.ts 0x123...abcWhere 0x123...abc is the transaction hash you want to analyze.
Programmatic Usage
Import the library:
import { calculateProfitFromTxHash, displayProfitResults } from './index';
async function analyzeTx(txHash: string) {
try {
// Calculate profit
const profit = await calculateProfitFromTxHash(txHash);
// Display results
await displayProfitResults(profit);
// Or use the raw profit data
console.log('Raw profit data:', profit);
} catch (error) {
console.error('Error:', error);
}
}
// Analyze a transaction
analyzeTx('0x123...abc');How It Works
- Fetches transaction details using Alchemy API
- Extracts logs and traces from the transaction
- Processes transfer logs to track token movements
- Analyzes ETH transfers from trace data
- Calculates profit by summing revenue and costs
- Displays results with proper token information
Requirements
- Node.js v14+
- TypeScript
- Alchemy API key
