From 99010ca601d6baf5899057cdf4c482cca7e75e08 Mon Sep 17 00:00:00 2001 From: samlior Date: Fri, 26 Dec 2025 11:34:45 +0800 Subject: [PATCH] chore: add shrederClient exaple --- cmd/shreder/main.go | 73 ++++++++++++++++++++++++++++++++++- pkg/logger/logger.go | 22 +++++++++++ pkg/shreder/shreder_client.go | 43 +++++++-------------- pkg/types/tx_signal.go | 37 +++++++++--------- 4 files changed, 126 insertions(+), 49 deletions(-) diff --git a/cmd/shreder/main.go b/cmd/shreder/main.go index a3dd973..3bac05a 100644 --- a/cmd/shreder/main.go +++ b/cmd/shreder/main.go @@ -1,7 +1,76 @@ package main -import "fmt" +import ( + "context" + "encoding/json" + "errors" + "fmt" + "os" + "os/signal" + "syscall" + + "github.com/samlior/libsam/pkg/logger" + "github.com/samlior/libsam/pkg/shreder" + "github.com/samlior/libsam/pkg/types" + "github.com/samlior/libsam/third_party/shreder_protos" +) func main() { - fmt.Println("Hello, World!") + url := os.Getenv("URL") + if url == "" { + panic("URL is not set") + } + + logger := logger.NewEmptyLogger() + shrederClient, cleanup, err := shreder.NewShrederClient( + logger, + url, + map[string]*shreder_protos.SubscribeRequestFilterTransactions{ + "pumpfunamm": { + AccountRequired: []string{ + "pAMMBay6oceH9fJKBRHGP5D4bD4sWpmSwMn52FMfXEA", + }, + }, + "photon": { + AccountRequired: []string{ + "BSfD6SHZigAfDWSjzD5Q41jw8LmKwtmjskPH9XW1mrRW", + }, + }, + // TODO: axiom, gmgn, etc. + }) + if err != nil { + panic(err) + } + defer cleanup() + + txCh := make(chan types.TxSignalBatch, 1000) + + exitSignal := make(chan os.Signal, 1) + ctx, cancel := context.WithCancel(context.Background()) + signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM) + + go func() { + <-exitSignal + cancel() + }() + + // async read from shreder + go func() { + err := shrederClient.ReadSync(ctx, txCh) + if err != nil { + if !errors.Is(err, context.Canceled) { + panic(err) + } + } + }() + + for { + select { + case <-ctx.Done(): + return + case txBatch := <-txCh: + jsonData, _ := json.MarshalIndent(txBatch, "", " ") + fmt.Println(string(jsonData)) + } + } } diff --git a/pkg/logger/logger.go b/pkg/logger/logger.go index 5876ab3..88bc45a 100644 --- a/pkg/logger/logger.go +++ b/pkg/logger/logger.go @@ -16,3 +16,25 @@ type Logger interface { Fatal(a ...interface{}) Fatalf(format string, a ...interface{}) } + +type EmptyLogger struct { +} + +func NewEmptyLogger() Logger { + return &EmptyLogger{} +} + +func (l *EmptyLogger) Info(a ...interface{}) {} +func (l *EmptyLogger) Infof(format string, a ...interface{}) {} + +func (l *EmptyLogger) Error(a ...interface{}) {} +func (l *EmptyLogger) Errorf(format string, a ...interface{}) {} + +func (l *EmptyLogger) Debug(a ...interface{}) {} +func (l *EmptyLogger) Debugf(format string, a ...interface{}) {} + +func (l *EmptyLogger) Warn(a ...interface{}) {} +func (l *EmptyLogger) Warnf(format string, a ...interface{}) {} + +func (l *EmptyLogger) Fatal(a ...interface{}) {} +func (l *EmptyLogger) Fatalf(format string, a ...interface{}) {} diff --git a/pkg/shreder/shreder_client.go b/pkg/shreder/shreder_client.go index 5dea9ce..efc1ceb 100644 --- a/pkg/shreder/shreder_client.go +++ b/pkg/shreder/shreder_client.go @@ -1,4 +1,4 @@ -package shreder_client +package shreder import ( "context" @@ -14,20 +14,26 @@ import ( type ShrederClient struct { log logger.Logger - conn *grpc.ClientConn - client shreder_protos.ShrederServiceClient + conn *grpc.ClientConn + client shreder_protos.ShrederServiceClient + subscription map[string]*shreder_protos.SubscribeRequestFilterTransactions } -func NewShrederClient(logger logger.Logger, url string) (*ShrederClient, func(), error) { +func NewShrederClient( + logger logger.Logger, + url string, + subscription map[string]*shreder_protos.SubscribeRequestFilterTransactions, +) (*ShrederClient, func(), error) { conn, err := grpc.NewClient(url, grpc.WithTransportCredentials(insecure.NewCredentials())) if err != nil { return nil, func() {}, err } s := &ShrederClient{ - log: logger, - conn: conn, - client: shreder_protos.NewShrederServiceClient(conn), + log: logger, + conn: conn, + client: shreder_protos.NewShrederServiceClient(conn), + subscription: subscription, } return s, func() { @@ -53,28 +59,7 @@ func (c *ShrederClient) ReadSync(ctx context.Context, txCh chan<- types.TxSignal } err = stream.Send(&shreder_protos.SubscribeTransactionsRequest{ - Transactions: map[string]*shreder_protos.SubscribeRequestFilterTransactions{ - "pumpfun": { - AccountRequired: []string{ - "6EF8rrecthR5Dkzon8Nwu78hRvfCKubJ14M5uBEwF6P", - }, - }, - "axiom": { - AccountRequired: []string{ - "F5tfvbLog9VdGUPqBDTT8rgXvTTcq7e5UiGnupL1zvBq", - }, - }, - "photon": { - AccountRequired: []string{ - "BSfD6SHZigAfDWSjzD5Q41jw8LmKwtmjskPH9XW1mrRW", - }, - }, - "fjsz": { - AccountRequired: []string{ - "FJsZbftBqRLfF7uqUKpm4s2goDr6xsQ5Q3mN7AFJB6hK", - }, - }, - }, + Transactions: c.subscription, }) if err != nil { return err diff --git a/pkg/types/tx_signal.go b/pkg/types/tx_signal.go index d126396..4bfc8e3 100644 --- a/pkg/types/tx_signal.go +++ b/pkg/types/tx_signal.go @@ -8,25 +8,26 @@ import ( ) type TxSignal struct { - Source string - TxHash string - Maker string - Token0Address string - Token1Address string - Token0Amount decimal.Decimal - Token1Amount decimal.Decimal - Block uint64 - BlockAt time.Time - BlockIndex uint64 - Event string - Program string - IsProcessed bool - IsToken2022 bool - IsMayhemMode bool - TxFee decimal.Decimal + Source string `json:"source"` + TxHash string `json:"tx_hash"` + Maker string `json:"maker"` + Token0Address string `json:"token0_address"` + Token1Address string `json:"token1_address"` + Token0Amount decimal.Decimal `json:"token0_amount"` + Token1Amount decimal.Decimal `json:"token1_amount"` + Block uint64 `json:"block"` + BlockAt time.Time `json:"block_at"` + BlockIndex uint64 `json:"block_index"` + Event string `json:"event"` + Program string `json:"program"` + IsProcessed bool `json:"is_processed"` + IsToken2022 bool `json:"is_token2022"` + IsMayhemMode bool `json:"is_mayhem_mode"` + TxFee decimal.Decimal `json:"tx_fee"` - Token0AmountUint64 uint64 - Token1AmountUint64 uint64 + // parsed values + Token0AmountUint64 uint64 `json:"-"` + Token1AmountUint64 uint64 `json:"-"` } func (t *TxSignal) Parse() *TxSignal {