Submit the code of a smart contract to chain and get its code id
PreRequisite
Make sure you follow the compile a contract guide first for your project. Once you have a contract compiled in the artifacts/*.wasm directory, you are ready for this guide.
This guide will show you how to deploy and interact with a contract using CLI. For scripting using Rust, you can use cw-orchestrator.
Upload
You need to upload the contract via the CLI to chain. To do this, perform the following
We can see both raw_log and also logs[0].events[1].store_code shows the code_id being 13. If you wish the automate this return code in bash to a variable, you can
With the code now being up on chain, we can now run logic to setup our own copy of the contract which we control. This will then give us a unique contract address for others to interact with in accordance with the contract logic. This example is from the cosmwasm/cw-template.
Ensure you change CODE_ID to match your code id from the store code
# ManualCODE_ID=1junodtxwasminstantiate"$CODE_ID"'{"count":0}'--label"some-contract" $FLAGS -y--admin<your-address-here># then query the tranasaction hash as we do above.# Automated return of the contract addressCODE_ID=1TX_INIT=$(junod tx wasm instantiate "$CODE_ID" '{"count":0}' --label "contract" $FLAGS -y --admin <your-address-here> | jq -r '.txhash') && echo $TX_INIT
CONTRACT_ADDR=$($BINARY query tx $TX_INIT --output json | jq -r '.logs[0].events[0].attributes[0].value') && echo "CONTRACT_ADDR: $CONTRACT_ADDR"