package main
import (
"context"
"fmt"
// directory of the generated code using the provided relay.proto file
pb "github.com/BlockRazorinc/relay_example/protobuf"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/rlp"
"google.golang.org/grpc"
)
// auth will be used to verify the credential
type Authentication struct {
apiKey string
}
func (a *Authentication) GetRequestMetadata(context.Context, ...string) (map[string]string, error) {
return map[string]string{"apiKey": a.apiKey}, nil
}
func (a *Authentication) RequireTransportSecurity() bool {
return false
}
func main() {
// BlockRazor relay endpoint address
blzrelayEndPoint := "ip:port"
// auth will be used to verify the credential
auth := Authentication{
"your auth token",
}
// open gRPC connection to BlockRazor relay
var err error
conn, err := grpc.Dial(blzrelayEndPoint, grpc.WithInsecure(), grpc.WithPerRPCCredentials(&auth), grpc.WithWriteBufferSize(0), grpc.WithInitialConnWindowSize(128*1024))
if err != nil {
fmt.Println("error: ", err)
return
}
// use the Gateway client connection interface
client := pb.NewGatewayClient(conn)
// create context and defer cancel of context
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
// create a subscription using the stream-specific method and request
stream, err := client.NewTxs(ctx, &pb.TxsRequest{NodeValidation: false})
if err != nil {
fmt.Println("failed to subscribe new tx: ", err)
return
}
for {
reply, err := stream.Recv()
if err != nil {
fmt.Println("stream receive error: ", err)
}
tx := &types.Transaction{}
err = rlp.DecodeBytes(reply.Tx.RawTx, tx)
if err != nil {
continue
}
fmt.Println("recieve new tx, tx hash is ", tx.Hash().String())
}
}