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
mintfunctionality 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
Tokenstruct with fields likeownerandtotal_supply. - Write a
mintfunction that checks if the caller is the contract owner before adding tototal_supply. - Add a
balance_offunction to query balances. Use Map
- Create a
- Additional Focus:
- Practice error handling for unauthorized minting.
- Use
env.logto 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_timelockfunction 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
ifandmatchexpressions 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
depositfunction for Alice to add funds. - Implement a
releasefunction for Alice to approve the release of funds to Bob. - Add a
withdrawfunction 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
transferfunction for peer-to-peer transfers. - Create an
approveandtransfer_frompattern for delegated transactions.
- Steps:
- Add a
burnfunction to reduce balances andtotal_supply. - Implement a
transferfunction with balance checks. - Extend the contract with
approveandtransfer_fromfunctions 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.logto debug and understand contract execution flow. - Gradually refactor your contracts to follow best practices for Rust and Soroban.


