Smart Contracts (in Rust) 5-day study plan
Learn Stellar Smart Contracts and Soroban SDK
The idea is to take good steps forward after doing the increment, log, error examples, and using Okashi.dev
We’ll use Soroban version 22.0 which is new and improved ✅
5 day study guide
Day 1: Minting a Token
Objective: Learn how to create a smart contract for minting and managing a token.
- What to Learn:
- Define custom token types in Soroban.
- Use Soroban’s storage and contract state for tracking balances.
- Implement
mint
functionality with access control.
#![no_std]
use soroban_sdk::{contract, contractimpl, short_symbol, Env, Symbol};
// Define storage keys using `short_symbol!`
const NAME: Symbol = short_symbol!("name");
const SYMBOL: Symbol = short_symbol!("symbol");
const TOTAL_SUPPLY: Symbol = short_symbol!("total");
// Define the contract struct
#[contract]
pub struct CustomToken;
- Steps:
- Create a
Token
struct with fields likeowner
andtotal_supply
. - Write a
mint
function that checks if the caller is the contract owner before adding tototal_supply
. - Add a
balance_of
function to query balances. Use Map
- Create a
- Additional Focus:
- Practice error handling for unauthorized minting.
- Use
env.log
to debug minting events.
- Resources:
- Soroban documentation on custom types and storage.
- Rust book chapter on structs and ownership.
Day 2: Time-Locking a Contract
Objective: Create a contract that only allows interactions after a specific block time.
- What to Learn:
- Use Soroban’s built-in time functions.
- Implement conditional logic based on block timestamps.
- Steps:
- Write a
set_timelock
function that stores a timestamp in contract state. - Write a function that requires the current block time to exceed the timelock before executing.
- Write a
- Additional Focus:
- Ensure state updates are atomic.
- Explore edge cases like block time being too far in the future.
- Resources:
- Soroban documentation on time-related operations.
- Rust
if
andmatch
expressions for conditionals.
Day 3: Escrow (Alice Paying Bob)
Objective: Implement a basic escrow system where Alice deposits funds, and Bob can withdraw them under certain conditions.
- What to Learn:
- Use multi-user contract logic.
- Implement checks for deposit, withdrawal, and state transitions.
- Steps:
- Write a
deposit
function for Alice to add funds. - Implement a
release
function for Alice to approve the release of funds to Bob. - Add a
withdraw
function for Bob to claim the funds.
- Write a
- Additional Focus:
- Practice implementing access control logic.
- Handle reentrancy or state transition errors.
- Resources:
- Soroban examples on user authentication and state.
- Rust book chapter on enums (useful for defining contract states).
enum Option<T> {
None,
Some(T),
}
Day 4: Simple Auction
Objective: Create an auction smart contract where users can bid on an item.
- What to Learn:
- Use Soroban storage for tracking bids and highest bidder.
- Implement logic to handle outbidding and refunds.
- Steps:
- Write a function to place a bid, updating the highest bidder and bid amount.
- Add logic to refund the previous highest bidder.
- Include a function to close the auction and release funds to the seller.
- Additional Focus:
- Manage storage efficiently for multiple bidders.
- Ensure that refunds are securely handled.
- Resources:
- Soroban documentation on storage and logs.
- Rust error handling with
Result
.
Day 5: Advanced Token Features (Burning, Transferring, Allowances)
Objective: Extend the token contract from Day 1 to include advanced functionality.
- What to Learn:
- Implement token burning to reduce supply.
- Add a
transfer
function for peer-to-peer transfers. - Create an
approve
andtransfer_from
pattern for delegated transactions.
- Steps:
- Add a
burn
function to reduce balances andtotal_supply
. - Implement a
transfer
function with balance checks. - Extend the contract with
approve
andtransfer_from
functions for allowance-based transactions.
- Add a
- Additional Focus:
- Test for edge cases like transferring to oneself or approving zero amounts.
- Practice unit testing in Rust for these functions.
- Resources:
- ERC-20 documentation for inspiration.
- Rust book chapter on iterators for handling balances and approvals.
General Tips for Each Day
- Write unit tests for every function you implement.
- Use
env.log
to debug and understand contract execution flow. - Gradually refactor your contracts to follow best practices for Rust and Soroban.