We generated a wasm binary executable in the previous chapter. Let's put it into use. Now, we will
upload the code to the blockchain. Afterwards, you can download the bytecode to verify it is proper:
Copy
# 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 resultRES=$(wasmd tx wasm store contract.wasm --from fred $TXFLAG -y)# you can also get the code this wayCODE_ID=$(echo $RES | jq -r '.logs[0].events[0].attributes[-1].value')# no contracts yet, this should return `null`
wasmd query wasm list-contract-by-code $CODE_ID$NODE$NODE# 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 contract.wasm download.wasm
We can now create an instance of this wasm contract. Here the verifier will fund an escrow, that
will allow fred to control payout and upon release, the funds go to bob.
Copy
# instantiate contract and verifyINIT=$(jq -n --arg fred $(wasmd keys show -a fred) --arg bob $(wasmd keys show -a bob)'{"arbiter":$fred,"recipient":$bob}')
wasmd tx wasm instantiate $CODE_ID"$INIT"\
--from fred --amount=50000umayo --label "escrow 1"$TXFLAG -y
# check the contract state (and account balance)
wasmd query wasm list-contract-by-code $CODE_ID$NODECONTRACT=$(wasmd query wasm list-contract-by-code $CODE_ID $NODE | jq -r '.[0].address')echo$CONTRACT# we should see this contract with 50000umayo
wasmd query wasm contract $CONTRACT$NODE
wasmd query account $CONTRACT$NODE# you can dump entire contract state
wasmd query wasm contract-state all $CONTRACT$NODE# note that we prefix the key "config" with two bytes indicating it's length# echo -n config | xxd -ps# gives 636f6e666967# thus we have a key 0006636f6e666967# you can also query one key directly
wasmd query wasm contract-state raw $CONTRACT 0006636f6e666967 $NODE --hex
# Note that keys are hex encoded, and val is base64 encoded.# To view the returned data (assuming it is ascii), try something like:# (Note that in many cases the binary data returned is non in ascii format, thus the encoding)
wasmd query wasm contract-state all $CONTRACT$NODE| jq -r '.[0].key'| xxd -r -ps
wasmd query wasm contract-state all $CONTRACT$NODE| jq -r '.[0].val'| base64 -d
# or try a "smart query", executing against the contract
wasmd query wasm contract-state smart $CONTRACT'{}'$NODE# (since we didn't implement any valid QueryMsg, we just get a parse error back)
Once we have the funds in the escrow, let us try to release them. First, failing to do so with a key
that is not the verifier, then using the proper key to release.
Copy
# execute fails if wrong personAPPROVE='{"approve":{"quantity":[{"amount":"50000","denom":"umayo"}]}}'
wasmd tx wasm execute $CONTRACT"$APPROVE"\
--from thief $TXFLAG -y
# looking at the logs should show: "execute wasm contract failed: Unauthorized"# and bob should still be broke (and broken showing the account does not exist Error)
wasmd query account $(wasmd keys show bob -a)$NODE# but succeeds when fred tries
wasmd tx wasm execute $CONTRACT"$APPROVE"\
--from fred $TXFLAG -y
wasmd query account $(wasmd keys show bob -a)$NODE# contract coins must be empty
wasmd query account $CONTRACT$NODE
If you set up the Node Console / REPL in the client setup section, you can use
that to deploy and execute your contract. I think you will find that JSON manipulation and parsing
is a bit nicer in JavaScript than in Shell Script.
First, go to the cli directory and start up your console:
Copy
const fredSeed =loadOrCreateMnemonic("fred.key");const{address: fredAddr,client: fredClient}=awaitconnect(fredSeed,{});// bob doesn't have a client here as we will not// submit any tx with this account, just query balanceconst bobSeed =loadOrCreateMnemonic("bob.key");const bobAddr =awaitmnemonicToAddress("muffin fix provide project obtain......", bobSeed);const thiefSeed =loadOrCreateMnemonic("thief.key");const{address: thiefAddr,client: thiefClient}=awaitconnect(thiefSeed,{});
console.log(fredAddr, bobAddr, thiefAddr);
Hit the faucet it needed for fred, so he has tokens to submit transactions:
Copy
fredClient.getAccount();// if "undefined", do the followinghitFaucet(defaultFaucetUrl, fredAddr,"umayo")
fredClient.getAccount();
thiefClient.getAccount();// if "undefined", do the followinghitFaucet(defaultFaucetUrl, thiefAddr,"umayo")
thiefClient.getAccount();// check bobAddr has no funds
fredClient.getAccount(bobAddr);// get the working directory (needed later)
process.cwd()
Now, we go back to the Node console and upload the contract and instantiate it:
Copy
const wasm = fs.readFileSync('contract.wasm');// you can add extra information to contract details such as source and builder.const up =await fredClient.upload(wasm,{source:"https://crates.io/api/v1/crates/cw-escrow/0.7.0/download",builder:"cosmwasm/rust-optimizer:0.10.7"});
console.log(up);const{ codeId }= up;const initMsg ={arbiter: fredAddr,recipient: bobAddr};const{ contractAddress }=await fredClient.instantiate(codeId, initMsg,"Escrow 1",{memo:"memo",transferAmount:[{denom:"umayo",amount:"50000"}]});// check the contract is set up properly
console.log(contractAddress);
fredClient.getContract(contractAddress);
fredClient.getAccount(contractAddress);// make a raw query - key length prefixed "config"const key =newUint8Array([0,6,...toAscii("config")]);const raw =await fredClient.queryContractRaw(contractAddress, key);JSON.parse(fromUtf8(raw))// note the addresses are stored in base64 internally, not bech32, but the data is there... this is why we often implement smart queries on real contracts
Once we have properly configured the contract, let's show how to use it, both the proper "approve"
command:
Copy
const approve ={approve:{quantity:[{amount:"50000",denom:"umayo"}]}};// thief cannot approve
thiefClient.execute(contractAddress, approve)// but fred can (and moves money to bob)
fredClient.execute(contractAddress, approve);// verify bob got the tokens
fredClient.getAccount(bobAddr);// verify contract lost
fredClient.getAccount(contractAddress);