Development Process
THORChain builds in the open—and Rujira is no exception—hence make sure to check the README file on Gitlab before starting up development on Rujira.
Start Developing
This guide will walk you through the development process for building, testing, and deploying CosmWasm contracts on Rujira. It covers everything from writing your contract, performing unit and integration testing, deploying to test environments, and finally deploying to mainnet.
1. Writing Your Contract
Rujira contracts are built using CosmWasm, a smart contract platform written in Rust. Follow the steps below to get started writing your contract:
Set up your environment: Ensure that you have Rust and the necessary development tools installed. You can follow CosmWasm's setup guide.
Contract Structure: CosmWasm contracts typically consist of the following components:
InstantiateMsg: Defines the parameters required to initialize the contract.
ExecuteMsg: Represents various actions that users can invoke on the contract.
QueryMsg: Represents read-only operations that fetch contract state.
Serialization: Contracts in CosmWasm use
#[cw_serde]
for serialization and deserialization. Rujira may introduce a modified implementation of#[cw_serde]
to provide a more compact encoding ofExecuteMsg
s, especially since these messages may be inserted into Layer 1 memos for CosmWasm callbacks. This will ensure efficient handling and smaller payloads.Best Practices:
Organize your code into modular functions for better readability and maintenance.
Use meaningful types and clear names for your messages and state variables.
Ensure that your contract handles errors gracefully and follows security best practices to prevent issues like reentrancy.
For more details on writing CosmWasm contracts, refer to the official CosmWasm documentation.
2. Testing
Before deploying your contract, it’s crucial to write and execute tests. CosmWasm provides a robust testing framework within Rust to simulate contract behavior and ensure correctness.
Unit Tests
Unit tests validate individual functions within your contract. Use these tests to check that your logic is working as expected for different scenarios, without the need for a full blockchain simulation.
Write unit tests: Add your test functions within the
tests
module in your contract.Test execution: Run your tests using the command:
Integration Testing with Multitest
For more comprehensive testing, you can use CosmWasm’s Multitest framework, which simulates a chain environment in Rust. This allows you to test your contract’s interaction with other contracts and the chain itself.
Set up multitest: Include the
cw-multi-test
package in yourCargo.toml
dependencies.Simulate complex scenarios: Write tests that involve message execution, querying, and contract-to-contract interactions.
Run multitest: Execute your multitest suite using:
Multitest provides a highly realistic environment, simulating actual blockchain behavior without the need for a full node or deployment to a testnet.
3. Test Deployment
After validating your contract through unit and integration tests, the next step is deploying it to a test environment. Rujira offers several options for deploying your contract for further testing.
Testnet Deployment
Permissionless deployment: Most developers will want to deploy to a permissionless testnet to verify their contract in a real end-to-end environment. You can use the CosmWasm binary to handle deployments, or utilize the Kujira Developer UI to interact with CosmWasm contracts via a web interface.
Mocknet and Stagenet:
Mocknet: A permissioned environment may be provided for quick mock deployments. This will allow fast iteration during the development process.
Stagenet: If Stagenet is permissioned, you'll need appropriate permissions for deployment. This will closely resemble the mainnet environment.
Rujira's UI will provide a convenient interface for uploading contracts, instantiating them, and managing contract states, making it easy for developers to test their contracts in a familiar setting.
Deployment Process
Compile your contract to WASM:
Use the Kujira Developer UI or command-line tools to upload and instantiate your contract.
4. Mainnet Deployment
Once your contract has been thoroughly tested, it’s time to deploy it to the Rujira mainnet. This process involves additional security checks and permissions to ensure the integrity of the contract on the network.
Registry and Permissions
For mainnet (and possibly Stagenet), Rujira will maintain an on-chain registry that maps each contract’s checksum to its deployer. This registry will grant permissions for instantiation based on the contract’s checksum rather than its code ID.
The process will involve:
Registry check: The registry will verify that the deployer is allowed to instantiate a specific contract version, using the contract’s computed checksum.
MsgStoreCore: If the checksum matches the registry entry, the deployer will be granted permission to store the contract on-chain.
MsgInstantiateContract: Once the contract is stored, the deployer can instantiate it with the appropriate parameters.
By tying instantiation permissions to the contract checksum, Rujira ensures that only approved contract versions can be deployed on mainnet, improving security and reliability.
Deployment Steps
Ensure your contract checksum matches the registry.
Submit MsgStoreCore with the correct checksum to store your contract.
Use MsgInstantiateContract to deploy your contract instance on mainnet.
Conclusion
The Rujira development process offers a streamlined workflow for writing, testing, and deploying CosmWasm contracts. By following the steps outlined above, you’ll ensure your contract is robust and ready for deployment on Rujira’s mainnet. Be sure to make use of the tools provided, such as the Kujira Developer UI, to simplify your deployment tasks.
Last updated