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
cosmwasmtools for compiling and optimizing smart contracts. Run:
cargo install cosmwasm-checkReal 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
Create a New Project: Start by creating a new Rust project for your smart contract.
cargo new --lib real-first-contract cd real-first-contractUpdate
Cargo.toml:
Add the necessary dependencies for CosmWasm. Edit your Cargo.toml to include:
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
countvalue.Execute: Allows users to increment the counter with the
Incrementmessage.Query: Lets users query the current count value with the
GetCountmessage.
Step 3: Compile and Optimize Your Contract
Compile the Contract:
Build the contract to ensure there are no errors:
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
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).
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.rsto verify your contract’s functionality. Use thecosmwasm_std::testingmodule 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
CosmWasm Documentation: cosmwasm.com
Cosmos SDK Guides: docs.cosmos.network
Real API Reference: docs.real.finance/api
Need Help?
If you have questions or need assistance, reach out to our team:
Email: team@real.finance
X: @RealFinOfficial
Telegram: t.me/RealFinanceRWA
Happy coding on Real – let’s build the future of decentralized finance together!
Last updated