Skip to content

Commit 2a7357b

Browse files
authored
Merge pull request #321 from InjectiveLabs/cp-502/fix_chain_responses_decoding
[CP-502] fix chain responses decoding
2 parents 7dfdce0 + 19f96de commit 2a7357b

File tree

10 files changed

+114
-82
lines changed

10 files changed

+114
-82
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
.DS_Store
22
.idea/
3+
.vscode/
34
.chain_cookie
45
.exchange_cookie
56
coverage.out

client/chain/chain.go

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
sdkclient "github.com/cosmos/cosmos-sdk/client"
2121
"github.com/cosmos/cosmos-sdk/client/grpc/cmtservice"
2222
"github.com/cosmos/cosmos-sdk/client/tx"
23+
"github.com/cosmos/cosmos-sdk/codec"
2324
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
2425
sdk "github.com/cosmos/cosmos-sdk/types"
2526
"github.com/cosmos/cosmos-sdk/types/query"
@@ -397,13 +398,28 @@ func NewChainClient(
397398
}
398399

399400
// init grpc connection
401+
protoCodec, ok := ctx.Codec.(*codec.ProtoCodec)
402+
if !ok {
403+
return nil, errors.New("codec is not a proto codec")
404+
}
405+
400406
var conn *grpc.ClientConn
401407
var err error
402408
stickySessionEnabled := true
403409
if opts.TLSCert != nil {
404-
conn, err = grpc.NewClient(network.ChainGrpcEndpoint, grpc.WithTransportCredentials(opts.TLSCert), grpc.WithContextDialer(common.DialerFunc))
410+
conn, err = grpc.NewClient(
411+
network.ChainGrpcEndpoint,
412+
grpc.WithTransportCredentials(opts.TLSCert),
413+
grpc.WithContextDialer(common.DialerFunc),
414+
grpc.WithDefaultCallOptions(grpc.ForceCodec(protoCodec.GRPCCodec())),
415+
)
405416
} else {
406-
conn, err = grpc.NewClient(network.ChainGrpcEndpoint, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(common.DialerFunc))
417+
conn, err = grpc.NewClient(
418+
network.ChainGrpcEndpoint,
419+
grpc.WithTransportCredentials(insecure.NewCredentials()),
420+
grpc.WithContextDialer(common.DialerFunc),
421+
grpc.WithDefaultCallOptions(grpc.ForceCodec(protoCodec.GRPCCodec())),
422+
)
407423
stickySessionEnabled = false
408424
}
409425
if err != nil {
@@ -413,9 +429,19 @@ func NewChainClient(
413429

414430
var chainStreamConn *grpc.ClientConn
415431
if opts.TLSCert != nil {
416-
chainStreamConn, err = grpc.NewClient(network.ChainStreamGrpcEndpoint, grpc.WithTransportCredentials(opts.TLSCert), grpc.WithContextDialer(common.DialerFunc))
432+
chainStreamConn, err = grpc.NewClient(
433+
network.ChainStreamGrpcEndpoint,
434+
grpc.WithTransportCredentials(opts.TLSCert),
435+
grpc.WithContextDialer(common.DialerFunc),
436+
grpc.WithDefaultCallOptions(grpc.ForceCodec(protoCodec.GRPCCodec())),
437+
)
417438
} else {
418-
chainStreamConn, err = grpc.NewClient(network.ChainStreamGrpcEndpoint, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(common.DialerFunc))
439+
chainStreamConn, err = grpc.NewClient(
440+
network.ChainStreamGrpcEndpoint,
441+
grpc.WithTransportCredentials(insecure.NewCredentials()),
442+
grpc.WithContextDialer(common.DialerFunc),
443+
grpc.WithDefaultCallOptions(grpc.ForceCodec(protoCodec.GRPCCodec())),
444+
)
419445
}
420446
if err != nil {
421447
err = errors.Wrapf(err, "failed to connect to the chain stream gRPC: %s", network.ChainStreamGrpcEndpoint)
@@ -953,7 +979,7 @@ func (c *chainClient) runBatchBroadcast() {
953979
}
954980

955981
func (c *chainClient) GetGasFee() (string, error) {
956-
gasPrices := strings.Trim(c.opts.GasPrices, "inj")
982+
gasPrices := strings.TrimSuffix(c.txFactory.GasPrices().String(), client.InjDenom)
957983

958984
gas, err := strconv.ParseFloat(gasPrices, 64)
959985

client/chain/chain_v2.go

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
sdkclient "github.com/cosmos/cosmos-sdk/client"
2121
"github.com/cosmos/cosmos-sdk/client/grpc/cmtservice"
2222
"github.com/cosmos/cosmos-sdk/client/tx"
23+
"github.com/cosmos/cosmos-sdk/codec"
2324
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
2425
sdk "github.com/cosmos/cosmos-sdk/types"
2526
"github.com/cosmos/cosmos-sdk/types/query"
@@ -42,10 +43,8 @@ import (
4243

4344
erc20types "github.com/InjectiveLabs/sdk-go/chain/erc20/types"
4445
evmtypes "github.com/InjectiveLabs/sdk-go/chain/evm/types"
45-
exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
4646
exchangev2types "github.com/InjectiveLabs/sdk-go/chain/exchange/types/v2"
4747
permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/types"
48-
chainstreamtypes "github.com/InjectiveLabs/sdk-go/chain/stream/types"
4948
chainstreamv2types "github.com/InjectiveLabs/sdk-go/chain/stream/types/v2"
5049
tokenfactorytypes "github.com/InjectiveLabs/sdk-go/chain/tokenfactory/types"
5150
txfeestypes "github.com/InjectiveLabs/sdk-go/chain/txfees/types"
@@ -342,12 +341,10 @@ type chainClientV2 struct {
342341
authQueryClient authtypes.QueryClient
343342
authzQueryClient authztypes.QueryClient
344343
bankQueryClient banktypes.QueryClient
345-
chainStreamClient chainstreamtypes.StreamClient
346344
chainStreamV2Client chainstreamv2types.StreamClient
347345
distributionQueryClient distributiontypes.QueryClient
348346
erc20QueryClient erc20types.QueryClient
349347
evmQueryClient evmtypes.QueryClient
350-
exchangeQueryClient exchangetypes.QueryClient
351348
exchangeV2QueryClient exchangev2types.QueryClient
352349
ibcChannelQueryClient ibcchanneltypes.QueryClient
353350
ibcClientQueryClient ibcclienttypes.QueryClient
@@ -396,13 +393,28 @@ func NewChainClientV2(
396393
}
397394

398395
// init grpc connection
396+
protoCodec, ok := ctx.Codec.(*codec.ProtoCodec)
397+
if !ok {
398+
return nil, errors.New("codec is not a proto codec")
399+
}
400+
399401
var conn *grpc.ClientConn
400402
var err error
401403
stickySessionEnabled := true
402404
if opts.TLSCert != nil {
403-
conn, err = grpc.NewClient(network.ChainGrpcEndpoint, grpc.WithTransportCredentials(opts.TLSCert), grpc.WithContextDialer(common.DialerFunc))
405+
conn, err = grpc.NewClient(
406+
network.ChainGrpcEndpoint,
407+
grpc.WithTransportCredentials(opts.TLSCert),
408+
grpc.WithContextDialer(common.DialerFunc),
409+
grpc.WithDefaultCallOptions(grpc.ForceCodec(protoCodec.GRPCCodec())),
410+
)
404411
} else {
405-
conn, err = grpc.NewClient(network.ChainGrpcEndpoint, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(common.DialerFunc))
412+
conn, err = grpc.NewClient(
413+
network.ChainGrpcEndpoint,
414+
grpc.WithTransportCredentials(insecure.NewCredentials()),
415+
grpc.WithContextDialer(common.DialerFunc),
416+
grpc.WithDefaultCallOptions(grpc.ForceCodec(protoCodec.GRPCCodec())),
417+
)
406418
stickySessionEnabled = false
407419
}
408420
if err != nil {
@@ -412,9 +424,20 @@ func NewChainClientV2(
412424

413425
var chainStreamConn *grpc.ClientConn
414426
if opts.TLSCert != nil {
415-
chainStreamConn, err = grpc.NewClient(network.ChainStreamGrpcEndpoint, grpc.WithTransportCredentials(opts.TLSCert), grpc.WithContextDialer(common.DialerFunc))
427+
chainStreamConn, err = grpc.NewClient(
428+
network.ChainStreamGrpcEndpoint,
429+
grpc.WithTransportCredentials(opts.TLSCert),
430+
grpc.WithContextDialer(common.DialerFunc),
431+
grpc.WithDefaultCallOptions(grpc.ForceCodec(protoCodec.GRPCCodec())),
432+
)
433+
416434
} else {
417-
chainStreamConn, err = grpc.NewClient(network.ChainStreamGrpcEndpoint, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(common.DialerFunc))
435+
chainStreamConn, err = grpc.NewClient(
436+
network.ChainStreamGrpcEndpoint,
437+
grpc.WithTransportCredentials(insecure.NewCredentials()),
438+
grpc.WithContextDialer(common.DialerFunc),
439+
grpc.WithDefaultCallOptions(grpc.ForceCodec(protoCodec.GRPCCodec())),
440+
)
418441
}
419442
if err != nil {
420443
err = errors.Wrapf(err, "failed to connect to the chain stream gRPC: %s", network.ChainStreamGrpcEndpoint)
@@ -448,12 +471,10 @@ func NewChainClientV2(
448471
authQueryClient: authtypes.NewQueryClient(conn),
449472
authzQueryClient: authztypes.NewQueryClient(conn),
450473
bankQueryClient: banktypes.NewQueryClient(conn),
451-
chainStreamClient: chainstreamtypes.NewStreamClient(chainStreamConn),
452474
chainStreamV2Client: chainstreamv2types.NewStreamClient(chainStreamConn),
453475
distributionQueryClient: distributiontypes.NewQueryClient(conn),
454476
erc20QueryClient: erc20types.NewQueryClient(conn),
455477
evmQueryClient: evmtypes.NewQueryClient(conn),
456-
exchangeQueryClient: exchangetypes.NewQueryClient(conn),
457478
exchangeV2QueryClient: exchangev2types.NewQueryClient(conn),
458479
ibcChannelQueryClient: ibcchanneltypes.NewQueryClient(conn),
459480
ibcClientQueryClient: ibcclienttypes.NewQueryClient(conn),
@@ -918,7 +939,7 @@ func (c *chainClientV2) runBatchBroadcast() {
918939
}
919940

920941
func (c *chainClientV2) GetGasFee() (string, error) {
921-
gasPrices := strings.Trim(c.opts.GasPrices, "inj")
942+
gasPrices := strings.TrimSuffix(c.txFactory.GasPrices().String(), client.InjDenom)
922943

923944
gas, err := strconv.ParseFloat(gasPrices, 64)
924945

client/chain/context.go

Lines changed: 9 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,10 @@ import (
3333
"github.com/cosmos/gogoproto/proto"
3434
icatypes "github.com/cosmos/ibc-go/v8/modules/apps/27-interchain-accounts/types"
3535
ibcfeetypes "github.com/cosmos/ibc-go/v8/modules/apps/29-fee/types"
36-
ibcapplicationtypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
36+
ibctransfertypes "github.com/cosmos/ibc-go/v8/modules/apps/transfer/types"
37+
ibcclienttypes "github.com/cosmos/ibc-go/v8/modules/core/02-client/types"
38+
ibcconnectiontypes "github.com/cosmos/ibc-go/v8/modules/core/03-connection/types"
39+
ibcchanneltypes "github.com/cosmos/ibc-go/v8/modules/core/04-channel/types"
3740
ibccoretypes "github.com/cosmos/ibc-go/v8/modules/core/types"
3841
ibclightclienttypes "github.com/cosmos/ibc-go/v8/modules/light-clients/06-solomachine"
3942
ibctenderminttypes "github.com/cosmos/ibc-go/v8/modules/light-clients/07-tendermint"
@@ -75,54 +78,6 @@ func NewInterfaceRegistry() types.InterfaceRegistry {
7578
return registry
7679
}
7780

78-
// NewTxConfig initializes new Cosmos TxConfig with certain signModes enabled.
79-
func NewTxConfig(signModes []signingtypes.SignMode) client.TxConfig {
80-
interfaceRegistry := NewInterfaceRegistry()
81-
keyscodec.RegisterInterfaces(interfaceRegistry)
82-
std.RegisterInterfaces(interfaceRegistry)
83-
erc20types.RegisterInterfaces(interfaceRegistry)
84-
evmtypes.RegisterInterfaces(interfaceRegistry)
85-
exchange.RegisterInterfaces(interfaceRegistry)
86-
exchangev2.RegisterInterfaces(interfaceRegistry)
87-
oracle.RegisterInterfaces(interfaceRegistry)
88-
insurance.RegisterInterfaces(interfaceRegistry)
89-
auction.RegisterInterfaces(interfaceRegistry)
90-
peggy.RegisterInterfaces(interfaceRegistry)
91-
ocr.RegisterInterfaces(interfaceRegistry)
92-
wasmx.RegisterInterfaces(interfaceRegistry)
93-
chaintypes.RegisterInterfaces(interfaceRegistry)
94-
tokenfactory.RegisterInterfaces(interfaceRegistry)
95-
permissions.RegisterInterfaces(interfaceRegistry)
96-
txfeestypes.RegisterInterfaces(interfaceRegistry)
97-
98-
// more cosmos types
99-
authtypes.RegisterInterfaces(interfaceRegistry)
100-
authztypes.RegisterInterfaces(interfaceRegistry)
101-
vestingtypes.RegisterInterfaces(interfaceRegistry)
102-
banktypes.RegisterInterfaces(interfaceRegistry)
103-
crisistypes.RegisterInterfaces(interfaceRegistry)
104-
distributiontypes.RegisterInterfaces(interfaceRegistry)
105-
evidencetypes.RegisterInterfaces(interfaceRegistry)
106-
govtypes.RegisterInterfaces(interfaceRegistry)
107-
govv1types.RegisterInterfaces(interfaceRegistry)
108-
paramproposaltypes.RegisterInterfaces(interfaceRegistry)
109-
ibcapplicationtypes.RegisterInterfaces(interfaceRegistry)
110-
ibccoretypes.RegisterInterfaces(interfaceRegistry)
111-
ibclightclienttypes.RegisterInterfaces(interfaceRegistry)
112-
ibctenderminttypes.RegisterInterfaces(interfaceRegistry)
113-
slashingtypes.RegisterInterfaces(interfaceRegistry)
114-
stakingtypes.RegisterInterfaces(interfaceRegistry)
115-
upgradetypes.RegisterInterfaces(interfaceRegistry)
116-
consensustypes.RegisterInterfaces(interfaceRegistry)
117-
minttypes.RegisterInterfaces(interfaceRegistry)
118-
feegranttypes.RegisterInterfaces(interfaceRegistry)
119-
wasmtypes.RegisterInterfaces(interfaceRegistry)
120-
icatypes.RegisterInterfaces(interfaceRegistry)
121-
122-
marshaler := codec.NewProtoCodec(interfaceRegistry)
123-
return tx.NewTxConfig(marshaler, signModes)
124-
}
125-
12681
// NewClientContext creates a new Cosmos Client context, where chainID
12782
// corresponds to Cosmos chain ID, fromSpec is either name of the key, or bech32-address
12883
// of the Cosmos account. Keyring is required to contain the specified key.
@@ -159,7 +114,6 @@ func NewClientContext(
159114
govtypes.RegisterInterfaces(interfaceRegistry)
160115
govv1types.RegisterInterfaces(interfaceRegistry)
161116
paramproposaltypes.RegisterInterfaces(interfaceRegistry)
162-
ibcapplicationtypes.RegisterInterfaces(interfaceRegistry)
163117
ibccoretypes.RegisterInterfaces(interfaceRegistry)
164118
ibclightclienttypes.RegisterInterfaces(interfaceRegistry)
165119
ibctenderminttypes.RegisterInterfaces(interfaceRegistry)
@@ -172,12 +126,16 @@ func NewClientContext(
172126
wasmtypes.RegisterInterfaces(interfaceRegistry)
173127
icatypes.RegisterInterfaces(interfaceRegistry)
174128
ibcfeetypes.RegisterInterfaces(interfaceRegistry)
129+
ibcchanneltypes.RegisterInterfaces(interfaceRegistry)
130+
ibcclienttypes.RegisterInterfaces(interfaceRegistry)
131+
ibcconnectiontypes.RegisterInterfaces(interfaceRegistry)
132+
ibctransfertypes.RegisterInterfaces(interfaceRegistry)
175133

176134
marshaler := codec.NewProtoCodec(interfaceRegistry)
177135
encodingConfig := EncodingConfig{
178136
InterfaceRegistry: interfaceRegistry,
179137
Marshaler: marshaler,
180-
TxConfig: NewTxConfig([]signingtypes.SignMode{
138+
TxConfig: tx.NewTxConfig(marshaler, []signingtypes.SignMode{
181139
signingtypes.SignMode_SIGN_MODE_DIRECT,
182140
}),
183141
}

client/common/api_request_assistant.go

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,13 @@ import (
1010
type APICall[Q any, R any] func(ctx context.Context, in *Q, opts ...grpc.CallOption) (*R, error)
1111
type APIStreamCall[Q any, S grpc.ClientStream] func(ctx context.Context, in *Q, opts ...grpc.CallOption) (S, error)
1212

13-
func ExecuteCall[Q any, R any](ctx context.Context, cookieAssistant CookieAssistant, call APICall[Q, R], in *Q) (*R, error) {
13+
func ExecuteCall[Q any, R any](
14+
ctx context.Context,
15+
cookieAssistant CookieAssistant,
16+
call APICall[Q, R],
17+
in *Q,
18+
opts ...grpc.CallOption,
19+
) (*R, error) {
1420
md := cookieAssistant.RealMetadata()
1521

1622
if upstreamMetadata, ok := metadata.FromOutgoingContext(ctx); ok {
@@ -20,17 +26,36 @@ func ExecuteCall[Q any, R any](ctx context.Context, cookieAssistant CookieAssist
2026

2127
localCtx := metadata.NewOutgoingContext(ctx, md)
2228

23-
response, err := call(localCtx, in, grpc.Header(&md))
29+
allOpts := make([]grpc.CallOption, 0, len(opts)+1)
30+
allOpts = append(allOpts, opts...)
31+
allOpts = append(allOpts, grpc.Header(&md))
32+
response, err := call(localCtx, in, allOpts...)
2433

2534
cookieAssistant.ProcessResponseMetadata(md)
2635

2736
return response, err
2837
}
2938

30-
func ExecuteStreamCall[Q any, S grpc.ClientStream](ctx context.Context, cookieAssistant CookieAssistant, call APIStreamCall[Q, S], in *Q) (S, error) {
31-
localCtx := metadata.NewOutgoingContext(ctx, cookieAssistant.RealMetadata())
39+
func ExecuteStreamCall[Q any, S grpc.ClientStream](
40+
ctx context.Context,
41+
cookieAssistant CookieAssistant,
42+
call APIStreamCall[Q, S],
43+
in *Q,
44+
opts ...grpc.CallOption,
45+
) (S, error) {
46+
md := cookieAssistant.RealMetadata()
47+
48+
if upstreamMetadata, ok := metadata.FromOutgoingContext(ctx); ok {
49+
// If metadata already exists in the context, merge it with the cookie metadata
50+
md = metadata.Join(md, upstreamMetadata)
51+
}
52+
53+
localCtx := metadata.NewOutgoingContext(ctx, md)
3254

33-
stream, callError := call(localCtx, in)
55+
allOpts := make([]grpc.CallOption, 0, len(opts)+1)
56+
allOpts = append(allOpts, opts...)
57+
allOpts = append(allOpts, grpc.Header(&md))
58+
stream, callError := call(localCtx, in, allOpts...)
3459

3560
if callError == nil {
3661
header, err := stream.Header()

examples/chain/12_ChainStream/example.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,6 @@ func main() {
9292
res, err := stream.Recv()
9393
if err != nil {
9494
panic(err)
95-
return
9695
}
9796
str, _ := json.MarshalIndent(res, "", "\t")
9897
fmt.Print(string(str))

examples/chain/authz/query/1_Grants/example.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package main
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76

87
"github.com/InjectiveLabs/sdk-go/client"
@@ -72,10 +71,13 @@ func main() {
7271

7372
res, err := chainClient.GetAuthzGrants(ctx, req)
7473
if err != nil {
75-
fmt.Println(err)
74+
panic(err)
7675
}
7776

78-
str, _ := json.MarshalIndent(res, "", "\t")
79-
fmt.Print(string(str))
77+
jsonResponse, err := clientCtx.Codec.MarshalJSON(res)
78+
if err != nil {
79+
panic(err)
80+
}
8081

82+
fmt.Print(string(jsonResponse))
8183
}

examples/chain/bank/query/2_BankBalances/example.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ func main() {
6161

6262
res, err := chainClient.GetBankBalances(ctx, address)
6363
if err != nil {
64-
fmt.Println(err)
64+
panic(err)
6565
}
6666

6767
str, _ := json.MarshalIndent(res, "", "\t")

examples/chain/exchange/28_MsgCreateGTBSpotLimitOrder/example.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import (
1616
)
1717

1818
func main() {
19-
network := common.LoadNetwork("mainnet", "lb")
19+
network := common.LoadNetwork("testnet", "lb")
2020
tmClient, err := rpchttp.New(network.TmEndpoint)
2121
if err != nil {
2222
panic(err)
@@ -71,7 +71,7 @@ func main() {
7171

7272
defaultSubaccountID := chainClient.DefaultSubaccount(senderAddress)
7373

74-
marketId := "0xa508cb32923323679f29a032c70342c147c17d0145625922b0ef22e955c844c0"
74+
marketId := "0x0611780ba69656949525013d947713300f56c37b6175e02f26bffa495c3208fe"
7575

7676
amount := decimal.NewFromFloat(2)
7777
price := decimal.NewFromFloat(22.55)

0 commit comments

Comments
 (0)