Skip to content

Commit 84f2f22

Browse files
authored
Merge pull request #313 from InjectiveLabs/feat/preserve-caller-defined-headers-in-execute-call
feat: preserve caller-defined headers in ExecuteCall
2 parents 678af51 + 6b69496 commit 84f2f22

File tree

2 files changed

+141
-4
lines changed

2 files changed

+141
-4
lines changed

client/common/api_request_assistant.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,18 @@ type APICall[Q any, R any] func(ctx context.Context, in *Q, opts ...grpc.CallOpt
1111
type APIStreamCall[Q any, S grpc.ClientStream] func(ctx context.Context, in *Q, opts ...grpc.CallOption) (S, error)
1212

1313
func ExecuteCall[Q any, R any](ctx context.Context, cookieAssistant CookieAssistant, call APICall[Q, R], in *Q) (*R, error) {
14-
var header metadata.MD
15-
localCtx := metadata.NewOutgoingContext(ctx, cookieAssistant.RealMetadata())
14+
md := cookieAssistant.RealMetadata()
15+
16+
if upstreamMetadata, ok := metadata.FromOutgoingContext(ctx); ok {
17+
// If metadata already exists in the context, merge it with the cookie metadata
18+
md = metadata.Join(md, upstreamMetadata)
19+
}
20+
21+
localCtx := metadata.NewOutgoingContext(ctx, md)
1622

17-
response, err := call(localCtx, in, grpc.Header(&header))
23+
response, err := call(localCtx, in, grpc.Header(&md))
1824

19-
cookieAssistant.ProcessResponseMetadata(header)
25+
cookieAssistant.ProcessResponseMetadata(md)
2026

2127
return response, err
2228
}
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
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

Comments
 (0)