Files
pump-parser/internal/example/geyser/pump.go
2025-12-15 15:14:14 +08:00

63 lines
1.5 KiB
Go

package geyser
import (
"fmt"
"github.com/shopspring/decimal"
types "github.com/thloyi/pump-parser"
)
type PumpHandler struct {
callback func(*types.Tx)
}
func NewPumpHandler(cb func(*types.Tx)) *PumpHandler {
return &PumpHandler{
callback: func(tx *types.Tx) {
//tx.Check(tx2)
cb(tx)
},
}
}
func (h *PumpHandler) HandleMessage(rawTx *types.RawTx) {
if rawTx.Meta.Err != nil {
// Notify the channel about the failed transaction
beforeSolBalance := decimal.Zero
afterSolBalance := decimal.Zero
if rawTx.Meta.PreBalances != nil && len(rawTx.Meta.PreBalances) > 0 {
beforeSolBalance = decimal.NewFromUint64(rawTx.Meta.PreBalances[0]).Div(decimal.NewFromInt(1e9))
}
if rawTx.Meta.PostBalances != nil && len(rawTx.Meta.PostBalances) > 0 {
afterSolBalance = decimal.NewFromUint64(rawTx.Meta.PostBalances[0]).Div(decimal.NewFromInt(1e9))
}
h.callback(&types.Tx{
TxHash: (*[64]byte)((rawTx.Transaction.Signatures[0][:])),
Err: rawTx.Meta.Err,
Signer: rawTx.GetSigner(),
Block: rawTx.Slot,
BlockIndex: uint64(rawTx.IndexWithinBlock),
BeforeSolBalance: beforeSolBalance,
AfterSOLBalance: afterSolBalance,
})
return
}
var parsedTx = &types.Tx{}
parsedTx.SetRawTx(rawTx)
err := parsedTx.Parser()
if err != nil {
fmt.Printf("parser error: %s, block: %d tx: %s\n", err, rawTx.Slot, rawTx.TxHash())
return
}
if len(parsedTx.Swaps) == 0 {
// no swap, ignore
return
}
// fmt.Println(parsedTx.GetTxHash(), len(parsedTx.Swaps))
if h.callback != nil {
h.callback(parsedTx)
}
}