A production-ready fungible token smart contract built with Soroban (Stellar's smart contract platform), implementing the CAP-46 standard for token interoperability.
Token BDB is a fully-featured ERC-20-like token implementation on Stellar, compatible with wallets, DEXs, and the entire Stellar ecosystem. It follows best practices for security, gas efficiency, and storage optimization. Made For Codigo Futura by Buen dia Builders.
- β CAP-46 Standard Compliance - Full compatibility with Stellar's fungible token standard
- π Access Control - Admin-only minting with proper authorization checks
- π° Core Token Operations - Transfer, approve, allowance, mint, and burn
- π Rich Event Emission - Detailed events for indexers and explorers
- β‘ Gas Optimized - Smart storage management with automatic cleanup
- π‘οΈ Overflow Protection - Safe arithmetic operations with checked math
- β° TTL Management - Automatic storage lifetime extension
- π― Zero-balance Optimization - Automatic key removal for efficiency
src/
βββ lib.rs # Main contract implementation
βββ storage.rs # Storage keys and data structures
βββ errors.rs # Custom error definitions
βββ test.rs # Unit tests
Admin- Contract administrator addressTokenName- Full token name (max 100 chars)TokenSymbol- Token ticker symbol (max 32 chars)Decimals- Token precision (max 18, typically 7 for Stellar)TotalSupply- Total tokens in circulationInitialized- Initialization flag
Balance(Address)- Individual user balancesAllowance(Address, Address)- Spending permissions between accounts
fn initialize(
env: Env,
admin: Address,
name: String,
symbol: String,
decimals: u32
) -> Result<(), TokenError>Initializes the token with metadata. Can only be called once.
Creates new tokens and adds them to recipient's balance. Admin only.
Destroys tokens from the specified account. Requires owner authorization.
Transfers tokens between accounts. Requires sender authorization.
Grants spending permission to another address. Set to 0 to revoke.
Transfers tokens on behalf of another user using approved allowance.
balance(env, account)- Returns account balanceallowance(env, from, spender)- Returns approved spending amountname(env)- Returns token namesymbol(env)- Returns token symboldecimals(env)- Returns decimal placestotal_supply(env)- Returns total token supplyadmin(env)- Returns administrator address
- β Amount must be positive (> 0)
- β No self-transfers allowed
- β Metadata length validation
- β Decimal places capped at 18
- π Admin signature required for minting
- π Owner signature required for transfers and burns
- π Spender signature required for transfer_from
- β All arithmetic uses
checked_add/checked_sub - π‘οΈ Returns
OverflowErroron overflow detection
| Error | Code | Description |
|---|---|---|
AlreadyInitialized |
1 | Contract already initialized |
InvalidAmount |
2 | Amount must be > 0 |
InsufficientBalance |
3 | Not enough tokens |
InsufficientAllowance |
4 | Allowance too low |
NotInitialized |
5 | Contract not initialized |
InvalidDecimals |
6 | Decimals > 18 |
OverflowError |
7 | Arithmetic overflow |
InvalidRecipient |
8 | Cannot transfer to self |
InvalidMetadata |
9 | Invalid name/symbol |
The contract emits rich events for all operations:
init- Token initializationmint- Token creationburn- Token destructiontransfer- Token transfersapprove- Allowance approvalstrnsf_frm- Delegated transfers
Each event includes relevant data like amounts, balances, and addresses.
cargo build --target wasm32-unknown-unknown --releasecargo testsoroban contract deploy \
--wasm target/wasm32-unknown-unknown/release/token_bdb.wasm \
--network testnet// Initialize the token
token.initialize(
&env,
&admin,
String::from_str(&env, "Buen DΓa Builders Token"),
String::from_str(&env, "BDB"),
7
);
// Mint tokens to a user
token.mint(&env, &user, &1_000_0000000); // 1000 tokens with 7 decimals
// Transfer tokens
token.transfer(&env, &user, &recipient, &100_0000000); // 100 tokens
// Approve spending
token.approve(&env, &user, &spender, &50_0000000); // 50 tokens
// Transfer on behalf
token.transfer_from(&env, &spender, &user, &recipient, &25_0000000);- β Check-Effects-Interactions Pattern - State updates before external calls
- β Storage Optimization - Remove zero-balance entries
- β TTL Management - Extend lifetime for active accounts (100k-200k ledgers)
- β Event-Driven Architecture - Rich events for off-chain indexing
- β Fail-Fast Validation - Early input validation to save gas
- β Atomic Operations - All state changes within single transaction
- Uses 7 decimals (Stellar standard) by default
- Instance storage for global data (cheaper than persistent)
- Persistent storage with TTL for user data
- Short symbol names for events (max 9 chars)
- Efficient key removal on zero balances
Contributions are welcome! Please open an issue or pull request.
Built with β€οΈ for Codigo Futura by Buen dia Builders for Stellar ecosystem# token_bdb