Software Engineer | OSS contributor Matias Telegram |
- π Prerequisites
- π₯οΈ Environment Setup
- π³ Wallet Configuration
- π Compilation and Deployment
- π΅π» Testing and Execution
- π© Practical Example
- π©Ί Troubleshooting
Before getting started, make sure you have the following installed on your system:
- If you using macOS, Linux, or any other Unix-like system:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
-
Download Rust and run
rustup-init.exe
.-
After installing Rust, add the
wasm32-unknown-unknown
target:
rustup target add wasm32-unknown-unknown
-
After installing Rust, add the
- There are a few ways to install the latest version of Stellar CLI.
- Rust allows you to use
cargo
command in the terminal to install the Stellar CLI.
cargo install --locked stellar-cli --features opt
brew install stellar-cli
-
Clone the repository ποΈ:
git clone https://github.com/<username>/Revo-Contracts.git cd ./Revo-Contracts
-
build the smart contract π·ββοΈ:
stellar contract build
-
Run the Tests π΅οΈ:
cargo test
- Install the Stellar Wallet (e.g., Freighter Wallet).
- Create a wallet and save the secret keys π securely.
- Connect wallet to the Stellar test network.
To build the smart contract, run the following command:
stellar contract build
This command will compile the contract and generate a contract.wasm file in the target/deploy directory.
To deploy the smart contract to the Stellar testnet, run the following command:
stellar contract deploy \
--wasm-hash <wasm_hash> \
--source <source_account> \
--network <network>
This command will deploy the contract to the testnet and return the contract's address.
- Assume the following values:
- <wasm_hash>: ./target/wasm32-unknown-unknown/release/stellar_smart_contract.wasm \
- <source_account>: GBZXN7PIRZGNWCXXFYU7KYWXX4BXZUYHZO5QUEMKRHLUVLYN53WVFG3E
- : testnet
stellar contract deploy \
--wasm ./target/wasm32-unknown-unknown/release/stellar_smart_contract.wasm \
--source GBZXN7PIRZGNWCXXFYU7KYWXX4BXZUYHZO5QUEMKRHLUVLYN53WVFG3E \
--network testnet
Where:
<wasm_hash>
is the hash of the.wasm
file generated during the contract installation.<source_account>
is the account from which the deployment will be made.<network>
is the network you are working on (e.g., testnet).
To run the tests, execute the following command:
cargo test
Fix any errors and re-run the tests.
- Simulate contract calls to ensure correctness:
stellar contract invoke \
--contract-id <contract_id> \
--source <source_account> \
--network <network> \
--function <function_name> \
--args <function_arguments>
Where:
- <contract_id> is the deployed contract ID.
- <function_name> is the function being tested.
Install all prerequisites,If not installed.
Create a new project using the init command to create a soroban-hello-world project.
stellar contract init soroban-hello-world
The init command will create a Rust workspace project structure π©»:
.
βββ Cargo.lock
βββ Cargo.toml
βββ README.md
βββ contracts
βββ hello_world
βββ Cargo.toml
βββ src
βββ lib.rs
βββ test.rs
- add simple contract in
contracts/hello_world/src/lib.rs
:
#![no_std]
use soroban_sdk::{contract, contractimpl, symbol_short, vec, Env, Symbol, Vec};
#[contract]
pub struct HelloContract;
#[contractimpl]
impl HelloContract {
pub fn hello(env: Env, to: Symbol) -> Vec<Symbol> {
vec![&env, symbol_short!("Hello"), to]
}
}
mod test;
- Add test contract file
contracts/hello_world/src/test.rs
:
#![cfg(test)]
use super::*;
use soroban_sdk::{symbol_short, vec, Env};
#[test]
fn test() {
let env = Env::default();
let contract_id = env.register_contract(None, HelloContract);
let client = HelloContractClient::new(&env, &contract_id);
let words = client.hello(&symbol_short!("Dev"));
assert_eq!(
words,
vec![&env, symbol_short!("Hello"), symbol_short!("Dev"),]
);
}
Run cargo test and watch the unit test run. You should see the following output:
cargo test
running 1 test
test test::test ... ok
To build a smart contract to deploy or run, use the stellar contract build command.
stellar contract build
To deploy your HelloWorld contract, run the following command:
stellar contract deploy \
--wasm target/wasm32-unknown-unknown/release/hello_world.wasm \
--source alice \
--network testnet
This returns the contract id CACDYF3CYMJEJTIVFESQYZTN67GO2R5D5IUABTCUG3HXQSRXCSOROBAN
, so replace it with your actual contract id.
run the following command to invoke the hello function.
stellar contract invoke \
--id CACDYF3CYMJEJTIVFESQYZTN67GO2R5D5IUABTCUG3HXQSRXCSOROBAN \
--source alice \
--network testnet \
-- \
hello \
--to RPC
output should appear:
["Hello", "RPC"]
In this example , we learned how to:
- deploy a contract to Testnet
- interact with a deployed contract
-
π¦Rust Installation Issues:
- Ensure
cargo
is in your system PATH.
- Ensure
-
π‘Stellar CLI Errors:
- Verify the version compatibility of the Stellar CLI.
- Use the --help flag to get details of commands:
stellar --help
-
πΈWallet Connectivity:
- Double-check network configuration (testnet/mainnet).