Skip to content

easyhat/token_bank_simulator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Token Bank Simulator

Overview

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.

Features

  • 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

Project Structure

The project is organized into several modules:

  • wallet.rs: Defines the Wallet 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

Usage Examples

Creating a Bank and Wallets

let mut bank = TokenBank::new();
let alice_wallet = bank.create_wallet("Alice");
let bob_wallet = bank.create_wallet("Bob");

Minting Tokens

// Add 100 tokens to Alice's wallet
if let Err(e) = bank.mint(&alice_wallet, 100.0) {
    println!("Error minting tokens: {:?}", e);
}

Transferring Tokens

// 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"),
}

Viewing Balances

// Print all wallet balances
bank.print_balances();
// Output:
// 📊 Balances:
// Alice: 50.0 tokens
// Bob: 50.0 tokens

Error Handling

The system handles several error types:

  • WalletNotFound: When operations are attempted with non-existent wallets
  • InsufficientFunds: When a transfer is attempted without enough tokens

Technical Implementation

The TokenBank stores balances in a HashMap using Wallet objects as keys. This design allows for:

  1. Efficient lookups via wallet references
  2. Clean ownership separation between bank and wallets
  3. Type-safe token operations

Rust's ownership system ensures thread safety and prevents common bugs like double-spending or accessing invalid wallets.

Tests

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

Future Improvements

  • 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.

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages