From c0037a7a54b9383d6bd101f3fc822e089a128e46 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Fri, 10 May 2024 21:25:38 +0000 Subject: [PATCH] feat: cosmwasm migrate contract (#1122) * feat: cosmwasm migrate contract (#1121) * migrate contract * consume codeID * fix --------- Co-authored-by: Reece Williams <31943163+Reecepbcups@users.noreply.github.com> (cherry picked from commit e7c0c395f8ed2af621a16ae282726a808f6b1564) # Conflicts: # chain/cosmos/chain_node.go * move to module_cosmwasm --------- Co-authored-by: Bi Phan <102435501+BiPhan4@users.noreply.github.com> Co-authored-by: Reece Williams --- chain/cosmos/cosmos_chain.go | 5 +++++ chain/cosmos/module_cosmwasm.go | 23 +++++++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/chain/cosmos/cosmos_chain.go b/chain/cosmos/cosmos_chain.go index 094d55508..ceaedc4f4 100644 --- a/chain/cosmos/cosmos_chain.go +++ b/chain/cosmos/cosmos_chain.go @@ -549,6 +549,11 @@ func (c *CosmosChain) ExecuteContract(ctx context.Context, keyName string, contr return c.getFullNode().ExecuteContract(ctx, keyName, contractAddress, message, extraExecTxArgs...) } +// MigrateContract performs contract migration +func (c *CosmosChain) MigrateContract(ctx context.Context, keyName string, contractAddress string, codeID string, message string, extraExecTxArgs ...string) (res *types.TxResponse, err error) { + return c.getFullNode().MigrateContract(ctx, keyName, contractAddress, codeID, message, extraExecTxArgs...) +} + // QueryContract performs a smart query, taking in a query struct and returning a error with the response struct populated. func (c *CosmosChain) QueryContract(ctx context.Context, contractAddress string, query any, response any) error { return c.getFullNode().QueryContract(ctx, contractAddress, query, response) diff --git a/chain/cosmos/module_cosmwasm.go b/chain/cosmos/module_cosmwasm.go index 3ba2e6536..9ca197145 100644 --- a/chain/cosmos/module_cosmwasm.go +++ b/chain/cosmos/module_cosmwasm.go @@ -10,6 +10,7 @@ import ( "path" "path/filepath" + "github.com/cosmos/cosmos-sdk/types" sdk "github.com/cosmos/cosmos-sdk/types" "github.com/strangelove-ventures/interchaintest/v8/testutil" ) @@ -160,6 +161,28 @@ func (tn *ChainNode) QueryContract(ctx context.Context, contractAddress string, return err } +// MigrateContract performs contract migration +func (tn *ChainNode) MigrateContract(ctx context.Context, keyName string, contractAddress string, codeID string, message string, extraExecTxArgs ...string) (res *types.TxResponse, err error) { + cmd := []string{"wasm", "migrate", contractAddress, codeID, message} + cmd = append(cmd, extraExecTxArgs...) + + 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) + } + + return txResp, nil +} + // StoreClientContract takes a file path to a client smart contract and stores it on-chain. Returns the contracts code id. func (tn *ChainNode) StoreClientContract(ctx context.Context, keyName string, fileName string, extraExecTxArgs ...string) (string, error) { content, err := os.ReadFile(fileName)