Deploy a Smart Contract
Clone cw-template
For this example, we will use the cw-template repo with counter example.
Select false
Compile the wasm contract
To deploy smart contracts, you must compile the code and make it an executable wasm binary file. We will compile the wasm contract with stable toolchain.
Compile using the command below:
After this compiles, it should produce a file in target/wasm32-unknown-unknown/release/my_first_contract.wasm
. If you check the size of the file by using the ls -lh
command, it shows around 1.9M
. This is a release build, but not stripped of all unneeded code. To produce a much smaller version, you can run this which tells the compiler to strip all unused code out:
This produces a file about 155K
. To reduce gas costs, the binary size should be as small as possible. This will result in a less costly deployment, and lower fees on every interaction. Also, if you don’t use compilation optimization, CosmWasm smart contract will not be deployed well due to exceeds limit
error.
Optimized Compilation
You can do further optimization using rust-optimizer. rust-optimizer produces reproducible builds of CosmWasm smart contracts and does heavy optimization on the build size, using binary stripping and wasm-opt
.
Binary file will be at artifacts/my_first_contract.wasm
folder and its size will be about 130K
, which is more smaller than when only RUTFLAGS
was used.
Store to BitSong Cosmwasm Testnet
We have the wasm binary executable ready. Now it is time to store the code to the BitSong Cosmwasm Testnet.
bitsongd tx wasm store
: upload a wasm binary--from
: name or address of private key with which to sign.--gas-prices
: gas prices in decimal format to determine the transaction fee.--gas
: gas limit to set per-transaction. set to “auto” to calculate sufficient gas automatically--gas-adjustment
: adjustment factor to be multiplied against the estimate returned by the tx simulation.-y
: to skip tx broadcasting prompt confirmation.--output
: output format.-b
: transaction broadcasting mode
Once that is complete, you can get the CODE_ID
easily using jq
.
jq
is an open source that helps extract data from JSON. Install it according to your OS using the following command:
Run the following command to set the CODE_ID
as a variable:
Instantiate the contract
We can now create an instance of this wasm contract. First, set the initial state of the instance in the INIT
variable and run the instantiate command
.
bitsongd tx wasm instantiate
: instantiate a wasm contract using CODE_ID of the uploaded binary.--label
: human-readable name for this contract in lists.--no-admin
: you must set this explicitly if you don’t want an admin.
Get the contract address using the command following:
Query the contract
Now, let’s see if the contract we deployed works well.
Get contract’s count
Send a get_count
query to check the count value. The previously set INIT
state is output as it is.: {"data":{"count":100}}
The output will be
bitsongd query wasm contract-state smart
: calls contract with given address with query data and prints the returned result
Execute the Contract
Increment contract’s count
This time, let’s send an increment
transaction that increases the count value by +1. Because the transaction changes the internal state of the contract, you must pay gas fees.
If you run the get_count
query again after sending the increment
transaction, you can see that +1 has increased from the previous count value.
Reset contract’s count
Lastly, let’s send a reset
transaction. Like increment, reset transaction also changes the internal state of contract, so you must pay gas fees.
Last updated