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
  • Loading branch information
Reecepbcups authored Oct 24, 2023
1 parent 128f818 commit b6928ac
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 15 deletions.
22 changes: 19 additions & 3 deletions chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -970,10 +970,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
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1312,6 +1312,7 @@ golang.org/x/net v0.0.0-20220909164309-bea034e7d591/go.mod h1:YDH+HFinaLZZlnHAfS
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=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
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
2 changes: 1 addition & 1 deletion go.work.sum
Original file line number Diff line number Diff line change
Expand Up @@ -403,7 +403,6 @@ golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
golang.org/x/net v0.8.0/go.mod h1:QVkue5JL9kW//ek3r6jTKnTFis1tRmNAW2P1shuFdJc=
golang.org/x/net v0.10.0/go.mod h1:0qNGK6F8kojg2nk9dLZ2mShWaEBan6FAoqfSigmmuDg=
golang.org/x/net v0.12.0/go.mod h1:zEVYFnQC7m/vmpQFELhcD1EWkZlX69l4oqgmer6hfKA=
golang.org/x/net v0.17.0 h1:pVaXccu2ozPjCXewfr1S7xza/zcXTity9cCdXQYSjIM=
golang.org/x/net v0.17.0/go.mod h1:NxSsAGuq816PNPmqtQdLE42eU2Fs7NoRIZrHJAlaCOE=
golang.org/x/oauth2 v0.0.0-20170207211851-4464e7848382/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/oauth2 v0.8.0/go.mod h1:yr7u4HXZRm1R1kBWqr/xKNqewf0plRYoB7sla+BCIXE=
Expand Down Expand Up @@ -474,6 +473,7 @@ google.golang.org/grpc v1.58.2/go.mod h1:tgX3ZQDlNJGU96V6yHh1T/JeoBQ2TXdr43YbYSs
google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
google.golang.org/protobuf v1.30.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I=
gopkg.in/check.v1 v1.0.0-20200227125254-8fa46927fb4f/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/go-playground/assert.v1 v1.2.1/go.mod h1:9RXL0bg/zibRAgZUYszZSwO/z8Y/a8bDuhia5mkpMnE=
gopkg.in/go-playground/validator.v8 v8.18.2/go.mod h1:RX2a/7Ha8BgOhfk7j780h4/u/RRjR0eouCJSH80/M2Y=
gopkg.in/mgo.v2 v2.0.0-20180705113604-9856a29383ce/go.mod h1:yeKp02qBN3iKW1OzL3MGk2IdtZzaj7SFntXj72NppTA=
gotest.tools v2.2.0+incompatible h1:VsBPFP1AI068pPrMxtb/S8Zkgf9xEmTLJjfM+P5UIEo=
Expand Down

0 comments on commit b6928ac

Please sign in to comment.