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.
- π 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
npm install evm-transaction-decoderor
yarn add evm-transaction-decoderimport { 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);
}Main class for detecting and decoding PancakeSwap transactions.
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 mappingsdebug: Enable debug logging (default: false)
Detects and decodes a PancakeSwap transaction.
Parameters:
tx: Transaction response from providerreceipt: Transaction receipt
Returns: Promise<SwapDetails | null>
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
}The library currently supports decoding for the following PancakeSwap router functions:
V3 Functions:
exactInputSingleexactInputexactOutputSingleexactOutput
V2 Functions:
swapExactTokensForTokensswapTokensForExactTokensswapExactETHForTokensswapTokensForExactETHswapExactTokensForETHswapETHForExactTokens
You can provide custom token symbol mappings:
const detector = new PancakeSwapDetector(provider, {
customTokens: {
'0xYourTokenAddress': 'CUSTOM',
'0xAnotherAddress': 'ANOTHER',
},
debug: true
});- Binance Smart Chain (BSC)
- Ethereum Mainnet
- Polygon
- Avalanche
- Optimism
- Arbitrum
- Any other EVM-compatible chain
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)}`);
}
}const detector = new PancakeSwapDetector(provider, {
customTokens: {
'0xYourTokenAddress': 'MYTOKEN',
},
debug: true
});npm run buildnpm run devnpm run example- Node.js >= 18.0.0
- ethers.js >= 6.0.0
MIT
Contributions are welcome! Please feel free to submit a Pull Request.
This library is provided for informational purposes only. Always verify the accuracy of decoded data independently before making any financial decisions.
For issues, questions, or suggestions, please open an issue on the GitHub repository.
Telegram - Crypmancer
Made with β€οΈ for the Ethereum ecosystem