Files
pump-parser/example/geyser/pump.go

63 lines
1.5 KiB
Go
Raw Normal View History

2025-11-20 17:56:45 +08:00
package geyser
import (
"fmt"
"github.com/shopspring/decimal"
types "github.com/thloyi/pump-parser"
)
type PumpHandler struct {
2025-11-21 12:01:44 +08:00
callback func(*types.Tx)
2025-11-20 17:56:45 +08:00
}
2025-11-21 12:01:44 +08:00
func NewPumpHandler(cb func(*types.Tx)) *PumpHandler {
2025-11-20 17:56:45 +08:00
return &PumpHandler{
2025-11-21 12:01:44 +08:00
callback: func(tx *types.Tx) {
2025-11-20 17:56:45 +08:00
//tx.Check(tx2)
2025-11-21 12:01:44 +08:00
cb(tx)
2025-11-20 17:56:45 +08:00
},
}
}
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,
2025-11-21 12:01:44 +08:00
})
2025-11-20 17:56:45 +08:00
return
}
2025-11-21 12:01:44 +08:00
var parsedTx = &types.Tx{}
parsedTx.SetRawTx(rawTx)
err := parsedTx.Parser()
2025-11-20 17:56:45 +08:00
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 {
2025-11-21 12:01:44 +08:00
h.callback(parsedTx)
2025-11-20 17:56:45 +08:00
}
}