|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "os" |
| 8 | + "strconv" |
| 9 | + |
| 10 | + "google.golang.org/grpc/metadata" |
| 11 | + |
| 12 | + grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" |
| 13 | + |
| 14 | + rpchttp "github.com/cometbft/cometbft/rpc/client/http" |
| 15 | + |
| 16 | + "github.com/InjectiveLabs/sdk-go/chain/exchange/types" |
| 17 | + "github.com/InjectiveLabs/sdk-go/client" |
| 18 | + chainclient "github.com/InjectiveLabs/sdk-go/client/chain" |
| 19 | + "github.com/InjectiveLabs/sdk-go/client/common" |
| 20 | + tmclient "github.com/InjectiveLabs/sdk-go/client/tm" |
| 21 | +) |
| 22 | + |
| 23 | +func main() { |
| 24 | + network := common.LoadNetwork("testnet", "lb") |
| 25 | + tmClient, err := rpchttp.New(network.TmEndpoint) |
| 26 | + if err != nil { |
| 27 | + panic(err) |
| 28 | + } |
| 29 | + |
| 30 | + senderAddress, cosmosKeyring, err := chainclient.InitCosmosKeyring( |
| 31 | + os.Getenv("HOME")+"/.injectived", |
| 32 | + "injectived", |
| 33 | + "file", |
| 34 | + "inj-user", |
| 35 | + "12345678", |
| 36 | + "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided |
| 37 | + false, |
| 38 | + ) |
| 39 | + |
| 40 | + if err != nil { |
| 41 | + panic(err) |
| 42 | + } |
| 43 | + |
| 44 | + clientCtx, err := chainclient.NewClientContext( |
| 45 | + network.ChainId, |
| 46 | + senderAddress.String(), |
| 47 | + cosmosKeyring, |
| 48 | + ) |
| 49 | + |
| 50 | + if err != nil { |
| 51 | + panic(err) |
| 52 | + } |
| 53 | + |
| 54 | + clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) |
| 55 | + |
| 56 | + chainClient, err := chainclient.NewChainClient( |
| 57 | + clientCtx, |
| 58 | + network, |
| 59 | + common.OptionGasPrices(client.DefaultGasPriceWithDenom), |
| 60 | + ) |
| 61 | + |
| 62 | + if err != nil { |
| 63 | + panic(err) |
| 64 | + } |
| 65 | + |
| 66 | + status := "Active" |
| 67 | + marketIds := []string{"0x0611780ba69656949525013d947713300f56c37b6175e02f26bffa495c3208fe"} |
| 68 | + |
| 69 | + rpcClient := tmclient.NewRPCClient(network.TmEndpoint) |
| 70 | + latestBlockHeight, err := rpcClient.GetLatestBlockHeight(context.Background()) |
| 71 | + if err != nil { |
| 72 | + panic(err) |
| 73 | + } |
| 74 | + |
| 75 | + { |
| 76 | + res, err := fetchSpotMarketsAtHeight( |
| 77 | + chainClient, |
| 78 | + latestBlockHeight, |
| 79 | + status, |
| 80 | + marketIds, |
| 81 | + ) |
| 82 | + if err != nil { |
| 83 | + panic(err) |
| 84 | + } |
| 85 | + str, _ := json.MarshalIndent(res, "", " ") |
| 86 | + fmt.Println(string(str)) |
| 87 | + } |
| 88 | + |
| 89 | + { |
| 90 | + res, err := fetchSpotMarketsAtHeight( |
| 91 | + chainClient, |
| 92 | + latestBlockHeight-1, |
| 93 | + status, |
| 94 | + marketIds, |
| 95 | + ) |
| 96 | + if err != nil { |
| 97 | + panic(err) |
| 98 | + } |
| 99 | + str, _ := json.MarshalIndent(res, "", " ") |
| 100 | + fmt.Println(string(str)) |
| 101 | + } |
| 102 | + |
| 103 | + { |
| 104 | + _, err := fetchSpotMarketsAtHeight( |
| 105 | + chainClient, |
| 106 | + 10, |
| 107 | + status, |
| 108 | + marketIds, |
| 109 | + ) |
| 110 | + if err == nil { |
| 111 | + panic("Expected error for old block height") |
| 112 | + } |
| 113 | + fmt.Println("Expected error for old block height:", err) |
| 114 | + } |
| 115 | +} |
| 116 | + |
| 117 | +func fetchSpotMarketsAtHeight( |
| 118 | + chainClient chainclient.ChainClient, |
| 119 | + height int64, |
| 120 | + status string, |
| 121 | + marketIds []string, |
| 122 | +) (*types.QuerySpotMarketsResponse, error) { |
| 123 | + ctx := context.Background() |
| 124 | + |
| 125 | + ctxWithHeight := metadata.AppendToOutgoingContext( |
| 126 | + ctx, |
| 127 | + grpctypes.GRPCBlockHeightHeader, strconv.FormatInt(height, 10), |
| 128 | + ) |
| 129 | + |
| 130 | + return chainClient.FetchChainSpotMarkets(ctxWithHeight, status, marketIds) |
| 131 | +} |
0 commit comments