Created
October 26, 2024 17:33
-
-
Save Tee-py/fbec823bad9e1b8cf69a211c47e5f7d2 to your computer and use it in GitHub Desktop.
Anchor parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { clusterApiUrl, Connection, PartiallyDecodedInstruction, PublicKey } from "@solana/web3.js" | |
import { sha256 } from '@noble/hashes/sha256'; | |
import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes'; | |
import * as borsh from "@coral-xyz/borsh"; | |
import { BorshCoder, EventParser, Idl } from "@coral-xyz/anchor"; | |
import { PumpFunIDL } from './idl'; | |
const parseInstruction = async () => { | |
const signature = "4XQZckrFKjaLHM68kJH7dpSPo2TCfMkwjYhLdcNRu5QdJTjAEehsS5UMaZKDXADD46d8v4XnuyuvLV36rNRTKhn7"; | |
const connection = new Connection(clusterApiUrl("mainnet-beta")); | |
const transaction = await connection.getParsedTransaction(signature, { maxSupportedTransactionVersion: 0 }); | |
const PumpFunProgram = new PublicKey("6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P") | |
const pumpIxs = transaction?.transaction.message.instructions.filter((ix) => ix.programId.equals(PumpFunProgram)) | |
const buyDiscrimator = Buffer.from(sha256('global:buy').slice(0, 8)); | |
const sellDiscriminator = Buffer.from(sha256('global:sell').slice(0, 8)); | |
const buySellIxs = pumpIxs?.filter(ix => { | |
const discriminator = bs58.decode((ix as PartiallyDecodedInstruction).data).subarray(0, 8); | |
return discriminator.equals(buyDiscrimator) || discriminator.equals(sellDiscriminator) | |
}) | |
const tradeSchema = borsh.struct([ | |
borsh.u64("discriminator"), | |
borsh.u64("amount"), | |
borsh.u64("solAmount") | |
]) | |
for (let ix of buySellIxs!) { | |
ix = ix as PartiallyDecodedInstruction; | |
const ixDataArray = bs58.decode(ix.data); | |
const ixData = tradeSchema.decode(ixDataArray); | |
const type = bs58.decode(ix.data).subarray(0, 8).equals(buyDiscrimator) ? 'buy' : 'sell'; | |
const tokenAmount = ixData.amount.toString(); | |
const mint = ix.accounts[2].toBase58(); | |
const trader = ix.accounts[6].toBase58(); | |
const bondingCurve = ix.accounts[3]; | |
const index = transaction?.transaction.message.accountKeys.findIndex((ix) => ix.pubkey.equals(bondingCurve)) | |
const preBalances = transaction?.meta?.preBalances || []; | |
const postBalances = transaction?.meta?.postBalances || []; | |
const solAmount = Math.abs(preBalances[index!] - postBalances[index!]); | |
console.log("--------- Trade Data ------------") | |
console.log(`solAmount: ${solAmount}\ntokenAmount: ${tokenAmount}\ntype: ${type}\nmint: ${mint}\ntrader: ${trader}\n`) | |
} | |
} | |
const parseEvents = async () => { | |
const signature = "4XQZckrFKjaLHM68kJH7dpSPo2TCfMkwjYhLdcNRu5QdJTjAEehsS5UMaZKDXADD46d8v4XnuyuvLV36rNRTKhn7"; | |
const PumpFunProgram = new PublicKey("6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P") | |
const connection = new Connection(clusterApiUrl("mainnet-beta")); | |
const transaction = await connection.getParsedTransaction(signature, { maxSupportedTransactionVersion: 0 }); | |
const eventParser = new EventParser(PumpFunProgram, new BorshCoder(PumpFunIDL as unknown as Idl)); | |
const events = eventParser.parseLogs(transaction?.meta?.logMessages!); | |
for (let event of events) { | |
console.log("--------- Trade Event Data ------------") | |
console.log(`solAmount: ${event.data.solAmount}\ntokenAmount: ${event.data.tokenAmount}\ntype: ${event.data.isBuy ? 'buy' : 'sell'}\nmint: ${event.data.mint}\ntrader: ${event.data.user}\n`) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment