Smart Contracts

Welcome to the Tutorials section of Real’s documentation! Here, you’ll find practical guides to help you build on the Real blockchain, a decentralized Layer 1 (L1) network built with the Cosmos SDK. This tutorial focuses on writing smart contracts, empowering developers to create decentralized applications (dApps) and tokenized real-world assets (RWAs) on Real.


Writing Smart Contracts on Real

The Real blockchain, as an L1 built with the Cosmos SDK, supports smart contracts through CosmWasm, a secure and efficient framework for writing smart contracts in Rust. This tutorial will guide you through the process of writing, compiling, and deploying a simple smart contract on Real.

Prerequisites

Before you begin, ensure you have the following:

  • Rust: Install Rust and Cargo (Rust’s package manager) by following the official Rust installation guide.

  • CosmWasm Tools: Install the cosmwasm tools for compiling and optimizing smart contracts. Run:

cargo install cosmwasm-check
  • Real Node Setup: Set up a local Real node or connect to the Real Testnet. Follow our Node Setup Guide for instructions.

  • Real CLI: Install the Real CLI (reald) to interact with the blockchain. Download it from our Downloads page.

  • Wallet: Set up a Real wallet with some testnet REAL tokens. Request tokens from the Real Testnet Faucet.

Step 1: Set Up Your Development Environment

  1. Create a New Project: Start by creating a new Rust project for your smart contract.

    cargo new --lib real-first-contract
    cd real-first-contract
  2. Update Cargo.toml:

Add the necessary dependencies for CosmWasm. Edit your Cargo.toml to include:

  1. Initialize Your Contract Structure:

CosmWasm contracts typically have three main entry points: instantiate, execute, and query. Create the basic structure in src/lib.rs.

Step 2: Write a Simple Smart Contract

Let’s write a basic smart contract that stores a counter value and allows users to increment it. This example demonstrates the core concepts of CosmWasm on Real.

Replace the contents of src/lib.rs with the following code:

What This Contract Does:

  • Instantiate: Initializes the contract with a starting count value.

  • Execute: Allows users to increment the counter with the Increment message.

  • Query: Lets users query the current count value with the GetCount message.

Step 3: Compile and Optimize Your Contract

  1. Compile the Contract:

Build the contract to ensure there are no errors:

  1. Optimize the Wasm File:

Use the CosmWasm optimizer to reduce the contract size for deployment:

If successful, the optimized .wasm file will be ready in the target directory.

Step 4: Deploy the Smart Contract on Real

  1. Store the Contract Code:

Use the Real CLI to upload the compiled contract to the blockchain. Replace <YOUR_WALLET>with your wallet name:

Note the code_id returned by this command (e.g., 1).

  1. Instantiate the Contract:

Deploy an instance of the contract with an initial count of 0:

Note the contract_address returned (e.g., real1...).

Interact with the Contract:

  • Increment the Counter:

  • Query the Counter:

    This should return the current count (e.g., {"count": 1} after one increment).

Step 5: Test and Debug

  • Run Unit Tests: Add unit tests in src/lib.rs to verify your contract’s functionality. Use the cosmwasm_std::testing module for mock dependencies.

  • Check Logs: Use the Real Block Explorer explorer.realfin.io to view transaction logs and ensure your contract behaves as expected.

  • Iterate: Debug any issues by checking error messages in the CLI output or explorer.

Next Steps

  • Explore Advanced Features: Learn how to integrate RWA tokenization logic into your smart contracts with our Tokenization Guide.

  • Use Real’s SDK: Leverage the Cosmos SDK’s modules (e.g., x/bank, x/staking) to add more functionality to your dApps. See our SDK Documentation.

  • Join the Community: Share your contract and get feedback on our X Channel (Twitter) or Telegram.

Additional Resources


Need Help?

If you have questions or need assistance, reach out to our team:

Happy coding on Real – let’s build the future of decentralized finance together!

Last updated