Skip to content

Commit

Permalink
feat: cosmwasm migrate contract (#1121)
Browse files Browse the repository at this point in the history
* migrate contract

* consume codeID

* fix

---------

Co-authored-by: Reece Williams <[email protected]>
(cherry picked from commit e7c0c39)

# Conflicts:
#	chain/cosmos/chain_node.go
  • Loading branch information
BiPhan4 authored and mergify[bot] committed May 10, 2024
1 parent 65cce0c commit 2af011f
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
138 changes: 138 additions & 0 deletions chain/cosmos/chain_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,144 @@ func (tn *ChainNode) GetBuildInformation(ctx context.Context) *BinaryBuildInform
}
}

<<<<<<< HEAD

Check failure on line 991 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / test-conformance

syntax error: non-declaration statement outside function body

Check failure on line 991 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / test-cosmos-examples

syntax error: non-declaration statement outside function body

Check failure on line 991 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / test-ibc-examples

syntax error: non-declaration statement outside function body

Check failure on line 991 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / unit-tests

syntax error: non-declaration statement outside function body

Check failure on line 991 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / unit-tests

syntax error: non-declaration statement outside function body

Check failure on line 991 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / unit-tests

syntax error: non-declaration statement outside function body

Check failure on line 991 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / lint

expected declaration, found '<<' (typecheck)
=======
// InstantiateContract takes a code id for a smart contract and initialization message and returns the instantiated contract address.
func (tn *ChainNode) InstantiateContract(ctx context.Context, keyName string, codeID string, initMessage string, needsNoAdminFlag bool, extraExecTxArgs ...string) (string, error) {
command := []string{"wasm", "instantiate", codeID, initMessage, "--label", "wasm-contract"}
command = append(command, extraExecTxArgs...)
if needsNoAdminFlag {
command = append(command, "--no-admin")
}
txHash, err := tn.ExecTx(ctx, keyName, command...)
if err != nil {
return "", err
}

txResp, err := tn.GetTransaction(tn.CliContext(), txHash)
if err != nil {
return "", fmt.Errorf("failed to get transaction %s: %w", txHash, err)
}
if txResp.Code != 0 {
return "", fmt.Errorf("error in transaction (code: %d): %s", txResp.Code, txResp.RawLog)
}

stdout, _, err := tn.ExecQuery(ctx, "wasm", "list-contract-by-code", codeID)
if err != nil {
return "", err
}

contactsRes := QueryContractResponse{}
if err := json.Unmarshal([]byte(stdout), &contactsRes); err != nil {
return "", err
}

contractAddress := contactsRes.Contracts[len(contactsRes.Contracts)-1]
return contractAddress, nil
}

// 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) (res *types.TxResponse, err error) {
cmd := []string{"wasm", "execute", contractAddress, 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
}

// 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
}

// 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 {
var query []byte
var err error

if q, ok := queryMsg.(string); ok {

Check failure on line 1076 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / lint

expected declaration, found 'if' (typecheck)
var jsonMap map[string]interface{}
if err := json.Unmarshal([]byte(q), &jsonMap); err != nil {

Check failure on line 1078 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / lint

expected declaration, found 'if' (typecheck)
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
}
err = json.Unmarshal([]byte(stdout), response)
return err
}

// 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)
if err != nil {
return "", err
}
_, file := filepath.Split(fileName)
err = tn.WriteFile(ctx, content, file)
if err != nil {
return "", fmt.Errorf("writing contract file to docker volume: %w", err)
}

cmd := []string{"ibc-wasm", "store-code", path.Join(tn.HomeDir(), file), "--gas", "auto"}
cmd = append(cmd, extraExecTxArgs...)

_, err = tn.ExecTx(ctx, keyName, cmd...)
if err != nil {
return "", err
}

codeHashByte32 := sha256.Sum256(content)
codeHash := hex.EncodeToString(codeHashByte32[:])

//return stdout, nil
return codeHash, nil
}

>>>>>>> e7c0c39 (feat: cosmwasm migrate contract (#1121))

Check failure on line 1128 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / test-conformance

syntax error: non-declaration statement outside function body

Check failure on line 1128 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / test-conformance

invalid character U+0023 '#'

Check failure on line 1128 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / test-cosmos-examples

syntax error: non-declaration statement outside function body

Check failure on line 1128 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / test-cosmos-examples

invalid character U+0023 '#'

Check failure on line 1128 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / test-ibc-examples

syntax error: non-declaration statement outside function body

Check failure on line 1128 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / test-ibc-examples

invalid character U+0023 '#'

Check failure on line 1128 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / unit-tests

syntax error: non-declaration statement outside function body

Check failure on line 1128 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / unit-tests

invalid character U+0023 '#'

Check failure on line 1128 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / unit-tests

syntax error: non-declaration statement outside function body

Check failure on line 1128 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / unit-tests

invalid character U+0023 '#'

Check failure on line 1128 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / unit-tests

syntax error: non-declaration statement outside function body

Check failure on line 1128 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / unit-tests

invalid character U+0023 '#'

Check failure on line 1128 in chain/cosmos/chain_node.go

View workflow job for this annotation

GitHub Actions / lint

illegal character U+0023 '#' (typecheck)
// QueryClientContractCode performs a query with the contract codeHash as the input and code as the output
func (tn *ChainNode) QueryClientContractCode(ctx context.Context, codeHash string, response any) error {
stdout, _, err := tn.ExecQuery(ctx, "ibc-wasm", "code", codeHash)
Expand Down
5 changes: 5 additions & 0 deletions chain/cosmos/cosmos_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,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)
Expand Down

0 comments on commit 2af011f

Please sign in to comment.