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 ✅

rust-playground

5 day study guide


Day 1: Minting a Token

Objective: Learn how to create a smart contract for minting and managing a token.

  1. 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;
  1. Steps:
    • Create a Token struct with fields like owner and total_supply.
    • Write a mint function that checks if the caller is the contract owner before adding to total_supply.
    • Add a balance_of function to query balances. Use Map
  2. Additional Focus:
    • Practice error handling for unauthorized minting.
    • Use env.log to debug minting events.
  3. Resources:

Day 2: Time-Locking a Contract

Objective: Create a contract that only allows interactions after a specific block time.

  1. What to Learn:
    • Use Soroban’s built-in time functions.
    • Implement conditional logic based on block timestamps.
  2. 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.
  3. Additional Focus:
    • Ensure state updates are atomic.
    • Explore edge cases like block time being too far in the future.
  4. Resources:
    • Soroban documentation on time-related operations.
    • Rust if and match 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.

  1. What to Learn:
    • Use multi-user contract logic.
    • Implement checks for deposit, withdrawal, and state transitions.
  2. 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.
  3. Additional Focus:
    • Practice implementing access control logic.
    • Handle reentrancy or state transition errors.
  4. Resources:

enum Option<T> { 
   None, 
   Some(T), 
}
study rust

Day 4: Simple Auction

Objective: Create an auction smart contract where users can bid on an item.

  1. What to Learn:
    • Use Soroban storage for tracking bids and highest bidder.
    • Implement logic to handle outbidding and refunds.
  2. 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.
  3. Additional Focus:
    • Manage storage efficiently for multiple bidders.
    • Ensure that refunds are securely handled.
  4. 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.

  1. What to Learn:
    • Implement token burning to reduce supply.
    • Add a transfer function for peer-to-peer transfers.
    • Create an approve and transfer_from pattern for delegated transactions.
  2. Steps:
    • Add a burn function to reduce balances and total_supply.
    • Implement a transfer function with balance checks.
    • Extend the contract with approve and transfer_from functions for allowance-based transactions.
  3. Additional Focus:
    • Test for edge cases like transferring to oneself or approving zero amounts.
    • Practice unit testing in Rust for these functions.
  4. 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.