Smart Contracts (CosmWasm)
Getting Started
If you're new to CosmWasm and Smart Contracts, we recommend starting with the CW-Template repository. This template will help you understand the basic interfaces and models of CosmWasm smart contracts.
When you're ready to deploy your smart contract, you’ll need a local build of the chain core. This will allow you to store your code on-chain and submit governance proposals to instantiate it.
Installation Steps
Clone the Rujira core repository:
git clone https://gitlab.com/thornode/thorchain/app-layer/ cd core make install
Deployment
Now that you've set up the environment, you're ready to store your code on-chain.
Step 1: Optimize the Code
To optimize your contract's code, run the following command:
cargo run-script optimize
Step 2: Create a Local Account
You’ll need a local account to handle transactions.
rujirad keys add <local-account>
This will generate a seed phrase and an associated Kujira address for your account. Make sure to store the seed phrase safely. You’ll also need to fund this address with KUJI tokens before proceeding to the next step.
Step 3: Store Code on Chain
To store the optimized code on-chain, run the following:
rujirad tx wasm store <./path/to/optimized/code.wasm> --from <local-account>
Upon successful execution, logs will be generated. These logs contain the code_id
of your contract, which will look something like this:
logs:
- events:
- attributes:
- key: action
value: /cosmwasm.wasm.v1.MsgStoreCode
- key: module
value: wasm
- key: sender
value: rujira1...
type: message
- attributes:
- key: code_id
value: "4"
type: store_code
Instantiation
Your code is now stored on-chain and behaves similarly to a class in an object-oriented language. Next, you need to instantiate the contract so you can interact with it.
On Kujira, instantiation must be done via a governance proposal. This ensures the quality and security of code and dApps on the network.
Step 1: Submit a Governance Proposal
Run the following command to submit an instantiation proposal:
rujirad tx gov submit-proposal instantiate-contract 4 \
'{"count": 0}' \
--title "Instantiate CW-Template" \
--description "A more detailed description of this proposal" \
--label "CW-Template" \
--from <local-account> \
--admin <local-account-address> \
--run-as <local-account-address> \
--gas 1000000 \
--fees 1250ukuji
This creates a proposal at the funding stage. You can then visit Rujira Governance to deposit funds and open it for voting.
Step 2: Retrieve Contract Address
After the proposal passes and the contract is instantiated, the contract will have its own unique address. You can find the contract address with the following query:
rujirad query wasm list-contract-by-code 4
Once the contract is instantiated, you're ready to move on to building the UI for your newly deployed smart contract.
Last updated