Fungible Asset vs Primary Fungible Store Details
1. Fungible Asset
Key Characteristics
module aptos_framework::fungible_asset {
struct FungibleAsset {
metadata: Object<Metadata>,
amount: u64,
}
struct FungibleStore has key {
metadata: Object<Metadata>,
balance: u64,
frozen: bool,
}
}
Functionality
- Low-level Operations
- Direct token manipulation
- Custom store creation
- Flexible store management
- Direct Control
- Manual store creation
- Explicit store handling
- Fine-grained control
- Advanced Features
- Custom store implementations
- Complex token behaviors
- Specialized token logic
Use Cases
-
DeFi Protocols
// Example: Custom liquidity pool store struct LiquidityPoolStore has key { store: FungibleStore, pool_info: PoolInfo, // Additional DeFi-specific fields }
-
Complex Token Systems
// Example: Multi-tier token system struct TieredStore has key { base_store: FungibleStore, tier_level: u8, rewards_multiplier: u64, }
-
Protocol-Specific Implementations
// Example: Staking contract struct StakingStore has key { tokens: FungibleStore, lock_period: u64, rewards_rate: u64, }
2. Primary Fungible Store
Functionality
- High-level Operations
- Automatic store management
- Simplified transfers
- Standard token operations
- User-Focused
- Automatic store creation
- Intuitive interfaces
- Standard workflows
- Standard Features
- Basic transfer operations
- Standard balance management
- Common token functionalities
Use Cases
-
Standard Tokens
// Example: Simple token transfer public fun transfer( from: &signer, to: address, amount: u64 ) { primary_fungible_store::transfer(from, get_metadata(), to, amount); }
-
User Wallets
// Example: Check balance public fun get_balance(account: address): u64 { primary_fungible_store::balance(account, get_metadata()) }
-
Simple Token Systems
// Example: Basic token implementation public fun mint_to(recipient: address, amount: u64) { primary_fungible_store::mint(&mint_ref, recipient, amount); }
3. When to Use
Use Fungible Asset When:
-
Custom Store Logic is Needed
// Example: Custom store with special logic struct CustomStore has key { base_store: FungibleStore, custom_data: vector<u8>, special_rules: Rules, }
-
Implementing Complex Protocols
// Example: AMM implementation struct AMMStore has key { token_a: FungibleStore, token_b: FungibleStore, lp_tokens: FungibleStore, curve_parameters: CurveParams, }
-
Detailed Control is Required
// Example: Custom transfer logic public fun custom_transfer( store: &mut FungibleStore, amount: u64, rules: &TransferRules ) { // Custom validation validate_transfer(store, amount, rules); // Custom transfer logic perform_transfer(store, amount); }
Use Primary Fungible Store When:
-
Implementing Simple Tokens
// Example: Basic token operations public entry fun simple_transfer( from: &signer, to: address, amount: u64 ) { transfer(from, get_metadata(), to, amount); }
-
Focusing on User Experience
// Example: User-friendly interface public entry fun send_tokens( user: &signer, recipient: address, amount: u64 ) { // No need to create store - handled automatically transfer(user, get_metadata(), recipient, amount); }
-
Implementing Standard Token Functionality
// Example: Standard token implementation public entry fun initialize_token( admin: &signer, name: String, symbol: String, decimals: u8 ) { // Standard initialization with automatic store support create_primary_store_enabled_fungible_asset( admin, name, symbol, decimals ); }
4. Best Practices
Fungible Asset Framework
-
Custom Store Design
- Clearly define store structure
- Implement specific rules
- Handle edge cases
-
Security Considerations
// Example: Secure custom store struct SecureStore has key { store: FungibleStore, access_control: AccessControl, audit_trail: AuditLog, }
Primary Fungible Store Framework
-
Simplicity First
- Use standard operations
- Leverage automatic store creation
- Focus on user experience
-
Standard Patterns
// Example: Standard token pattern public fun initialize_standard_token( admin: &signer, config: TokenConfig ) { // Use standard initialization create_standard_token(admin, config); }
5. Decision Matrix
| Criteria | Fungible Asset | Primary Fungible Store | | --- | --- | --- | | Complexity | High | Low | | Flexibility | High | Moderate | | User Experience | Custom | Standardized | | Use Case | Complex DeFi/Custom Logic | Standard Tokens | | Development Effort | High | Low | | Maintenance | Complex | Simple | | Integration | Custom | Standardized |