Files
libsam/pkg/shreder/txparser.go

1608 lines
46 KiB
Go
Raw Normal View History

2025-12-30 11:03:11 +08:00
package shreder
2025-12-26 10:57:37 +08:00
import (
"bytes"
"encoding/binary"
"fmt"
"math/big"
"github.com/gagliardetto/solana-go"
2025-12-30 11:03:11 +08:00
"github.com/mr-tron/base58"
2025-12-26 10:57:37 +08:00
"github.com/near/borsh-go"
"github.com/shopspring/decimal"
)
const (
wsolMint = "So11111111111111111111111111111111111111112"
tokenProgram = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA"
)
// program ids
2025-12-30 11:03:11 +08:00
var (
pumpProgramID = solana.MustPublicKeyFromBase58("6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P")
2025-12-26 10:57:37 +08:00
2025-12-30 11:03:11 +08:00
// has no sell function with pump and pump.amm program
azczProgramID = solana.MustPublicKeyFromBase58("AzcZqCRUQgKEg5FTAgY7JacATABEYCEfMbjXEzspLYFB")
// only buy function with pump program
f5tfProgramID = solana.MustPublicKeyFromBase58("F5tfvbLog9VdGUPqBDTT8rgXvTTcq7e5UiGnupL1zvBq")
// only pump.fun function
photonProgramID = solana.MustPublicKeyFromBase58("BSfD6SHZigAfDWSjzD5Q41jw8LmKwtmjskPH9XW1mrRW")
2025-12-26 10:57:37 +08:00
2025-12-30 11:03:11 +08:00
pumpAmmProgramID = solana.MustPublicKeyFromBase58("pAMMBay6oceH9fJKBRHGP5D4bD4sWpmSwMn52FMfXEA")
2025-12-26 10:57:37 +08:00
2025-12-30 11:03:11 +08:00
boboProgramID = solana.MustPublicKeyFromBase58("BobogA5N2KN2GG4XN3E3rNNRw3L8H1QPXp7QLxGrNHGM")
2025-12-26 10:57:37 +08:00
2025-12-30 11:03:11 +08:00
qtkvProgramID = solana.MustPublicKeyFromBase58("qtkvapJEvRWWrB7i5K6RaA1kvq5x3qmMKZ98ad71XQ7")
// only buy function with pump program
fjszProgramID = solana.MustPublicKeyFromBase58("FJsZbftBqRLfF7uqUKpm4s2goDr6xsQ5Q3mN7AFJB6hK")
flasProgramID = solana.MustPublicKeyFromBase58("FLASHX8DrLbgeR8FcfNV1F5krxYcYMUdBkrP1EPBtxB9")
terminalProgramID = solana.MustPublicKeyFromBase58("term9YPb9mzAsABaqN71A4xdbxHmpBNZavpBiQKZzN3")
2025-12-26 10:57:37 +08:00
)
2025-12-30 11:03:11 +08:00
// instruction discriminators
2025-12-26 10:57:37 +08:00
var (
2025-12-30 11:03:11 +08:00
pumpCreateCoinIX = []byte{24, 30, 200, 40, 5, 28, 7, 119}
pumpCreateCoinV2IX = []byte{214, 144, 76, 236, 95, 139, 49, 180}
pumpExtendedSellIX = []byte{51, 230, 133, 164, 1, 127, 131, 173}
pumpBuyTokensIX = []byte{102, 6, 61, 18, 1, 218, 235, 234}
pumpBuyV2TokensIX = []byte{56, 252, 116, 8, 158, 223, 205, 95}
azczBuyTokensIX = []byte{11}
azczAmmBuyTokensIX = []byte{0xf}
f5tfBuyTokensIX = []byte{0}
flasBuyTokensIX = []byte{0x00, 0x1, 0x4}
flasSellTokensIX = []byte{0x01, 0x1, 0x3}
flasAmmBuyTokensIX = []byte{0x00, 0x2, 0x2}
flasAmmSellTokensIX = []byte{0x01, 0x2, 0x2}
pumpAmmBuyTokensV2IX = []byte{198, 46, 21, 82, 180, 217, 232, 112}
pumpAmmBuyTokensIX = []byte{102, 6, 61, 18, 1, 218, 235, 234}
pumpAmmSellTokensIX = []byte{51, 230, 133, 164, 1, 127, 131, 173}
qtkvBuyTokensIX = []byte{0x02}
qtkvSellTokensIX = []byte{0x03}
qtkvAmmSellTokensIX = []byte{0x05}
2025-12-26 10:57:37 +08:00
boboBuyPumpTokensIX = []byte{0xff, 0xe7, 0x11, 0x53, 0x15, 0xc5, 0xc3, 0xdf}
fjszBuyTokensIX = []byte{0xe7, 0x3f, 0x99, 0x83, 0xf3, 0xed, 0xe3, 0x3c}
photonBuyPumpTokensIX = []byte{0x52, 0xe1, 0x77, 0xe7, 0x4e, 0x1d, 0x2d, 0x46}
photonSwapPumpAmmIX = []byte{0x2c, 0x77, 0xaf, 0xda, 0xc7, 0x4d, 0xc4, 0xeb}
2025-12-30 11:03:11 +08:00
terminalBuyTokensIX = []byte{0xa6, 0x54, 0x14, 0x96, 0x9f, 0x77, 0x59, 0xca}
terminalSellTokensIX = []byte{0xbe, 0x84, 0xa2, 0x96, 0x93, 0x7c, 0xf8, 0x6b}
terminalAmmSellTokensIX = []byte{0x40, 0x64, 0x97, 0xb9, 0x16, 0xfa, 0xec, 0xb1}
2025-12-26 10:57:37 +08:00
)
// table lookups
const (
photonTableLookup = "3r6paeFSLpeUVmWtShb5uZtXYpcBE3729kUxkUS7xKi1"
)
type compiledInstruction struct {
ProgramIDIndex uint8
Accounts []uint8
Data []byte
}
type addressTableLookup struct {
AccountKey solana.PublicKey
WritableIndexes []uint8
ReadonlyIndexes []uint8
}
type versionedMessage struct {
StaticAccountKeys []solana.PublicKey
Instructions []compiledInstruction
AddressTableLookups []addressTableLookup
}
type versionedTransaction struct {
Signatures []solana.Signature
Message versionedMessage
Block uint64
}
type pumpExtendedSellArgs struct {
Amount uint64
MinSolOutput uint64
}
type pumpBuyArgs struct {
Amount uint64
MaxSolCost uint64
}
type pumpCreateCoinV2Args struct {
Name string
Symbol string
Uri string
Creator solana.PublicKey
IsMayhemMode bool
}
type azczBuyArgs struct {
SolAmount uint64
TokenAmount uint64
}
type f5tfBuyArgs struct {
SolAmount uint64
TokenAmount uint64
}
type flasBuyArgs struct {
SolAmount uint64
TokenAmount uint64
Placeholder [3]uint8
}
type photonBuyPumpArgs struct {
Timestamp uint64
SolAmount uint64
TokenAmount uint64
Fee uint64
}
type photonSwapPumpAmmArgs struct {
FromAmount uint64
ToAmount uint64
}
type pumpAmmBuyArgs struct {
Amount uint64
MaxSolCost uint64
}
type _6hb1BuyArgs struct {
SolAmount uint64
TokenNumber uint64
}
type _8rsrBuyArgs struct {
SolIn uint64
TokenOut uint64
}
type boboBuyArgs struct {
Placeholder1 uint64
Placeholder2 uint64
SolAmount uint64
Placeholder3 uint64
Placeholder4 uint64
Placeholder5 uint64
Placeholder6 uint64
}
type qtkvBuyArgs struct {
Placeholder uint64
TokenNumber uint64
SolAmount uint64
}
type fjszBuyArgs struct {
SolAmount uint64
TokenAmount uint64
}
// ParseTransaction mirrors the Rust parse_transaction entry point.
2026-01-05 12:45:32 +08:00
func ParseTransaction(update *SubscribeUpdateTransaction, loader *AddressTables) []*TxSignal {
2025-12-26 10:57:37 +08:00
versioned, err := toVersionedTransaction(update)
if err != nil || versioned == nil || len(versioned.Signatures) == 0 {
return nil
}
2025-12-30 11:03:11 +08:00
txHash := versioned.Signatures[0]
2025-12-26 10:57:37 +08:00
staticKeys := versioned.Message.StaticAccountKeys
instructions := versioned.Message.Instructions
2026-01-05 12:45:32 +08:00
if loader != nil && len(versioned.Message.AddressTableLookups) > 0 {
// currently we only care about photon table lookup
for _, lookup := range versioned.Message.AddressTableLookups {
accounts := loader.GetAddressTable(lookup.AccountKey, lookup.WritableIndexes)
if len(accounts) != len(lookup.WritableIndexes) {
break
}
staticKeys = append(staticKeys, accounts...)
accounts2 := loader.GetAddressTable(lookup.AccountKey, lookup.ReadonlyIndexes)
if len(accounts2) != len(lookup.ReadonlyIndexes) {
break
}
staticKeys = append(staticKeys, accounts2...)
}
}
2025-12-30 11:03:11 +08:00
var parsed []*TxSignal
2025-12-26 10:57:37 +08:00
for i := range instructions {
inst := instructions[i]
if int(inst.ProgramIDIndex) >= len(staticKeys) {
continue
}
2025-12-30 11:03:11 +08:00
programID := staticKeys[inst.ProgramIDIndex]
2025-12-26 10:57:37 +08:00
switch programID {
case pumpProgramID:
txRes, err := parsePumpInstruction(versioned, i)
parsed = appendParsed(parsed, txRes, err, txHash, "pump")
case azczProgramID:
txRes, err := parseAzczInstruction(versioned, i)
parsed = appendParsed(parsed, txRes, err, txHash, "azcz")
case f5tfProgramID:
txRes, err := parseF5tfInstruction(versioned, i)
parsed = appendParsed(parsed, txRes, err, txHash, "f5tf")
case flasProgramID:
txRes, err := parseFlasInstruction(versioned, i)
parsed = appendParsed(parsed, txRes, err, txHash, "flas")
case photonProgramID:
txRes, err := parsePhotonInstruction(versioned, i)
parsed = appendParsed(parsed, txRes, err, txHash, "photon")
case pumpAmmProgramID:
txRes, err := parsePumpAmmInstruction(versioned, i)
parsed = appendParsed(parsed, txRes, err, txHash, "pumpamm")
case boboProgramID:
txRes, err := parseBoboInstruction(versioned, i)
parsed = appendParsed(parsed, txRes, err, txHash, "bobo")
case qtkvProgramID:
txRes, err := parseQtkvInstruction(versioned, i)
parsed = appendParsed(parsed, txRes, err, txHash, "qtkv")
case fjszProgramID:
txRes, err := parseFjszInstruction(versioned, i)
parsed = appendParsed(parsed, txRes, err, txHash, "fjsz")
2025-12-30 11:03:11 +08:00
case terminalProgramID:
txRes, err := parseTermInstruction(versioned, i)
parsed = appendParsed(parsed, txRes, err, txHash, "terminal")
2025-12-26 10:57:37 +08:00
}
}
return parsed
}
2025-12-30 11:03:11 +08:00
func appendParsed(list []*TxSignal, parsed *TxSignal, err error, txHash [64]byte, label string) []*TxSignal {
2025-12-26 10:57:37 +08:00
if err != nil {
2026-01-05 12:45:32 +08:00
logger.Debug("txparser: failed to parse", "label", label, "instruction", err, "tx_hash", base58.Encode(txHash[:]))
2025-12-26 10:57:37 +08:00
return list
}
if parsed != nil {
list = append(list, parsed)
}
return list
}
2025-12-30 11:03:11 +08:00
func toVersionedTransaction(update *SubscribeUpdateTransaction) (*versionedTransaction, error) {
2025-12-26 10:57:37 +08:00
if update == nil || update.Transaction == nil || update.Transaction.Message == nil {
return nil, fmt.Errorf("transaction is nil")
}
protoTx := update.Transaction
msg := protoTx.Message
signatures := make([]solana.Signature, len(protoTx.Signatures))
for i, rawSig := range protoTx.Signatures {
signatures[i] = solana.SignatureFromBytes(rawSig)
}
staticKeys := make([]solana.PublicKey, len(msg.AccountKeys))
for i, key := range msg.AccountKeys {
staticKeys[i] = solana.PublicKeyFromBytes(key)
}
instructions := make([]compiledInstruction, len(msg.Instructions))
for i, instr := range msg.Instructions {
accounts := append([]uint8(nil), instr.Accounts...)
instructions[i] = compiledInstruction{
ProgramIDIndex: uint8(instr.ProgramIdIndex),
Accounts: accounts,
Data: instr.Data,
}
}
lookups := make([]addressTableLookup, len(msg.AddressTableLookups))
for i, lookup := range msg.AddressTableLookups {
writable := append([]uint8(nil), lookup.WritableIndexes...)
readonly := append([]uint8(nil), lookup.ReadonlyIndexes...)
lookups[i] = addressTableLookup{
AccountKey: solana.PublicKeyFromBytes(lookup.AccountKey),
WritableIndexes: writable,
ReadonlyIndexes: readonly,
}
}
return &versionedTransaction{
Signatures: signatures,
Message: versionedMessage{
StaticAccountKeys: staticKeys,
Instructions: instructions,
AddressTableLookups: lookups,
},
Block: update.GetSlot(),
}, nil
}
func formatTokenAmount(amount uint64) decimal.Decimal {
val := decimal.NewFromBigInt(new(big.Int).SetUint64(amount), 0)
return val.Div(decimal.NewFromInt(1_000_000))
}
func formatSolAmount(lamports uint64) decimal.Decimal {
val := decimal.NewFromBigInt(new(big.Int).SetUint64(lamports), 0)
return val.Div(decimal.NewFromInt(1_000_000_000))
}
func getStaticKey(static []solana.PublicKey, index int) (solana.PublicKey, error) {
2026-01-05 12:45:32 +08:00
if index < 0 {
return solana.PublicKey{}, fmt.Errorf("account index %d less then 0", index)
}
if index >= len(static) {
return solana.PublicKey{}, fmt.Errorf("account index %d out of range", index)
2025-12-26 10:57:37 +08:00
}
return static[index], nil
}
2025-12-30 11:03:11 +08:00
func parsePumpInstruction(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
2025-12-26 10:57:37 +08:00
msg := tx.Message
if instructionIndex >= len(msg.Instructions) {
return nil, fmt.Errorf("instruction index out of bounds")
}
instruction := msg.Instructions[instructionIndex]
2025-12-30 11:03:11 +08:00
if len(instruction.Data) < 8 {
2025-12-26 10:57:37 +08:00
return nil, fmt.Errorf("data is empty")
}
2025-12-30 11:03:11 +08:00
if matchMethod(instruction.Data[0:8], pumpBuyV2TokensIX) || matchMethod(instruction.Data[0:8], pumpBuyTokensIX) {
2025-12-26 10:57:37 +08:00
return parsePumpBuy(tx, &instruction)
2025-12-30 11:03:11 +08:00
} else if matchMethod(instruction.Data[0:8], pumpExtendedSellIX) {
2025-12-26 10:57:37 +08:00
return parsePumpSell(tx, &instruction)
2025-12-30 11:03:11 +08:00
} else if matchMethod(instruction.Data[0:8], pumpCreateCoinIX) {
2025-12-26 10:57:37 +08:00
return parsePumpCreate(tx, &instruction)
2025-12-30 11:03:11 +08:00
} else if matchMethod(instruction.Data[0:8], pumpCreateCoinV2IX) {
2025-12-26 10:57:37 +08:00
return parsePumpCreateV2(tx, &instruction)
}
2025-12-30 11:03:11 +08:00
return nil, nil
2025-12-26 10:57:37 +08:00
}
2025-12-30 11:03:11 +08:00
func parsePumpCreate(tx *versionedTransaction, instruction *compiledInstruction) (*TxSignal, error) {
2025-12-26 10:57:37 +08:00
if len(instruction.Accounts) < 8 {
return nil, fmt.Errorf("accounts too short")
}
staticKeys := tx.Message.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[0]))
if err != nil {
return nil, err
}
creator, err := getStaticKey(staticKeys, int(instruction.Accounts[7]))
if err != nil {
return nil, err
}
2025-12-30 11:03:11 +08:00
return &TxSignal{
2025-12-26 10:57:37 +08:00
TxHash: tx.Signatures[0].String(),
Maker: creator.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: decimal.Zero,
Token1Amount: decimal.Zero,
Program: "Pump",
Event: "create",
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
Token0AmountUint64: 0,
Token1AmountUint64: 0,
}, nil
}
2025-12-30 11:03:11 +08:00
func parsePumpCreateV2(tx *versionedTransaction, instruction *compiledInstruction) (*TxSignal, error) {
2025-12-26 10:57:37 +08:00
if len(instruction.Accounts) < 8 {
return nil, fmt.Errorf("accounts too short")
}
if len(instruction.Data) < 8 {
return nil, fmt.Errorf("data too short for create v2 args")
}
staticKeys := tx.Message.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[0]))
if err != nil {
return nil, err
}
tokenProgramKey, err := getStaticKey(staticKeys, int(instruction.Accounts[7]))
if err != nil {
return nil, err
}
var args pumpCreateCoinV2Args
if err := borsh.Deserialize(&args, instruction.Data[8:]); err != nil {
return nil, fmt.Errorf("failed to parse create coin v2 args: %w", err)
}
2025-12-30 11:03:11 +08:00
return &TxSignal{
2025-12-26 10:57:37 +08:00
TxHash: tx.Signatures[0].String(),
Maker: args.Creator.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: decimal.Zero,
Token1Amount: decimal.Zero,
Program: "Pump",
Event: "create",
IsToken2022: tokenProgramKey.String() != tokenProgram,
IsMayhemMode: args.IsMayhemMode,
Block: tx.Block,
Token0AmountUint64: 0,
Token1AmountUint64: 0,
}, nil
}
func decodePumpBuyArgs(data []byte) (uint64, uint64, error) {
if len(data) < 9 {
return 0, 0, fmt.Errorf("data too short for buy args")
}
var args pumpBuyArgs
if err := borsh.Deserialize(&args, data[8:]); err == nil {
return args.Amount, args.MaxSolCost, nil
}
if len(data) >= 24 {
amount := binary.LittleEndian.Uint64(data[8:16])
maxSol := binary.LittleEndian.Uint64(data[16:24])
return amount, maxSol, nil
}
return 0, 0, fmt.Errorf("failed to parse buy tokens args")
}
2025-12-30 11:03:11 +08:00
func parsePumpBuy(tx *versionedTransaction, instruction *compiledInstruction) (*TxSignal, error) {
amount, sol, err := decodePumpBuyArgs(instruction.Data)
2025-12-26 10:57:37 +08:00
if err != nil {
return nil, err
}
2025-12-30 11:03:11 +08:00
exactIn := false
if matchMethod(instruction.Data, pumpBuyV2TokensIX) {
temp := amount
amount = sol
sol = temp
exactIn = true
}
2025-12-26 10:57:37 +08:00
if len(instruction.Accounts) < 7 {
return nil, fmt.Errorf("accounts too short")
}
staticKeys := tx.Message.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[2]))
if err != nil {
return nil, err
}
buyer, err := getStaticKey(staticKeys, int(instruction.Accounts[6]))
if err != nil {
return nil, err
}
2025-12-30 11:03:11 +08:00
return &TxSignal{
2025-12-26 10:57:37 +08:00
TxHash: tx.Signatures[0].String(),
Maker: buyer.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: formatTokenAmount(amount),
2025-12-30 11:03:11 +08:00
Token1Amount: formatSolAmount(sol),
2025-12-26 10:57:37 +08:00
Program: "Pump",
Event: "buy",
2025-12-30 11:03:11 +08:00
ExactSOL: exactIn,
2025-12-26 10:57:37 +08:00
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
Token0AmountUint64: amount,
2025-12-30 11:03:11 +08:00
Token1AmountUint64: sol,
2025-12-26 10:57:37 +08:00
}, nil
}
func decodePumpSellArgs(data []byte) (uint64, uint64, error) {
if len(data) < 9 {
return 0, 0, fmt.Errorf("data too short for sell args")
}
var args pumpExtendedSellArgs
if err := borsh.Deserialize(&args, data[8:]); err == nil {
return args.Amount, args.MinSolOutput, nil
}
if len(data) >= 24 {
amount := binary.LittleEndian.Uint64(data[8:16])
minSol := binary.LittleEndian.Uint64(data[16:24])
return amount, minSol, nil
}
return 0, 0, fmt.Errorf("failed to parse sell tokens args")
}
2025-12-30 11:03:11 +08:00
func parsePumpSell(tx *versionedTransaction, instruction *compiledInstruction) (*TxSignal, error) {
2025-12-26 10:57:37 +08:00
amount, minSol, err := decodePumpSellArgs(instruction.Data)
if err != nil {
return nil, err
}
if len(instruction.Accounts) < 7 {
return nil, fmt.Errorf("accounts too short")
}
staticKeys := tx.Message.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[2]))
if err != nil {
return nil, err
}
seller, err := getStaticKey(staticKeys, int(instruction.Accounts[6]))
if err != nil {
return nil, err
}
2025-12-30 11:03:11 +08:00
return &TxSignal{
2025-12-26 10:57:37 +08:00
TxHash: tx.Signatures[0].String(),
Maker: seller.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: formatTokenAmount(amount),
Token1Amount: formatSolAmount(minSol),
Program: "Pump",
Event: "sell",
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
Token0AmountUint64: amount,
Token1AmountUint64: minSol,
}, nil
}
2025-12-30 11:03:11 +08:00
func parseAzczInstruction(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
2025-12-26 10:57:37 +08:00
msg := tx.Message
if instructionIndex >= len(msg.Instructions) {
return nil, fmt.Errorf("instruction index out of bounds")
}
instruction := msg.Instructions[instructionIndex]
if len(instruction.Data) == 0 {
return nil, fmt.Errorf("data is empty")
}
2025-12-30 11:03:11 +08:00
if matchMethod(instruction.Data, azczBuyTokensIX) {
return parseAzczBuy(tx, instructionIndex)
} else if matchMethod(instruction.Data, azczAmmBuyTokensIX) {
return parseAzczAmmBuy(tx, instructionIndex)
2025-12-26 10:57:37 +08:00
}
2025-12-30 11:03:11 +08:00
return nil, nil
}
2025-12-26 10:57:37 +08:00
2025-12-30 11:03:11 +08:00
func parseAzczAmmBuy(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
instruction := tx.Message.Instructions[instructionIndex]
2025-12-26 10:57:37 +08:00
if len(instruction.Accounts) < 8 {
return nil, fmt.Errorf("accounts too short")
}
2025-12-30 11:03:11 +08:00
staticKeys := tx.Message.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[3]))
if err != nil {
return nil, err
}
user, err := getStaticKey(staticKeys, int(instruction.Accounts[1]))
if err != nil {
return nil, err
}
if len(instruction.Data) < 17 {
return nil, fmt.Errorf("data too short for buy args")
}
solAmount := binary.LittleEndian.Uint64(instruction.Data[1:9])
return &TxSignal{
TxHash: tx.Signatures[0].String(),
Maker: user.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: decimal.Zero,
Token1Amount: formatSolAmount(solAmount),
Program: "Pump",
Event: "buy",
IsToken2022: false,
IsMayhemMode: false,
ExactSOL: true,
Block: tx.Block,
Token0AmountUint64: 0,
Token1AmountUint64: solAmount,
}, nil
}
func parseAzczBuy(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
instruction := tx.Message.Instructions[instructionIndex]
if len(instruction.Accounts) < 8 {
return nil, fmt.Errorf("accounts too short")
}
staticKeys := tx.Message.StaticAccountKeys
2025-12-26 10:57:37 +08:00
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[3]))
if err != nil {
return nil, err
}
user, err := getStaticKey(staticKeys, int(instruction.Accounts[7]))
if err != nil {
return nil, err
}
if len(instruction.Data) < 2 {
return nil, fmt.Errorf("data too short for buy args")
}
var args azczBuyArgs
if err := borsh.Deserialize(&args, instruction.Data[1:]); err != nil {
return nil, fmt.Errorf("failed to parse buy tokens args: %w", err)
}
2025-12-30 11:03:11 +08:00
return &TxSignal{
2025-12-26 10:57:37 +08:00
TxHash: tx.Signatures[0].String(),
Maker: user.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: formatTokenAmount(args.TokenAmount),
Token1Amount: formatSolAmount(args.SolAmount),
Program: "Pump",
Event: "buy",
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
Token0AmountUint64: args.TokenAmount,
Token1AmountUint64: args.SolAmount,
}, nil
}
2025-12-30 11:03:11 +08:00
func parseF5tfInstruction(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
2025-12-26 10:57:37 +08:00
msg := tx.Message
if instructionIndex >= len(msg.Instructions) {
return nil, fmt.Errorf("instruction index out of bounds")
}
instruction := msg.Instructions[instructionIndex]
if len(instruction.Data) == 0 {
return nil, fmt.Errorf("data is empty")
}
2025-12-30 11:03:11 +08:00
if !matchMethod(instruction.Data, f5tfBuyTokensIX) {
2025-12-26 10:57:37 +08:00
return nil, nil
}
if len(instruction.Accounts) < 7 {
return nil, fmt.Errorf("accounts too short")
}
staticKeys := msg.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[2]))
if err != nil {
return nil, err
}
user, err := getStaticKey(staticKeys, int(instruction.Accounts[6]))
if err != nil {
return nil, err
}
if len(instruction.Data) < 2 {
return nil, fmt.Errorf("data too short for buy args")
}
var args f5tfBuyArgs
if err := borsh.Deserialize(&args, instruction.Data[1:]); err != nil {
return nil, fmt.Errorf("failed to parse buy tokens args: %w", err)
}
2025-12-30 11:03:11 +08:00
return &TxSignal{
2025-12-26 10:57:37 +08:00
TxHash: tx.Signatures[0].String(),
Maker: user.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: formatTokenAmount(args.TokenAmount),
Token1Amount: formatSolAmount(args.SolAmount),
Program: "Pump",
Event: "buy",
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
Token0AmountUint64: args.TokenAmount,
Token1AmountUint64: args.SolAmount,
}, nil
}
2025-12-30 11:03:11 +08:00
func parseFlasInstruction(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
2025-12-26 10:57:37 +08:00
msg := tx.Message
if instructionIndex >= len(msg.Instructions) {
return nil, fmt.Errorf("instruction index out of bounds")
}
instruction := msg.Instructions[instructionIndex]
if len(instruction.Data) == 0 {
return nil, fmt.Errorf("data is empty")
}
2025-12-30 11:03:11 +08:00
if len(instruction.Data) < 20 {
return nil, fmt.Errorf("data too short for args")
}
methodData := instruction.Data[17:20]
if !matchMethod(methodData, flasBuyTokensIX) {
2025-12-26 10:57:37 +08:00
return nil, nil
}
2025-12-30 11:03:11 +08:00
if matchMethod(methodData, f5tfBuyTokensIX) {
return parseFlasBuy(tx, instructionIndex)
} else if matchMethod(methodData, flasSellTokensIX) {
return parseFlasSell(tx, instructionIndex)
} else if matchMethod(methodData, flasAmmBuyTokensIX) {
return parseFlasAmmBuy(tx, instructionIndex)
} else if matchMethod(methodData, flasAmmSellTokensIX) {
return parseFlasAmmSell(tx, instructionIndex)
}
return nil, nil
}
func parseFlasAmmSell(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
instruction := tx.Message.Instructions[instructionIndex]
if len(instruction.Accounts) < 10 {
return nil, fmt.Errorf("accounts too short")
}
2025-12-26 10:57:37 +08:00
2025-12-30 11:03:11 +08:00
staticKeys := tx.Message.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[9]))
if err != nil {
return nil, err
}
user, err := getStaticKey(staticKeys, int(instruction.Accounts[1]))
if err != nil {
return nil, err
}
var args flasBuyArgs
if err := borsh.Deserialize(&args, instruction.Data[1:]); err != nil {
return nil, fmt.Errorf("failed to parse buy tokens args: %w", err)
}
return &TxSignal{
TxHash: tx.Signatures[0].String(),
Maker: user.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: decimal.Zero,
Token1Amount: formatSolAmount(args.TokenAmount),
Program: "Pump",
Event: "sell",
IsToken2022: false,
IsMayhemMode: false,
ExactSOL: true,
Block: tx.Block,
Token0AmountUint64: 0,
Token1AmountUint64: args.TokenAmount,
}, nil
}
func parseFlasAmmBuy(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
instruction := tx.Message.Instructions[instructionIndex]
if len(instruction.Accounts) < 10 {
return nil, fmt.Errorf("accounts too short")
}
staticKeys := tx.Message.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[9]))
if err != nil {
return nil, err
}
user, err := getStaticKey(staticKeys, int(instruction.Accounts[1]))
if err != nil {
return nil, err
}
var args flasBuyArgs
if err := borsh.Deserialize(&args, instruction.Data[1:]); err != nil {
return nil, fmt.Errorf("failed to parse buy tokens args: %w", err)
}
return &TxSignal{
TxHash: tx.Signatures[0].String(),
Maker: user.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: decimal.Zero,
Token1Amount: formatSolAmount(args.TokenAmount),
Program: "Pump",
Event: "sell",
IsToken2022: false,
IsMayhemMode: false,
ExactSOL: true,
Block: tx.Block,
Token0AmountUint64: 0,
Token1AmountUint64: args.TokenAmount,
}, nil
}
func parseFlasSell(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
instruction := tx.Message.Instructions[instructionIndex]
2025-12-26 10:57:37 +08:00
if len(instruction.Accounts) < 9 {
return nil, fmt.Errorf("accounts too short")
}
2025-12-30 11:03:11 +08:00
staticKeys := tx.Message.StaticAccountKeys
2025-12-26 10:57:37 +08:00
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[8]))
if err != nil {
return nil, err
}
2025-12-30 11:03:11 +08:00
user, err := getStaticKey(staticKeys, int(instruction.Accounts[1]))
2025-12-26 10:57:37 +08:00
if err != nil {
return nil, err
}
2025-12-30 11:03:11 +08:00
var args flasBuyArgs
if err := borsh.Deserialize(&args, instruction.Data[1:]); err != nil {
return nil, fmt.Errorf("failed to parse buy tokens args: %w", err)
}
return &TxSignal{
TxHash: tx.Signatures[0].String(),
Maker: user.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: formatTokenAmount(args.TokenAmount),
Token1Amount: formatSolAmount(args.SolAmount),
Program: "Pump",
Event: "sell",
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
Token0AmountUint64: args.TokenAmount,
Token1AmountUint64: args.SolAmount,
}, nil
}
func parseFlasBuy(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
instruction := tx.Message.Instructions[instructionIndex]
if len(instruction.Accounts) < 9 {
return nil, fmt.Errorf("accounts too short")
}
staticKeys := tx.Message.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[8]))
if err != nil {
return nil, err
}
user, err := getStaticKey(staticKeys, int(instruction.Accounts[0]))
if err != nil {
return nil, err
2025-12-26 10:57:37 +08:00
}
var args flasBuyArgs
if err := borsh.Deserialize(&args, instruction.Data[1:]); err != nil {
return nil, fmt.Errorf("failed to parse buy tokens args: %w", err)
}
2025-12-30 11:03:11 +08:00
return &TxSignal{
2025-12-26 10:57:37 +08:00
TxHash: tx.Signatures[0].String(),
Maker: user.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: formatTokenAmount(args.TokenAmount),
Token1Amount: formatSolAmount(args.SolAmount),
Program: "Pump",
Event: "buy",
IsToken2022: false,
IsMayhemMode: false,
2025-12-30 11:03:11 +08:00
ExactSOL: true,
2025-12-26 10:57:37 +08:00
Block: tx.Block,
Token0AmountUint64: args.TokenAmount,
Token1AmountUint64: args.SolAmount,
}, nil
}
2025-12-30 11:03:11 +08:00
func parsePhotonInstruction(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
2025-12-26 10:57:37 +08:00
msg := tx.Message
if instructionIndex >= len(msg.Instructions) {
return nil, fmt.Errorf("instruction index out of bounds")
}
instruction := msg.Instructions[instructionIndex]
if len(instruction.Data) == 0 {
return nil, fmt.Errorf("data is empty")
}
if len(instruction.Data) < 8 {
return nil, nil
}
switch {
case bytes.Equal(instruction.Data[:8], photonBuyPumpTokensIX):
return parsePhotonBuy(tx, &instruction)
case bytes.Equal(instruction.Data[:8], photonSwapPumpAmmIX):
return parsePhotonSwap(tx, &instruction)
default:
return nil, nil
}
}
2025-12-30 11:03:11 +08:00
func parsePhotonBuy(tx *versionedTransaction, instruction *compiledInstruction) (*TxSignal, error) {
2025-12-26 10:57:37 +08:00
if len(instruction.Accounts) < 8 {
return nil, fmt.Errorf("accounts too short")
}
if len(instruction.Data) < 16 {
return nil, fmt.Errorf("data too short for buy args")
}
staticKeys := tx.Message.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[3]))
if err != nil {
return nil, err
}
user, err := getStaticKey(staticKeys, int(instruction.Accounts[7]))
if err != nil {
return nil, err
}
var args photonBuyPumpArgs
if err := borsh.Deserialize(&args, instruction.Data[8:]); err != nil {
return nil, fmt.Errorf("failed to parse buy tokens args: %w", err)
}
solAmount := args.SolAmount * (100000000 - 1234568) / 100000000
2025-12-30 11:03:11 +08:00
return &TxSignal{
2025-12-26 10:57:37 +08:00
TxHash: tx.Signatures[0].String(),
Maker: user.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: formatTokenAmount(args.TokenAmount),
Token1Amount: formatSolAmount(solAmount),
Program: "Pump",
Event: "buy",
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
Token0AmountUint64: args.TokenAmount,
Token1AmountUint64: solAmount,
}, nil
}
2025-12-30 11:03:11 +08:00
func parsePhotonSwap(tx *versionedTransaction, instruction *compiledInstruction) (*TxSignal, error) {
2025-12-26 10:57:37 +08:00
if len(instruction.Accounts) < 8 {
return nil, fmt.Errorf("accounts too short")
}
if len(instruction.Data) < 16 {
return nil, fmt.Errorf("data too short for swap args")
}
staticKeys := tx.Message.StaticAccountKeys
base, err := getStaticKey(staticKeys, int(instruction.Accounts[3]))
if err != nil {
return nil, err
}
quoteIndex := int(instruction.Accounts[4])
quote, err := resolveQuoteAccount(tx, quoteIndex, []string{photonTableLookup}, 0)
if err != nil {
return nil, err
}
if quote != wsolMint {
return nil, nil
}
buyer, err := getStaticKey(staticKeys, int(instruction.Accounts[1]))
if err != nil {
return nil, err
}
var args photonSwapPumpAmmArgs
if err := borsh.Deserialize(&args, instruction.Data[8:]); err != nil {
return nil, fmt.Errorf("failed to parse swap pump amm tokens args: %w", err)
}
if args.FromAmount > args.ToAmount {
// sell; ignore
return nil, nil
}
solAmount := args.FromAmount * (100000000 - 1234568) / 100000000
2025-12-30 11:03:11 +08:00
return &TxSignal{
2025-12-26 10:57:37 +08:00
TxHash: tx.Signatures[0].String(),
Maker: buyer.String(),
Token0Address: base.String(),
Token1Address: wsolMint,
Token0Amount: formatTokenAmount(args.ToAmount),
Token1Amount: formatSolAmount(solAmount),
Program: "PumpAMM",
Event: "buy",
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
Token0AmountUint64: args.ToAmount,
Token1AmountUint64: solAmount,
}, nil
}
2025-12-30 11:03:11 +08:00
func parsePumpAmmInstruction(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
2025-12-26 10:57:37 +08:00
msg := tx.Message
if instructionIndex >= len(msg.Instructions) {
return nil, fmt.Errorf("instruction index out of bounds")
}
instruction := msg.Instructions[instructionIndex]
if len(instruction.Data) == 0 {
return nil, fmt.Errorf("data is empty")
}
2025-12-30 11:03:11 +08:00
if matchMethod(instruction.Data, pumpAmmBuyTokensIX) || matchMethod(instruction.Data, pumpAmmBuyTokensV2IX) {
return parsePumpAmmBuy(tx, &instruction)
} else if matchMethod(instruction.Data, pumpAmmSellTokensIX) {
return parsePumpAmmSell(tx, &instruction)
}
return nil, nil
}
func parseTermInstruction(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
msg := tx.Message
if instructionIndex >= len(msg.Instructions) {
return nil, fmt.Errorf("instruction index out of bounds")
}
instruction := msg.Instructions[instructionIndex]
if len(instruction.Data) == 0 {
return nil, fmt.Errorf("data is empty")
}
if len(instruction.Data) < 24 {
2025-12-26 10:57:37 +08:00
return nil, nil
}
2025-12-30 11:03:11 +08:00
switch {
case bytes.Equal(instruction.Data[:8], terminalBuyTokensIX):
return parseTermBuy(tx, &instruction)
case bytes.Equal(instruction.Data[:8], terminalSellTokensIX):
return parseTermSell(tx, &instruction)
case bytes.Equal(instruction.Data[:8], terminalAmmSellTokensIX):
return parseTermAmmSell(tx, &instruction)
default:
return nil, nil
}
}
func parseTermAmmSell(tx *versionedTransaction, instruction *compiledInstruction) (*TxSignal, error) {
if len(instruction.Accounts) < 8 {
return nil, fmt.Errorf("accounts too short")
}
staticKeys := tx.Message.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[3]))
if err != nil {
return nil, err
}
user, err := getStaticKey(staticKeys, int(instruction.Accounts[1]))
if err != nil {
return nil, err
}
solAmount := binary.LittleEndian.Uint64(instruction.Data[8:16])
tokenAmount := binary.LittleEndian.Uint64(instruction.Data[16:24])
return &TxSignal{
TxHash: tx.Signatures[0].String(),
Maker: user.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: formatTokenAmount(tokenAmount),
Token1Amount: formatSolAmount(solAmount),
Program: "Pump",
Event: "buy",
IsToken2022: false,
IsMayhemMode: false,
ExactSOL: true,
Block: tx.Block,
Token0AmountUint64: tokenAmount,
Token1AmountUint64: solAmount,
}, nil
}
func parseTermBuy(tx *versionedTransaction, instruction *compiledInstruction) (*TxSignal, error) {
if len(instruction.Accounts) < 8 {
return nil, fmt.Errorf("accounts too short")
}
staticKeys := tx.Message.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[2]))
if err != nil {
return nil, err
}
user, err := getStaticKey(staticKeys, int(instruction.Accounts[6]))
if err != nil {
return nil, err
}
solAmount := binary.LittleEndian.Uint64(instruction.Data[8:16])
tokenAmount := binary.LittleEndian.Uint64(instruction.Data[16:24])
return &TxSignal{
TxHash: tx.Signatures[0].String(),
Maker: user.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: formatTokenAmount(tokenAmount),
Token1Amount: formatSolAmount(solAmount),
Program: "Pump",
Event: "buy",
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
Token0AmountUint64: tokenAmount,
Token1AmountUint64: solAmount,
}, nil
}
func parseTermSell(tx *versionedTransaction, instruction *compiledInstruction) (*TxSignal, error) {
if len(instruction.Accounts) < 8 {
return nil, fmt.Errorf("accounts too short")
}
staticKeys := tx.Message.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[2]))
if err != nil {
return nil, err
}
user, err := getStaticKey(staticKeys, int(instruction.Accounts[6]))
if err != nil {
return nil, err
}
tokenAmount := binary.LittleEndian.Uint64(instruction.Data[8:16])
solAmount := binary.LittleEndian.Uint64(instruction.Data[16:24])
return &TxSignal{
TxHash: tx.Signatures[0].String(),
Maker: user.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: formatTokenAmount(tokenAmount),
Token1Amount: formatSolAmount(solAmount),
Program: "Pump",
Event: "buy",
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
Token0AmountUint64: tokenAmount,
Token1AmountUint64: solAmount,
}, nil
2025-12-26 10:57:37 +08:00
}
func decodePumpAmmBuyArgs(data []byte) (uint64, uint64, error) {
if len(data) < 9 {
return 0, 0, fmt.Errorf("data too short for buy args")
}
var args pumpAmmBuyArgs
if err := borsh.Deserialize(&args, data[8:]); err == nil {
return args.Amount, args.MaxSolCost, nil
}
if len(data) >= 24 {
amount := binary.LittleEndian.Uint64(data[8:16])
maxSol := binary.LittleEndian.Uint64(data[16:24])
return amount, maxSol, nil
}
return 0, 0, fmt.Errorf("failed to parse buy tokens args")
}
2025-12-30 11:03:11 +08:00
func parsePumpAmmBuy(tx *versionedTransaction, instruction *compiledInstruction) (*TxSignal, error) {
2025-12-26 10:57:37 +08:00
amount, maxSol, err := decodePumpAmmBuyArgs(instruction.Data)
if err != nil {
return nil, err
}
2025-12-30 11:03:11 +08:00
exactIn := false
if matchMethod(instruction.Data, pumpAmmBuyTokensV2IX) {
temp := amount
amount = maxSol
maxSol = temp
exactIn = true
}
2025-12-26 10:57:37 +08:00
if len(instruction.Accounts) < 7 {
return nil, fmt.Errorf("accounts too short")
}
staticKeys := tx.Message.StaticAccountKeys
base, err := getStaticKey(staticKeys, int(instruction.Accounts[3]))
if err != nil {
return nil, err
}
quoteIndex := int(instruction.Accounts[4])
quote, err := resolveQuoteAccount(tx, quoteIndex, nil, 0)
if err != nil {
return nil, err
}
if quote != wsolMint {
return nil, nil
}
buyer, err := getStaticKey(staticKeys, int(instruction.Accounts[1]))
if err != nil {
return nil, err
}
2025-12-30 11:03:11 +08:00
return &TxSignal{
2025-12-26 10:57:37 +08:00
TxHash: tx.Signatures[0].String(),
Maker: buyer.String(),
Token0Address: base.String(),
Token1Address: wsolMint,
Token0Amount: formatTokenAmount(amount),
Token1Amount: formatSolAmount(maxSol),
Program: "PumpAMM",
Event: "buy",
IsToken2022: false,
IsMayhemMode: false,
2025-12-30 11:03:11 +08:00
ExactSOL: exactIn,
2025-12-26 10:57:37 +08:00
Block: tx.Block,
Token0AmountUint64: amount,
Token1AmountUint64: maxSol,
}, nil
}
2025-12-30 11:03:11 +08:00
func parsePumpAmmSell(tx *versionedTransaction, instruction *compiledInstruction) (*TxSignal, error) {
amount, minSol, err := decodePumpAmmBuyArgs(instruction.Data)
if err != nil {
return nil, err
2025-12-26 10:57:37 +08:00
}
if len(instruction.Accounts) < 7 {
return nil, fmt.Errorf("accounts too short")
}
staticKeys := tx.Message.StaticAccountKeys
base, err := getStaticKey(staticKeys, int(instruction.Accounts[3]))
if err != nil {
return nil, err
}
quoteIndex := int(instruction.Accounts[4])
2025-12-30 11:03:11 +08:00
quote, err := resolveQuoteAccount(tx, quoteIndex, nil, 0)
2025-12-26 10:57:37 +08:00
if err != nil {
return nil, err
}
if quote != wsolMint {
return nil, nil
}
buyer, err := getStaticKey(staticKeys, int(instruction.Accounts[1]))
if err != nil {
return nil, err
}
2025-12-30 11:03:11 +08:00
return &TxSignal{
2025-12-26 10:57:37 +08:00
TxHash: tx.Signatures[0].String(),
Maker: buyer.String(),
Token0Address: base.String(),
Token1Address: wsolMint,
2025-12-30 11:03:11 +08:00
Token0Amount: formatTokenAmount(amount),
Token1Amount: formatSolAmount(minSol),
2025-12-26 10:57:37 +08:00
Program: "PumpAMM",
2025-12-30 11:03:11 +08:00
Event: "sell",
2025-12-26 10:57:37 +08:00
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
2025-12-30 11:03:11 +08:00
Token0AmountUint64: amount,
Token1AmountUint64: minSol,
2025-12-26 10:57:37 +08:00
}, nil
}
2025-12-30 11:03:11 +08:00
func parseBoboInstruction(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
2025-12-26 10:57:37 +08:00
msg := tx.Message
if instructionIndex >= len(msg.Instructions) {
return nil, fmt.Errorf("instruction index out of bounds")
}
instruction := msg.Instructions[instructionIndex]
if len(instruction.Data) == 0 {
return nil, fmt.Errorf("data is empty")
}
2025-12-30 11:03:11 +08:00
if len(instruction.Data) < 8 || !bytes.Equal(instruction.Data[:8], boboBuyPumpTokensIX) {
2025-12-26 10:57:37 +08:00
return nil, nil
}
2025-12-30 11:03:11 +08:00
if len(instruction.Accounts) < 8 {
2025-12-26 10:57:37 +08:00
return nil, fmt.Errorf("accounts too short")
}
if len(instruction.Data) < 16 {
return nil, fmt.Errorf("data too short for buy args")
}
staticKeys := tx.Message.StaticAccountKeys
2025-12-30 11:03:11 +08:00
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[2]))
2025-12-26 10:57:37 +08:00
if err != nil {
return nil, err
}
2025-12-30 11:03:11 +08:00
user, err := getStaticKey(staticKeys, int(instruction.Accounts[6]))
2025-12-26 10:57:37 +08:00
if err != nil {
return nil, err
}
2025-12-30 11:03:11 +08:00
var args boboBuyArgs
2025-12-26 10:57:37 +08:00
if err := borsh.Deserialize(&args, instruction.Data[8:]); err != nil {
return nil, fmt.Errorf("failed to parse buy tokens args: %w", err)
}
2025-12-30 11:03:11 +08:00
return &TxSignal{
2025-12-26 10:57:37 +08:00
TxHash: tx.Signatures[0].String(),
2025-12-30 11:03:11 +08:00
Maker: user.String(),
Token0Address: mint.String(),
2025-12-26 10:57:37 +08:00
Token1Address: wsolMint,
2025-12-30 11:03:11 +08:00
Token0Amount: decimal.NewFromInt(1),
Token1Amount: formatSolAmount(args.SolAmount),
Program: "Pump",
2025-12-26 10:57:37 +08:00
Event: "buy",
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
2025-12-30 11:03:11 +08:00
Token0AmountUint64: 1,
Token1AmountUint64: args.SolAmount,
2025-12-26 10:57:37 +08:00
}, nil
}
2025-12-30 11:03:11 +08:00
func parseQtkvInstruction(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
2025-12-26 10:57:37 +08:00
msg := tx.Message
if instructionIndex >= len(msg.Instructions) {
return nil, fmt.Errorf("instruction index out of bounds")
}
instruction := msg.Instructions[instructionIndex]
if len(instruction.Data) == 0 {
return nil, fmt.Errorf("data is empty")
}
2025-12-30 11:03:11 +08:00
if matchMethod(instruction.Data, qtkvBuyTokensIX) {
return parseQtkvBuy(tx, instructionIndex)
} else if matchMethod(instruction.Data, qtkvAmmSellTokensIX) {
return parseQtkvAmmSell(tx, instructionIndex)
} else if matchMethod(instruction.Data, qtkvSellTokensIX) {
return parseQtkvSell(tx, instructionIndex)
2025-12-26 10:57:37 +08:00
}
2025-12-30 11:03:11 +08:00
return nil, nil
}
func parseQtkvSell(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
instruction := tx.Message.Instructions[instructionIndex]
if len(instruction.Accounts) < 11 {
2025-12-26 10:57:37 +08:00
return nil, fmt.Errorf("accounts too short")
}
2025-12-30 11:03:11 +08:00
if len(instruction.Data) < 24 {
return nil, fmt.Errorf("data too short for sell args")
2025-12-26 10:57:37 +08:00
}
staticKeys := tx.Message.StaticAccountKeys
2025-12-30 11:03:11 +08:00
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[10]))
2025-12-26 10:57:37 +08:00
if err != nil {
return nil, err
}
2025-12-30 11:03:11 +08:00
user, err := getStaticKey(staticKeys, int(instruction.Accounts[0]))
2025-12-26 10:57:37 +08:00
if err != nil {
return nil, err
}
2025-12-30 11:03:11 +08:00
// in sell, sol amount is not directly provided, so we set it to 0
tokenAmount := binary.LittleEndian.Uint64(instruction.Data[19:25])
return &TxSignal{
2025-12-26 10:57:37 +08:00
TxHash: tx.Signatures[0].String(),
Maker: user.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
2025-12-30 11:03:11 +08:00
Token0Amount: formatTokenAmount(tokenAmount),
Token1Amount: decimal.Zero,
2025-12-26 10:57:37 +08:00
Program: "Pump",
2025-12-30 11:03:11 +08:00
Event: "sell",
2025-12-26 10:57:37 +08:00
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
2025-12-30 11:03:11 +08:00
Token0AmountUint64: tokenAmount,
Token1AmountUint64: 0,
2025-12-26 10:57:37 +08:00
}, nil
}
2025-12-30 11:03:11 +08:00
func parseQtkvAmmSell(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
instruction := tx.Message.Instructions[instructionIndex]
if len(instruction.Accounts) < 11 {
return nil, fmt.Errorf("accounts too short")
2025-12-26 10:57:37 +08:00
}
2025-12-30 11:03:11 +08:00
if len(instruction.Data) < 24 {
return nil, fmt.Errorf("data too short for sell args")
2025-12-26 10:57:37 +08:00
}
2025-12-30 11:03:11 +08:00
staticKeys := tx.Message.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[10]))
if err != nil {
return nil, err
2025-12-26 10:57:37 +08:00
}
2025-12-30 11:03:11 +08:00
user, err := getStaticKey(staticKeys, int(instruction.Accounts[0]))
if err != nil {
return nil, err
2025-12-26 10:57:37 +08:00
}
2025-12-30 11:03:11 +08:00
// in sell, sol amount is not directly provided, so we set it to 0
tokenAmount := binary.LittleEndian.Uint64(instruction.Data[19:25])
return &TxSignal{
TxHash: tx.Signatures[0].String(),
Maker: user.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: formatTokenAmount(tokenAmount),
Token1Amount: decimal.Zero,
Program: "PumpAMM",
Event: "sell",
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
Token0AmountUint64: tokenAmount,
Token1AmountUint64: 0,
}, nil
}
func parseQtkvBuy(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
instruction := tx.Message.Instructions[instructionIndex]
2025-12-26 10:57:37 +08:00
if len(instruction.Accounts) < 8 {
return nil, fmt.Errorf("accounts too short")
}
staticKeys := tx.Message.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[3]))
if err != nil {
return nil, err
}
user, err := getStaticKey(staticKeys, int(instruction.Accounts[0]))
if err != nil {
return nil, err
}
var args qtkvBuyArgs
if err := borsh.Deserialize(&args, instruction.Data[1:]); err != nil {
return nil, fmt.Errorf("failed to parse buy tokens args: %w", err)
}
2025-12-30 11:03:11 +08:00
return &TxSignal{
2025-12-26 10:57:37 +08:00
TxHash: tx.Signatures[0].String(),
Maker: user.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: formatTokenAmount(args.TokenNumber),
Token1Amount: formatSolAmount(args.SolAmount),
Program: "Pump",
Event: "buy",
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
Token0AmountUint64: args.TokenNumber,
Token1AmountUint64: args.SolAmount,
}, nil
}
2025-12-30 11:03:11 +08:00
func parseFjszInstruction(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
2025-12-26 10:57:37 +08:00
msg := tx.Message
if instructionIndex >= len(msg.Instructions) {
return nil, fmt.Errorf("instruction index out of bounds")
}
instruction := msg.Instructions[instructionIndex]
if len(instruction.Data) == 0 {
return nil, fmt.Errorf("data is empty")
}
2025-12-30 11:03:11 +08:00
if !matchMethod(instruction.Data, fjszBuyTokensIX) {
2025-12-26 10:57:37 +08:00
return nil, nil
}
if len(instruction.Accounts) < 7 {
return nil, fmt.Errorf("accounts too short")
}
if len(instruction.Data) < 16 {
return nil, fmt.Errorf("data too short for buy args")
}
staticKeys := tx.Message.StaticAccountKeys
mint, err := getStaticKey(staticKeys, int(instruction.Accounts[2]))
if err != nil {
return nil, err
}
user, err := getStaticKey(staticKeys, int(instruction.Accounts[6]))
if err != nil {
return nil, err
}
var args fjszBuyArgs
if err := borsh.Deserialize(&args, instruction.Data[8:]); err != nil {
return nil, fmt.Errorf("failed to parse buy tokens args: %w", err)
}
2025-12-30 11:03:11 +08:00
return &TxSignal{
2025-12-26 10:57:37 +08:00
TxHash: tx.Signatures[0].String(),
Maker: user.String(),
Token0Address: mint.String(),
Token1Address: wsolMint,
Token0Amount: formatTokenAmount(args.TokenAmount),
Token1Amount: formatSolAmount(args.SolAmount),
Program: "Pump",
Event: "buy",
IsToken2022: false,
IsMayhemMode: false,
Block: tx.Block,
Token0AmountUint64: args.TokenAmount,
Token1AmountUint64: args.SolAmount,
}, nil
}
2025-12-30 11:03:11 +08:00
func parseTerminalInstruction(tx *versionedTransaction, instructionIndex int) (*TxSignal, error) {
msg := tx.Message
if instructionIndex >= len(msg.Instructions) {
return nil, fmt.Errorf("instruction index out of bounds")
}
instruction := msg.Instructions[instructionIndex]
if len(instruction.Data) == 0 {
return nil, fmt.Errorf("data is empty")
}
if matchMethod(instruction.Data, terminalBuyTokensIX) {
return parseTermBuy(tx, &instruction)
} else if matchMethod(instruction.Data, terminalSellTokensIX) {
return parseTermSell(tx, &instruction)
} else if matchMethod(instruction.Data, terminalAmmSellTokensIX) {
return parseTermAmmSell(tx, &instruction)
}
return nil, nil
}
2025-12-26 10:57:37 +08:00
func resolveQuoteAccount(tx *versionedTransaction, quoteIndex int, expectedTableKeys []string, targetIndex uint8) (string, error) {
staticKeys := tx.Message.StaticAccountKeys
if quoteIndex < len(staticKeys) {
quoteKey := staticKeys[quoteIndex].String()
return quoteKey, nil
}
// attempt to load from address table lookup
if len(expectedTableKeys) == 0 || len(tx.Message.AddressTableLookups) != 1 {
return "", fmt.Errorf("parse quote from table lookup failed")
}
table := tx.Message.AddressTableLookups[0]
match := false
for _, key := range expectedTableKeys {
if table.AccountKey.String() == key {
match = true
break
}
}
if !match {
return "", fmt.Errorf("parse quote from table lookup failed")
}
indexOfTarget := indexOf(table.ReadonlyIndexes, targetIndex)
if indexOfTarget < 0 {
return "", fmt.Errorf("parse quote from table lookup failed")
}
expectedIndex := len(staticKeys) + len(table.WritableIndexes) + indexOfTarget
if quoteIndex != expectedIndex {
return "", fmt.Errorf("parse quote from table lookup failed")
}
return wsolMint, nil
}
func indexOf(haystack []uint8, needle uint8) int {
for i, v := range haystack {
if v == needle {
return i
}
}
return -1
}
2025-12-30 11:03:11 +08:00
func matchMethod(data []byte, methods []byte) bool {
if len(data) < len(methods) {
return false
}
return bytes.Equal(data[0:len(methods)], methods)
}