Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wasm exec returns response & err #842

Merged
merged 4 commits into from
Oct 30, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -814,7 +814,7 @@ func (tn *ChainNode) StoreContract(ctx context.Context, keyName string, fileName
return res.CodeInfos[0].CodeID, nil
}

func (tn *ChainNode) getTransaction(clientCtx client.Context, txHash string) (*types.TxResponse, error) {
func (tn *ChainNode) GetTransaction(clientCtx client.Context, txHash string) (*types.TxResponse, error) {
// Retry because sometimes the tx is not committed to state yet.
var txResp *types.TxResponse
err := retry.Do(func() error {
Expand Down Expand Up @@ -938,7 +938,7 @@ func (tn *ChainNode) InstantiateContract(ctx context.Context, keyName string, co
return "", err
}

txResp, err := tn.getTransaction(tn.CliContext(), txHash)
txResp, err := tn.GetTransaction(tn.CliContext(), txHash)
if err != nil {
return "", fmt.Errorf("failed to get transaction %s: %w", txHash, err)
}
Expand All @@ -961,11 +961,25 @@ func (tn *ChainNode) InstantiateContract(ctx context.Context, keyName string, co
}

// ExecuteContract executes a contract transaction with a message using it's address.
func (tn *ChainNode) ExecuteContract(ctx context.Context, keyName string, contractAddress string, message string, extraExecTxArgs ...string) (txHash string, err error) {
func (tn *ChainNode) ExecuteContract(ctx context.Context, keyName string, contractAddress string, message string, extraExecTxArgs ...string) (res *types.TxResponse, err error) {
cmd := []string{"wasm", "execute", contractAddress, message}
cmd = append(cmd, extraExecTxArgs...)

return tn.ExecTx(ctx, keyName, cmd...)
txHash, err := tn.ExecTx(ctx, keyName, cmd...)
if err != nil {
return &types.TxResponse{}, err
}

txResp, err := tn.GetTransaction(tn.CliContext(), txHash)
if err != nil {
return &types.TxResponse{}, fmt.Errorf("failed to get transaction %s: %w", txHash, err)
}

if txResp.Code != 0 {
return txResp, fmt.Errorf("error in transaction (code: %d): %s", txResp.Code, txResp.RawLog)
Reecepbcups marked this conversation as resolved.
Show resolved Hide resolved
}

return txResp, nil
}

// QueryContract performs a smart query, taking in a query struct and returning a error with the response struct populated.
Expand Down
10 changes: 5 additions & 5 deletions chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ func (c *CosmosChain) SendIBCTransfer(
if err != nil {
return tx, fmt.Errorf("send ibc transfer: %w", err)
}
txResp, err := c.getTransaction(txHash)
txResp, err := c.GetTransaction(txHash)
if err != nil {
return tx, fmt.Errorf("failed to get transaction %s: %w", txHash, err)
}
Expand Down Expand Up @@ -492,7 +492,7 @@ func (c *CosmosChain) QueryBankMetadata(ctx context.Context, denom string) (*Ban
}

func (c *CosmosChain) txProposal(txHash string) (tx TxProposal, _ error) {
txResp, err := c.getTransaction(txHash)
txResp, err := c.GetTransaction(txHash)
if err != nil {
return tx, fmt.Errorf("failed to get transaction %s: %w", txHash, err)
}
Expand Down Expand Up @@ -522,7 +522,7 @@ func (c *CosmosChain) InstantiateContract(ctx context.Context, keyName string, c
}

// ExecuteContract executes a contract transaction with a message using it's address.
func (c *CosmosChain) ExecuteContract(ctx context.Context, keyName string, contractAddress string, message string, extraExecTxArgs ...string) (txHash string, err error) {
func (c *CosmosChain) ExecuteContract(ctx context.Context, keyName string, contractAddress string, message string, extraExecTxArgs ...string) (res *types.TxResponse, err error) {
return c.getFullNode().ExecuteContract(ctx, keyName, contractAddress, message, extraExecTxArgs...)
}

Expand Down Expand Up @@ -593,9 +593,9 @@ func (c *CosmosChain) AllBalances(ctx context.Context, address string) (types.Co
return res.GetBalances(), nil
}

func (c *CosmosChain) getTransaction(txhash string) (*types.TxResponse, error) {
func (c *CosmosChain) GetTransaction(txhash string) (*types.TxResponse, error) {
fn := c.getFullNode()
return fn.getTransaction(fn.CliContext(), txhash)
return fn.GetTransaction(fn.CliContext(), txhash)
}

func (c *CosmosChain) GetGasFeesInNativeDenom(gasPaid int64) int64 {
Expand Down
40 changes: 36 additions & 4 deletions examples/cosmos/chain_miscellaneous_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import (

"cosmossdk.io/math"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"

sdk "github.com/cosmos/cosmos-sdk/types"
testutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
Expand Down Expand Up @@ -44,10 +46,11 @@ func CosmosChainTestMiscellaneous(t *testing.T, name, version string) {
ChainName: name,
Version: version,
ChainConfig: ibc.ChainConfig{
Denom: "ujuno",
Bech32Prefix: "juno",
CoinType: "118",
ModifyGenesis: cosmos.ModifyGenesis(sdk47Genesis),
Denom: "ujuno",
Bech32Prefix: "juno",
CoinType: "118",
ModifyGenesis: cosmos.ModifyGenesis(sdk47Genesis),
EncodingConfig: wasmEncoding(),
},
NumValidators: &numVals,
NumFullNodes: &numFullNodes,
Expand Down Expand Up @@ -87,10 +90,17 @@ func CosmosChainTestMiscellaneous(t *testing.T, name, version string) {
testQueryCmd(ctx, t, chain)
testHasCommand(ctx, t, chain)
testTokenFactory(ctx, t, chain, users)
testFailedCWExecute(ctx, t, chain, users)
testAddingNode(ctx, t, chain)
testGetGovernanceAddress(ctx, t, chain)
}

func wasmEncoding() *testutil.TestEncodingConfig {
cfg := cosmos.DefaultEncoding()
wasmtypes.RegisterInterfaces(cfg.InterfaceRegistry)
return &cfg
}

func testBuildDependencies(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain) {
deps := chain.Validators[0].GetBuildInformation(ctx)

Expand Down Expand Up @@ -129,6 +139,28 @@ func testBuildDependencies(ctx context.Context, t *testing.T, chain *cosmos.Cosm
}
}

func testFailedCWExecute(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain, users []ibc.Wallet) {
user := users[0]
keyName := user.KeyName()

codeId, err := chain.StoreContract(ctx, keyName, "sample_contracts/cw_template.wasm")
if err != nil {
t.Fatal(err)
}

contractAddr, err := chain.InstantiateContract(ctx, keyName, codeId, `{"count":0}`, true)
if err != nil {
t.Fatal(err)
}

// execute on the contract with the wrong message (err)
txResp, err := chain.ExecuteContract(ctx, keyName, contractAddr, `{"not_a_func":{}}`)
require.Error(t, err)
fmt.Printf("txResp.RawLog: %+v\n", txResp.RawLog)
fmt.Printf("err: %+v\n", err)
require.Contains(t, err.Error(), "failed to execute message")
}

func testWalletKeys(ctx context.Context, t *testing.T, chain *cosmos.CosmosChain) {
// create a general key
randKey := "randkey123"
Expand Down
Binary file not shown.
14 changes: 2 additions & 12 deletions examples/ibc/wasm/wasm_icq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,19 +278,9 @@ func TestInterchainQueriesWASM(t *testing.T) {
logger.Info("Executing msg ->", zap.String("msg", msg))

//Query the contract on chain 1. The contract makes an interchain query to chain 2 to get the chain 2 user's balance.
hash, err := chain1CChain.ExecuteContract(ctx, chain1User.KeyName(), contractAddr, msg)

require.NoError(t, err)

// Check the results from the interchain query above.
cmd = []string{chain1.Config().Bin, "query", "tx", hash,
"--node", chain1.GetRPCAddress(),
"--home", chain1.HomeDir(),
"--chain-id", chain1.Config().ChainID,
"--output", "json",
}
_, _, err = chain1.Exec(ctx, cmd, nil)
resp, err := chain1CChain.ExecuteContract(ctx, chain1User.KeyName(), contractAddr, msg)
require.NoError(t, err)
require.NotNil(t, resp)

// Wait a few blocks for query to be sent to counterparty.
err = testutil.WaitForBlocks(ctx, 5, chain1, chain2)
Expand Down
Loading
Loading