|
| 1 | +package tychoclient |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "crypto/tls" |
| 6 | + "fmt" |
| 7 | + "time" |
| 8 | + |
| 9 | + "google.golang.org/grpc" |
| 10 | + "google.golang.org/grpc/credentials" |
| 11 | + |
| 12 | + "github.com/tonkeeper/tongo/tychoclient/proto" |
| 13 | +) |
| 14 | + |
| 15 | +const ( |
| 16 | + // DefaultEndpoint is the testnet endpoint provided by the team |
| 17 | + DefaultEndpoint = "tonapi-testnet.tychoprotocol.com:443" |
| 18 | + |
| 19 | + // DefaultTimeout for gRPC calls |
| 20 | + DefaultTimeout = 30 * time.Second |
| 21 | +) |
| 22 | + |
| 23 | +// Client provides access to Tycho blockchain data via gRPC |
| 24 | +type Client struct { |
| 25 | + conn *grpc.ClientConn |
| 26 | + client proto.TychoIndexerClient |
| 27 | +} |
| 28 | + |
| 29 | +// NewClient creates a new Tycho client with default settings |
| 30 | +func NewClient() (*Client, error) { |
| 31 | + return NewClientWithEndpoint(DefaultEndpoint) |
| 32 | +} |
| 33 | + |
| 34 | +// NewClientWithEndpoint creates a new Tycho client with custom endpoint |
| 35 | +func NewClientWithEndpoint(endpoint string) (*Client, error) { |
| 36 | + // Use TLS for secure connection |
| 37 | + creds := credentials.NewTLS(&tls.Config{}) |
| 38 | + |
| 39 | + conn, err := grpc.Dial(endpoint, grpc.WithTransportCredentials(creds)) |
| 40 | + if err != nil { |
| 41 | + return nil, fmt.Errorf("failed to connect to %s: %w", endpoint, err) |
| 42 | + } |
| 43 | + |
| 44 | + return &Client{ |
| 45 | + conn: conn, |
| 46 | + client: proto.NewTychoIndexerClient(conn), |
| 47 | + }, nil |
| 48 | +} |
| 49 | + |
| 50 | +// Close closes the gRPC connection |
| 51 | +func (c *Client) Close() error { |
| 52 | + return c.conn.Close() |
| 53 | +} |
| 54 | + |
| 55 | +// GetStatus returns the current status of the Tycho node |
| 56 | +func (c *Client) GetStatus(ctx context.Context) (*proto.GetStatusResponse, error) { |
| 57 | + ctx, cancel := context.WithTimeout(ctx, DefaultTimeout) |
| 58 | + defer cancel() |
| 59 | + |
| 60 | + return c.client.GetStatus(ctx, &proto.GetStatusRequest{}) |
| 61 | +} |
| 62 | + |
| 63 | +// GetLibraryCell fetches a library cell by hash |
| 64 | +func (c *Client) GetLibraryCell(ctx context.Context, hash []byte) ([]byte, error) { |
| 65 | + ctx, cancel := context.WithTimeout(ctx, DefaultTimeout) |
| 66 | + defer cancel() |
| 67 | + |
| 68 | + req := &proto.GetLibraryCellRequest{ |
| 69 | + Hash: hash, |
| 70 | + } |
| 71 | + |
| 72 | + resp, err := c.client.GetLibraryCell(ctx, req) |
| 73 | + if err != nil { |
| 74 | + return nil, fmt.Errorf("failed to get library cell: %w", err) |
| 75 | + } |
| 76 | + |
| 77 | + switch result := resp.Library.(type) { |
| 78 | + case *proto.GetLibraryCellResponse_NotFound: |
| 79 | + return nil, fmt.Errorf("library cell not found") |
| 80 | + case *proto.GetLibraryCellResponse_Found: |
| 81 | + return result.Found.Cell, nil |
| 82 | + default: |
| 83 | + return nil, fmt.Errorf("unexpected response type") |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +// GetRawBlockData fetches raw BOC data for debugging purposes |
| 88 | +func (c *Client) GetRawBlockData(ctx context.Context, workchain int32, shard uint64, seqno uint32) ([]byte, error) { |
| 89 | + ctx, cancel := context.WithTimeout(ctx, DefaultTimeout) |
| 90 | + defer cancel() |
| 91 | + |
| 92 | + req := &proto.GetBlockRequest{ |
| 93 | + Query: &proto.GetBlockRequest_BySeqno{ |
| 94 | + BySeqno: &proto.BlockBySeqno{ |
| 95 | + Workchain: workchain, |
| 96 | + Shard: shard, |
| 97 | + Seqno: seqno, |
| 98 | + }, |
| 99 | + }, |
| 100 | + } |
| 101 | + |
| 102 | + stream, err := c.client.GetBlock(ctx, req) |
| 103 | + if err != nil { |
| 104 | + return nil, fmt.Errorf("failed to start block stream: %w", err) |
| 105 | + } |
| 106 | + |
| 107 | + return c.readBlockFromStream(stream) |
| 108 | +} |
| 109 | + |
| 110 | +// readBlockFromStream reads block data from the gRPC stream |
| 111 | +func (c *Client) readBlockFromStream(stream proto.TychoIndexer_GetBlockClient) ([]byte, error) { |
| 112 | + var totalData []byte |
| 113 | + |
| 114 | + for { |
| 115 | + resp, err := stream.Recv() |
| 116 | + if err != nil { |
| 117 | + if err.Error() == "EOF" { |
| 118 | + break |
| 119 | + } |
| 120 | + return nil, fmt.Errorf("stream error: %w", err) |
| 121 | + } |
| 122 | + |
| 123 | + switch msg := resp.Msg.(type) { |
| 124 | + case *proto.GetBlockResponse_NotFound: |
| 125 | + return nil, fmt.Errorf("block not found") |
| 126 | + case *proto.GetBlockResponse_Found: |
| 127 | + // First chunk with metadata |
| 128 | + if msg.Found.FirstChunk != nil { |
| 129 | + totalData = append(totalData, msg.Found.FirstChunk.Data...) |
| 130 | + } |
| 131 | + case *proto.GetBlockResponse_Chunk: |
| 132 | + // Subsequent chunks |
| 133 | + totalData = append(totalData, msg.Chunk.Data...) |
| 134 | + default: |
| 135 | + return nil, fmt.Errorf("unexpected response type: %T", msg) |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + if len(totalData) == 0 { |
| 140 | + return nil, fmt.Errorf("no block data received") |
| 141 | + } |
| 142 | + |
| 143 | + return totalData, nil |
| 144 | +} |
0 commit comments