Cosmos 合约部署交互

接上篇:

Deploy the contract to the testnet

export TXFLAG="${NODE} --chain-id ${CHAIN_ID} --gas-prices 0.25umlg --gas auto --gas-adjustment 1.3"

 

# see how many codes we have now
./wasmd query wasm list-code $NODE

  

 # gas is huge due to wasm size... but auto-zipping reduced this from 1.8M to around 600k
 # you can see the code in the result
RES=$(./wasmd tx wasm store /root/go/bin/cw-contracts/contracts/cw-to-do-list/artifacts/cw_to_do_list.wasm --from wallet $TXFLAG -y --output json -b block)

    # you can also get the code this way
CODE_ID=$(echo $RES | jq -r '.logs[0].events[-1].attributes[0].value')

 # no contracts yet, this should return an empty list
   ./wasmd query wasm list-contract-by-code $CODE_ID $NODE --output json
  
 # you can also download the wasm from the chain and check that the diff between them is empty
./wasmd query wasm code $CODE_ID $NODE download.wasm
diff /root/go/bin/cw-contracts/contracts/cw-to-do-list/artifacts/cw_to_do_list.wasm download.wasm

 

合约实例化:

安装CosmJS CLI

npx @cosmjs/cli@^0.28.1 --init https://raw.githubusercontent.com/InterWasm/cw-plus-helpers/main/base.ts

上一步已进入CosmJS环境

Let us import the necessary StdFee interface and generate/load a wallet address.

import { StdFee } from "@cosmjs/stargate";

const [addr, client] = await useOptions(malagaOptions).setup("password");

//Display the wallet address
client.getAccount(addr);
//Display the account balance
client.getBalance(addr,"umlg")

Define a defaultFee to be passed into instantiation and execution functions later on:

const defaultFee: StdFee = { amount: [{amount: "200000", denom: "umlg",},], gas: "200000",};

  

We can now instantiate the contract and create a To-Do List instance using the code id we have received upon deployment. Notice that the instantiation message is empty (i.e., {}) and does not specify an owner address. In this case, the contract will be instantiated with our wallet address assigned as the contract owner.

//Replace the Code Id with the one you have received earlier
const codeId = 2461  

const instantiateResponse = await client.instantiate(addr, codeId, {}, "To Do List", defaultFee)
console.log(instantiateResponse)

 

 

 

 create a new To-Do List entry

//Enable REPL editor mode to edit multiple lines of code
.editor

const executeResponse = await client.execute(
         addr, 
         instantiateResponse.contractAddress,
         {
           new_entry: {
             description: "A new entry.",
             priority: "Medium"
           }
         },
         defaultFee,
       )

//Exit editor using `^D` to execute the code entered
^D

console.log(executeResponse)

 

如果已实例化完成的,退出去又进来JS环境,指定具体合约地址进行交互


import { StdFee } from "@cosmjs/stargate";

const [addr, client] = await useOptions(malagaOptions).setup("password");

client.getAccount(addr);

client.getBalance(addr,"umlg");

const defaultFee: StdFee = { amount: [{amount: "200000", denom: "umlg",},], gas: "200000",};

const executeResponse = await client.execute(
    addr,
    "wasm1g5zvh2yya84npxe27gvdmftfnfpj32r5ra0gf8n4a4vgaeuggrws4xyp4p",
    {
      new_entry: {
        description: "A new entry 1.",
        priority: "medium"
      }
    },
    defaultFee,
);

 

 

 查询合约

const queryResult = await client.queryContractSmart(instantiateResponse.contractAddress, { query_list: {} })
 const queryResult = await client.queryContractSmart("wasm1g5zvh2yya84npxe27gvdmftfnfpj32r5ra0gf8n4a4vgaeuggrws4xyp4p", { query_list: {} })

  

Now, running the command below will create a new key file containing an encrypted mnemonic in your HOME directory (i.e., ~/.another.key) and generate a new wallet address.

const [another_addr, another_client] = await useOptions(malagaOptions).setup("password", ".another.key" );

  

//The original wallet address for the contract owner
client.getAccount(addr)

//The new wallet address that is not the contract owner
another_client.getAccount(another_addr)

  可以用其他账户执行程序

//Enable REPL editor mode to edit multiple lines of code
.editor

const executeResponse_5 = await another_client.execute(
         another_addr, 
         instantiateResponse.contractAddress,
         {
           new_entry: {
             description: "A new entry attempt from a wallet address that is not the contract owner.",
             priority: "Medium"
           }
         },
         defaultFee,
       )

//Exit editor using `^D` to execute the code entered
^D

 Because another_addr is not the owner of our contract instance, the following portion of our contract code returns the error message that corresponds to ContractError::Unauthorized {} defined in the file /src/error.rs.

 

You may now exit the CosmJS CLI session with the command .exit.

 

posted @ 2022-11-07 17:06  李东浩  阅读(201)  评论(0编辑  收藏  举报