github.com/InjectiveLabs/sdk-go@v1.53.0/examples/chain/exchange/query/14_SpotOrderbook/example.go (about)

     1  package main
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	"cosmossdk.io/math"
     9  
    10  	"os"
    11  
    12  	"github.com/InjectiveLabs/sdk-go/chain/exchange/types"
    13  
    14  	"github.com/InjectiveLabs/sdk-go/client"
    15  	chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
    16  	"github.com/InjectiveLabs/sdk-go/client/common"
    17  	rpchttp "github.com/cometbft/cometbft/rpc/client/http"
    18  )
    19  
    20  func main() {
    21  	network := common.LoadNetwork("testnet", "lb")
    22  	tmClient, err := rpchttp.New(network.TmEndpoint, "/websocket")
    23  	if err != nil {
    24  		panic(err)
    25  	}
    26  
    27  	senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring(
    28  		os.Getenv("HOME")+"/.injectived",
    29  		"injectived",
    30  		"file",
    31  		"inj-user",
    32  		"12345678",
    33  		"5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided
    34  		false,
    35  	)
    36  
    37  	if err != nil {
    38  		panic(err)
    39  	}
    40  
    41  	clientCtx, err := chainclient.NewClientContext(
    42  		network.ChainId,
    43  		senderAddress.String(),
    44  		cosmosKeyring,
    45  	)
    46  
    47  	if err != nil {
    48  		panic(err)
    49  	}
    50  
    51  	clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient)
    52  
    53  	chainClient, err := chainclient.NewChainClient(
    54  		clientCtx,
    55  		network,
    56  		common.OptionGasPrices(client.DefaultGasPriceWithDenom),
    57  	)
    58  
    59  	if err != nil {
    60  		panic(err)
    61  	}
    62  
    63  	ctx := context.Background()
    64  
    65  	marketId := "0x0611780ba69656949525013d947713300f56c37b6175e02f26bffa495c3208fe"
    66  	limit := uint64(2)
    67  	orderSide := types.OrderSide_Buy
    68  	limitCumulativeNotional := math.LegacyDec{}
    69  	limitCumulativeQuantity := math.LegacyDec{}
    70  
    71  	res, err := chainClient.FetchChainSpotOrderbook(ctx, marketId, limit, orderSide, limitCumulativeNotional, limitCumulativeQuantity)
    72  	if err != nil {
    73  		fmt.Println(err)
    74  	}
    75  
    76  	str, _ := json.MarshalIndent(res, "", " ")
    77  	fmt.Print(string(str))
    78  
    79  }