Token Bank Simulator is a Rust-based project that simulates a simple blockchain-like token bank system. It allows users to create wallets, mint tokens, and transfer tokens between wallets. This project demonstrates fundamental Rust concepts including ownership, error handling, and data structures.
- Wallet Management: Create unique wallets identified by owner names
- Token Operations: Mint new tokens and transfer existing tokens between wallets
- Balance Tracking: Monitor token balances for all wallets in the system
- Error Handling: Proper error handling for common scenarios like insufficient funds
The project is organized into several modules:
wallet.rs
: Defines theWallet
struct for identifying token owners- token_bank.rs: Implements the core banking logic including transfers and balance management
errors.rs
: Custom error types for banking operations
let mut bank = TokenBank::new();
let alice_wallet = bank.create_wallet("Alice");
let bob_wallet = bank.create_wallet("Bob");
// Add 100 tokens to Alice's wallet
if let Err(e) = bank.mint(&alice_wallet, 100.0) {
println!("Error minting tokens: {:?}", e);
}
// Transfer 50 tokens from Alice to Bob
match bank.transfer(&alice_wallet, &bob_wallet, 50.0) {
Ok(()) => println!("Transfer successful!"),
Err(BankError::InsufficientFunds) => println!("Not enough tokens to transfer"),
Err(BankError::WalletNotFound) => println!("One of the wallets doesn't exist"),
Err(_) => println!("Unknown error occurred during transfer"),
}
// Print all wallet balances
bank.print_balances();
// Output:
// 📊 Balances:
// Alice: 50.0 tokens
// Bob: 50.0 tokens
The system handles several error types:
WalletNotFound
: When operations are attempted with non-existent walletsInsufficientFunds
: When a transfer is attempted without enough tokens
The TokenBank
stores balances in a HashMap using Wallet
objects as keys. This design allows for:
- Efficient lookups via wallet references
- Clean ownership separation between bank and wallets
- Type-safe token operations
Rust's ownership system ensures thread safety and prevents common bugs like double-spending or accessing invalid wallets.
The project includes comprehensive tests for all core functionality:
- Wallet creation
- Token minting
- Token transfers with both successful and error cases
To run tests:
cargo test
- Add transaction history tracking
- Implement multi-signature wallets
- Add support for different token types
- Improve error handling with more specific error types
- Add serialization/deserialization for persistence.