github.com/InjectiveLabs/sdk-go@v1.53.0/examples/exchange/spot/10_StreamTrades/example.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	"github.com/InjectiveLabs/sdk-go/client/common"
     9  	exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange"
    10  	spotExchangePB "github.com/InjectiveLabs/sdk-go/exchange/spot_exchange_rpc/pb"
    11  )
    12  
    13  func main() {
    14  	// network := common.LoadNetwork("mainnet", "k8s")
    15  	network := common.LoadNetwork("testnet", "lb")
    16  	exchangeClient, err := exchangeclient.NewExchangeClient(network)
    17  	if err != nil {
    18  		panic(err)
    19  	}
    20  
    21  	ctx := context.Background()
    22  	marketId := "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
    23  	subaccountId := "0xc6fe5d33615a1c52c08018c47e8bc53646a0e101000000000000000000000000"
    24  
    25  	req := spotExchangePB.StreamTradesRequest{
    26  		MarketId:     marketId,
    27  		SubaccountId: subaccountId,
    28  	}
    29  	stream, err := exchangeClient.StreamSpotTrades(ctx, &req)
    30  	if err != nil {
    31  		panic(err)
    32  	}
    33  
    34  	for {
    35  		select {
    36  		case <-ctx.Done():
    37  			return
    38  		default:
    39  			res, err := stream.Recv()
    40  			if err != nil {
    41  				fmt.Println(err)
    42  				return
    43  			}
    44  			str, _ := json.MarshalIndent(res, "", " ")
    45  			fmt.Print(string(str))
    46  		}
    47  	}
    48  }