chore: add shrederClient exaple

This commit is contained in:
2025-12-26 11:34:45 +08:00
parent 0d2e29cacf
commit 99010ca601
4 changed files with 126 additions and 49 deletions

View File

@@ -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))
}
}
}

View File

@@ -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{}) {}

View File

@@ -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

View File

@@ -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 {