Back
+20 XP
4/12
Challenge: Build a Solana Account Struct
Apply what you've learned about Rust ownership, structs, and methods. You'll create a SolanaAccount struct that models an on-chain account with balance management.
Your Task
Complete the SolanaAccount struct and its methods, then implement create_and_withdraw which:
- Creates a new
SolanaAccountusingSolanaAccount::new(owner, initial_balance) - Attempts to withdraw
withdraw_amount - Returns a
Stringdescribing the result
Requirements:
new()creates an account that is not frozen by defaultwithdraw()returnsErr("Account is frozen")if the account is frozenwithdraw()returnsErr("Insufficient balance")ifamount > balancewithdraw()subtracts the amount and returnsOk(remaining_balance)create_and_withdrawformats the result as"OK:<remaining>"or"ERR:<message>"
Rust Concepts Used:
- Structs with
implblocks Result<T, E>return type- Pattern matching with
match - String formatting with
format!()
Test Cases
Withdrawing 300 from 1000 should leave 700
Input:
"Alice", 1000, 300Expected: "OK:700".to_string()Withdrawing more than balance should return an error
Input:
"Bob", 500, 600Expected: __result__.starts_with("ERR:")