Skip to content

Commit

Permalink
feat: query contract with a string (#806)
Browse files Browse the repository at this point in the history
* Use a string in QueryContract

* Marshal jsonMap to query

* use string query for wasm ibc test

(cherry picked from commit b6928ac)

# Conflicts:
#	go.sum
#	go.work.sum
  • Loading branch information
Reecepbcups authored and mergify[bot] committed Oct 24, 2023
1 parent b988aa9 commit fed2475
Show file tree
Hide file tree
Showing 4 changed files with 506 additions and 14 deletions.
22 changes: 19 additions & 3 deletions chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -990,10 +990,26 @@ func (tn *ChainNode) ExecuteContract(ctx context.Context, keyName string, contra

// QueryContract performs a smart query, taking in a query struct and returning a error with the response struct populated.
func (tn *ChainNode) QueryContract(ctx context.Context, contractAddress string, queryMsg any, response any) error {
query, err := json.Marshal(queryMsg)
if err != nil {
return err
var query []byte
var err error

if q, ok := queryMsg.(string); ok {
var jsonMap map[string]interface{}
if err := json.Unmarshal([]byte(q), &jsonMap); err != nil {
return err
}

query, err = json.Marshal(jsonMap)
if err != nil {
return err
}
} else {
query, err = json.Marshal(queryMsg)
if err != nil {
return err
}
}

stdout, _, err := tn.ExecQuery(ctx, "wasm", "contract-state", "smart", contractAddress, string(query))
if err != nil {
return err
Expand Down
13 changes: 2 additions & 11 deletions examples/ibc/wasm/wasm_ibc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,9 @@ func TestWasmIbc(t *testing.T) {
require.NoError(t, err)
juno1ChannelID := juno1ChannelInfo[len(juno1ChannelInfo)-1].ChannelID

queryMsg := fmt.Sprintf(`{"account":{"channel_id":"%s"}}`, juno1ChannelID)

// Query ibc_reflect_send contract on Juno1 for remote address (populated via ibc)
queryMsg := ReflectSendQueryMsg{Account: &AccountQuery{ChannelID: juno1ChannelID}}
var ibcReflectSendResponse IbcReflectSendResponseData
err = juno1Chain.QueryContract(ctx, ibcReflectSendContractAddr, queryMsg, &ibcReflectSendResponse)
require.NoError(t, err)
Expand All @@ -183,16 +184,6 @@ func TestWasmIbc(t *testing.T) {
require.Equal(t, ibcReflectSendResponse.Data.RemoteAddr, ibcReflectResponse.Data.Account)
}

type ReflectSendQueryMsg struct {
Admin *struct{} `json:"admin,omitempty"`
ListAccounts *struct{} `json:"list_accounts,omitempty"`
Account *AccountQuery `json:"account,omitempty"`
}

type AccountQuery struct {
ChannelID string `json:"channel_id"`
}

type Coin struct {
Denom string `json:"denom"` // type, eg. "ATOM"
Amount string `json:"amount"` // string encoing of decimal value, eg. "12.3456"
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1233,8 +1233,13 @@ golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug
golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.0.0-20221014081412-f15817d10f9b/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
<<<<<<< HEAD
golang.org/x/net v0.14.0 h1:BONx9s002vGdD9umnlX1Po8vOZmrgH34qlHcD1MfK14=
golang.org/x/net v0.14.0/go.mod h1:PpSgVXXLK0OxS0F31C1/tv6XNguvCrnXIDrFMspZIUI=
=======
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
>>>>>>> b6928ac (feat: query contract with a string (#806))
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.0.0-20190226205417-e64efc72b421/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
golang.org/x/oauth2 v0.0.0-20190604053449-0f29369cfe45/go.mod h1:gOpvHmFTYa4IltrdGE7lF6nIHvwfUNPOp7c8zoXwtLw=
Expand Down
Loading

0 comments on commit fed2475

Please sign in to comment.