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
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)
CODE_ID=$(echo $RES | jq -r '.logs[0].events[-1].attributes[0].value')
./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环境,指定具体合约地址进行交互
查询合约
const queryResult = await client.queryContractSmart(instantiateResponse.contractAddress, { 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
.