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:

  1. Set up your environment: Ensure that you have Rust and the necessary development tools installed. You can follow CosmWasm's setup guide.

  2. 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.

  3. 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 of ExecuteMsgs, especially since these messages may be inserted into Layer 1 memos for CosmWasm callbacks. This will ensure efficient handling and smaller payloads.

  4. 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.

  1. Write unit tests: Add your test functions within the tests module in your contract.

    #[cfg(test)]
    mod tests {
        use super::*;
        // Your test cases here
    }
  2. Test execution: Run your tests using the command:

    cargo test

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.

  1. Set up multitest: Include the cw-multi-test package in your Cargo.toml dependencies.

  2. Simulate complex scenarios: Write tests that involve message execution, querying, and contract-to-contract interactions.

  3. Run multitest: Execute your multitest suite using:

    cargo test --features multitest

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

  1. 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.

  2. 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

  1. Compile your contract to WASM:

    cargo wasm
  2. 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:

  1. Registry check: The registry will verify that the deployer is allowed to instantiate a specific contract version, using the contract’s computed checksum.

  2. MsgStoreCore: If the checksum matches the registry entry, the deployer will be granted permission to store the contract on-chain.

  3. 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

  1. Ensure your contract checksum matches the registry.

  2. Submit MsgStoreCore with the correct checksum to store your contract.

  3. 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