Skip to content

A powerful TypeScript library for detecting and analyzing PancakeSwap transactions on EVM-compatible blockchains (Binance Smart Chain, Ethereum, etc.). This library automatically decodes swap transactions and extracts detailed information including tokens involved, amounts, and contract addresses.

License

Notifications You must be signed in to change notification settings

crypmancer/evm-transaction-decoder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

2 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

EVM Transaction Decoder

A powerful TypeScript library for detecting and analyzing PancakeSwap transactions on EVM-compatible blockchains (Binance Smart Chain, Ethereum, etc.). This library automatically decodes swap transactions and extracts detailed information including tokens involved, amounts, and contract addresses.

Features

  • πŸ” Automatic Detection: Detects PancakeSwap V2 and V3 router interactions
  • πŸ“Š Detailed Decoding: Extracts token addresses, amounts, and human-readable symbols
  • πŸš€ Easy to Use: Simple API with just a few lines of code
  • ⚑ Flexible: Works with any EVM provider (Infura, Alchemy, QuickNode, etc.)
  • 🎯 TypeScript: Fully typed with comprehensive type definitions
  • πŸ”§ Configurable: Support for custom token mappings and debug mode

Installation

npm install evm-transaction-decoder

or

yarn add evm-transaction-decoder

Quick Start

import { ethers } from 'ethers';
import { PancakeSwapDetector } from 'evm-transaction-decoder';

// Initialize provider
const provider = new ethers.JsonRpcProvider('YOUR_RPC_URL');

// Create detector
const detector = new PancakeSwapDetector(provider);

// Fetch transaction
const txHash = '0x...';
const tx = await provider.getTransaction(txHash);
const receipt = await provider.getTransactionReceipt(txHash);

// Detect swap
const swapDetails = await detector.detectPancakeSwap(tx!, receipt!);

if (swapDetails) {
  console.log('Token In:', swapDetails.tokenInSymbol);
  console.log('Token Out:', swapDetails.tokenOutSymbol);
  console.log('Amount In:', swapDetails.amountIn);
  console.log('Amount Out:', swapDetails.amountOut);
}

API Reference

PancakeSwapDetector

Main class for detecting and decoding PancakeSwap transactions.

Constructor

constructor(provider: ethers.Provider, config?: DecoderConfig)

Parameters:

  • provider: An ethers.js provider instance (JsonRpcProvider, InfuraProvider, etc.)
  • config: Optional configuration object

Config Options:

  • customTokens: Record of token address to symbol mappings
  • debug: Enable debug logging (default: false)

Methods

detectPancakeSwap(tx, receipt)

Detects and decodes a PancakeSwap transaction.

Parameters:

  • tx: Transaction response from provider
  • receipt: Transaction receipt

Returns: Promise<SwapDetails | null>

SwapDetails

Interface containing decoded swap information.

interface SwapDetails {
  txHash: string;              // Transaction hash
  routerAddress: string;       // Router contract address
  fromAddress: string;         // Address that initiated the swap
  tokenIn: string;             // Input token address
  tokenOut: string;            // Output token address
  amountIn: string;            // Input amount (in token's smallest unit)
  amountOut: string;           // Output amount (in token's smallest unit)
  tokenInSymbol: string;       // Human-readable input token symbol
  tokenOutSymbol: string;      // Human-readable output token symbol
}

Supported Functions

The library currently supports decoding for the following PancakeSwap router functions:

V3 Functions:

  • exactInputSingle
  • exactInput
  • exactOutputSingle
  • exactOutput

V2 Functions:

  • swapExactTokensForTokens
  • swapTokensForExactTokens
  • swapExactETHForTokens
  • swapTokensForExactETH
  • swapExactTokensForETH
  • swapETHForExactTokens

Configuration

Custom Token Symbols

You can provide custom token symbol mappings:

const detector = new PancakeSwapDetector(provider, {
  customTokens: {
    '0xYourTokenAddress': 'CUSTOM',
    '0xAnotherAddress': 'ANOTHER',
  },
  debug: true
});

Supported Blockchains

  • Binance Smart Chain (BSC)
  • Ethereum Mainnet
  • Polygon
  • Avalanche
  • Optimism
  • Arbitrum
  • Any other EVM-compatible chain

Examples

Basic Usage

import { ethers } from 'ethers';
import { PancakeSwapDetector } from 'evm-transaction-decoder';

async function analyzeTransaction(txHash: string) {
  const provider = new ethers.JsonRpcProvider('https://bsc-dataseed1.binance.org/');
  const detector = new PancakeSwapDetector(provider);

  const tx = await provider.getTransaction(txHash);
  const receipt = await provider.getTransactionReceipt(txHash);

  const details = await detector.detectPancakeSwap(tx!, receipt!);

  if (details) {
    console.log(`Swapped ${details.tokenInSymbol} for ${details.tokenOutSymbol}`);
    console.log(`Amount: ${ethers.formatUnits(details.amountIn, 18)} β†’ ${ethers.formatUnits(details.amountOut, 18)}`);
  }
}

With Custom Configuration

const detector = new PancakeSwapDetector(provider, {
  customTokens: {
    '0xYourTokenAddress': 'MYTOKEN',
  },
  debug: true
});

Development

Building

npm run build

Development Mode

npm run dev

Running Examples

npm run example

Requirements

  • Node.js >= 18.0.0
  • ethers.js >= 6.0.0

License

MIT

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Disclaimer

This library is provided for informational purposes only. Always verify the accuracy of decoded data independently before making any financial decisions.

Support

For issues, questions, or suggestions, please open an issue on the GitHub repository.

Telegram - Crypmancer


Made with ❀️ for the Ethereum ecosystem

About

A powerful TypeScript library for detecting and analyzing PancakeSwap transactions on EVM-compatible blockchains (Binance Smart Chain, Ethereum, etc.). This library automatically decodes swap transactions and extracts detailed information including tokens involved, amounts, and contract addresses.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published