- Notifications
You must be signed in to change notification settings - Fork21
An open-source transaction parser for popular DeFi applications on the Solana blockchain 🚀🤖..
License
Tee-py/solana-txn-parser
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
- PumpFun ✅
- RaydiumV4 ✅
- Jupiter 🔜
npm i solana-parser
import{PumpFunParser}from'solana-parser';import{Connection,PublicKey,clusterApiUrl,ParsedTransactionWithMeta}from'@solana/web3.js';importfsfrom"fs";constconnection=newConnection(clusterApiUrl('mainnet-beta'));constparser=newPumpFunParser();// Fetch a transactionconsttxnSig='<transaction_signature>'consttxn1=awaitconnection.getParsedTransaction(txnSig);// Parse single transactionconstpumpTxn=parser.parse(transaction);console.log(parsedTx);// Parse multiple transactionsconsttxnSig2='<second transaction signature>'consttxn2=awaitconnection.getParsedTransaction(txnSig2)constpumpTxns=parser.parseMultiple([txn1,txn2])// Parse transaction from json fileconsttxn=JSON.parse(fs.readFileSync("<file_path>","utf-8"))asunknownasParsedTransactionWithMetaconstpumpTxn=parser.parse(txn)
The parser returns aPumpFunTransaction
object (or an array ofPumpFunTransaction
objects ifparseMultiple
is called):
ThePumpFunTransaction
object shows the different operations that occurred in the transaction. These operations are gotten from the events emitted during the transaction execution and are represented by thePumpFunAction
interface as follows:
interfacePumpFunTransaction{platform:string;// pumpfunactions:PumpFunAction[];}
ThePumpFunAction
interface contains the three major actions that can occur in a PumpFun transaction (create
,complete
,trade
), with theinfo
field containing the relevant information for each action. The info field is of typeTradeInfo
,CreateInfo
, orCompleteInfo
depending on the action.
interfacePumpFunAction{type:"create"|"complete"|"trade";info:TradeInfo|CreateInfo|CompleteInfo;}typeTradeInfo={solAmount:bigint;tokenAmount:bigint;tokenMint:PublicKey;trader:PublicKey;isBuy:boolean;timestamp:bigint;virtualSolReserves:bigint;virtualTokenReserves:bigint;};typeCreateInfo={name:string;symbol:string;uri:string;tokenMint:PublicKey;bondingCurve:PublicKey;tokenDecimals:number;createdBy:PublicKey;};typeCompleteInfo={user:PublicKey;tokenMint:PublicKey;bondingCurve:PublicKey;timestamp:bigint;};
import{RaydiumV4Parser}from'sol-parser/src';import{Connection,PublicKey,clusterApiUrl,ParsedTransactionWithMeta}from'@solana/web3.js';importfsfrom"fs";constconnection=newConnection(clusterApiUrl('mainnet-beta'));// set max size of lru cache for caching decoded pool infoconstparser=newRaydiumV4Parser(connection,{maxPoolCache:20});// Fetch a transactionconsttxnSig='<transaction_signature>'consttxn1=awaitconnection.getParsedTransaction(txnSig);// Parse single transactionconstresult=awaitparser.parse(transaction);console.log(result);// Parse multiple transactionsconsttxnSig2='<second transaction signature>'consttxn2=awaitconnection.getParsedTransaction(txnSig2)constresults=awaitparser.parseMultiple([txn1,txn2])// Parse transaction from json fileconsttxn=JSON.parse(fs.readFileSync("<file_path>","utf-8"))asunknownasParsedTransactionWithMetaconstresult=parser.parse(txn)
You can create custom parsers for other DeFi platforms by extending theBaseParser
class:
import{BaseParser,ParsedTransactionWithMeta}from'solana-txn-parser';// define action informationtypeActionInfo={// add neccessary fields for the action};// define your custom actioninterfaceCustomActionextendsBaseParsedAction{info:ActionInfo;}// define your custom transactioninterfaceCustomTransactionextendsBaseParsedTransaction<CustomAction>{actions:CustomAction[];}// define your parser classclassCustomParserextendsBaseParser<CustomTransaction>{parse(transaction:ParsedTransactionWithMeta):CustomTransaction{// Implement your parsing logic here}parseMultiple(transactions:ParsedTransactionWithMeta[]):CustomTransaction[]{returntransactions.map((tx)=>this.parse(tx));}}
NB: For anchor specific parsers that rely on events, you can use the
anchorLogScanner
function present in thesrc/core/utils
file to get program events from the transaction.
Here's how you can contribute to the library:
- Fork the repository and create a new branch for your parser.
- Create a new folder in the
src/parsers
directory for your parser (e.g.,newparser
). - Add an index.ts file in the
src/parser/<newparser>
directory to hold your Parser logic - Implement your parser by extending the
BaseParser
class. - Write unit tests for your parser in the
tests/newparser
directory. - Update the README.md to include documentation for your new parser.
- Submit a pull request with your changes.
You can check the parser directory for more information on how to implement your new parser
- Fork the repository and create a new branch for your modifications.
- Make your changes to the existing parser file.
- If you're adding new functionality, make sure to add corresponding unit tests.
- If you're fixing a bug, add a test case that would have caught the bug.
- Update the README.md if your changes affect the usage of the parser.
- Submit a pull request with your changes, explaining the modifications and their purpose.
NB: For all contributions, please ensure your code passes all existing tests and include additional tests for the new parser. I also recommend using the
anchorLogScanner
function present in thesrc/core/utils
file to get anchor program events from the transaction to avoid having to install anchor library (trying to make this library as lightweight as possible).
This project is licensed under the MIT License - see theLICENSE file for details.
About
An open-source transaction parser for popular DeFi applications on the Solana blockchain 🚀🤖..