2025-12-26 11:34:45 +08:00
|
|
|
package shreder
|
2025-12-26 10:57:37 +08:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
2025-12-30 11:03:11 +08:00
|
|
|
"log/slog"
|
2025-12-26 10:57:37 +08:00
|
|
|
|
|
|
|
|
"google.golang.org/grpc"
|
|
|
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
|
|
|
)
|
|
|
|
|
|
2025-12-30 11:03:11 +08:00
|
|
|
type Client struct {
|
|
|
|
|
log *slog.Logger
|
2025-12-26 10:57:37 +08:00
|
|
|
|
2025-12-26 11:34:45 +08:00
|
|
|
conn *grpc.ClientConn
|
2025-12-30 11:03:11 +08:00
|
|
|
client ShrederServiceClient
|
|
|
|
|
subscription map[string]*SubscribeRequestFilterTransactions
|
2025-12-26 10:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
2025-12-26 11:34:45 +08:00
|
|
|
func NewShrederClient(
|
|
|
|
|
url string,
|
2025-12-30 11:03:11 +08:00
|
|
|
subscription map[string]*SubscribeRequestFilterTransactions,
|
|
|
|
|
) (*Client, func(), error) {
|
2025-12-26 10:57:37 +08:00
|
|
|
conn, err := grpc.NewClient(url, grpc.WithTransportCredentials(insecure.NewCredentials()))
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, func() {}, err
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 11:03:11 +08:00
|
|
|
logger := slog.Default()
|
|
|
|
|
s := &Client{
|
2025-12-26 11:34:45 +08:00
|
|
|
log: logger,
|
|
|
|
|
conn: conn,
|
2025-12-30 11:03:11 +08:00
|
|
|
client: NewShrederServiceClient(conn),
|
2025-12-26 11:34:45 +08:00
|
|
|
subscription: subscription,
|
2025-12-26 10:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return s, func() {
|
|
|
|
|
s.Wait()
|
|
|
|
|
}, nil
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 11:03:11 +08:00
|
|
|
func (c *Client) Wait() {
|
2025-12-26 10:57:37 +08:00
|
|
|
c.log.Debug("waiting for shreder client to stop")
|
|
|
|
|
|
|
|
|
|
err := c.conn.Close()
|
|
|
|
|
if err != nil {
|
2025-12-30 11:03:11 +08:00
|
|
|
c.log.Error("failed to close connection: ", "err", err)
|
2025-12-26 10:57:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
c.log.Debug("shreder client stopped")
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 11:03:11 +08:00
|
|
|
func (c *Client) ReadSync(ctx context.Context, txCh chan<- TxSignalBatch) error {
|
2025-12-26 10:57:37 +08:00
|
|
|
stream, err := c.client.SubscribeTransactions(ctx)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 11:03:11 +08:00
|
|
|
err = stream.Send(&SubscribeTransactionsRequest{
|
2025-12-26 11:34:45 +08:00
|
|
|
Transactions: c.subscription,
|
2025-12-26 10:57:37 +08:00
|
|
|
})
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for {
|
|
|
|
|
response, err := stream.Recv()
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-30 11:03:11 +08:00
|
|
|
txBatch := ParseTransaction(response.Transaction)
|
2025-12-26 10:57:37 +08:00
|
|
|
if len(txBatch) == 0 {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// set fixed source for tx signals
|
|
|
|
|
for _, tx := range txBatch {
|
|
|
|
|
tx.Source = "shreder"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
select {
|
|
|
|
|
case <-ctx.Done():
|
|
|
|
|
return ctx.Err()
|
|
|
|
|
case txCh <- txBatch:
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|