Fee Model
Fee Model - Transaction Fees and Economics
This document describes the fee model, fee calculation, and economic incentives in the Roru Protocol.
Fee Structure
Fee Components
Fee Types:
Base Fee: Fixed fee per transaction
Size Fee: Fee based on transaction size
Priority Fee: Optional priority fee
Network Fee: Chain-specific network fees
Fee Format:
pub struct TransactionFees {
pub base_fee: u64,
pub size_fee: u64,
pub priority_fee: Option<u64>,
pub network_fee: u64,
pub total: u64,
}Fee Calculation
Base Fee
Base Fee Calculation:
fn calculate_base_fee() -> u64 {
BASE_FEE_CONSTANT // Fixed base fee
}Size Fee
Size Fee Calculation:
fn calculate_size_fee(tx_size: usize) -> u64 {
let size_in_kb = (tx_size + 1023) / 1024; // Round up to KB
size_in_kb * SIZE_FEE_PER_KB
}Priority Fee
Priority Fee Calculation:
fn calculate_priority_fee(priority: Priority) -> u64 {
match priority {
Priority::Low => 0,
Priority::Normal => NORMAL_PRIORITY_FEE,
Priority::High => HIGH_PRIORITY_FEE,
}
}Network Fee
Network Fee Calculation:
async fn calculate_network_fee(
adapter: &dyn ChainAdapter,
tx: &SettlementTx,
) -> Result<u64> {
adapter.estimate_fees(tx).await
}Total Fee Calculation
Calculation Process
Total Fee:
fn calculate_total_fee(
tx_size: usize,
priority: Priority,
network_fee: u64,
) -> u64 {
let base = calculate_base_fee();
let size = calculate_size_fee(tx_size);
let priority = calculate_priority_fee(priority);
base + size + priority + network_fee
}Fee Payment
Payment Methods
Payment Options:
Dedicated Fee Note: Separate note for fees
Change Output: Fees from change
Public Fee: Public fee payment
Asset-Specific: Pay in specific asset
Fee Note Creation
Fee Note:
fn create_fee_note(fee: u64) -> Note {
Note {
value: fee,
recipient: FEE_ADDRESS,
randomness: generate_randomness(),
nullifier_key: generate_nullifier_key(),
timestamp: current_timestamp(),
asset_id: FEE_ASSET,
}
}Economic Model
Fee Distribution
Distribution:
Infrastructure providers
Prover nodes
Relayer nodes
Protocol treasury (optional)
Distribution Code:
fn distribute_fees(fees: &TransactionFees) -> FeeDistribution {
FeeDistribution {
infrastructure: fees.total * 0.4,
provers: fees.total * 0.3,
relayers: fees.total * 0.2,
treasury: fees.total * 0.1,
}
}Dynamic Fee Adjustment
Fee Adjustment
Adjustment Factors:
Network congestion
Demand
Supply
Historical data
Adjustment Algorithm:
fn adjust_fees(base_fees: &TransactionFees, congestion: f64) -> TransactionFees {
let multiplier = 1.0 + (congestion * 0.5); // Up to 50% increase
TransactionFees {
base_fee: (base_fees.base_fee as f64 * multiplier) as u64,
size_fee: (base_fees.size_fee as f64 * multiplier) as u64,
priority_fee: base_fees.priority_fee,
network_fee: base_fees.network_fee,
total: calculate_total(&base_fees) * multiplier as u64,
}
}Fee Estimation
Estimation Process
Estimation Steps:
Estimate transaction size
Get current network fees
Calculate base fees
Apply priority
Return estimate
Estimation Code:
async fn estimate_fees(
tx: &Transaction,
priority: Priority,
adapters: &[ChainAdapter],
) -> Result<FeeEstimate> {
let tx_size = estimate_transaction_size(tx);
let base = calculate_base_fee();
let size = calculate_size_fee(tx_size);
let priority_fee = calculate_priority_fee(priority);
let mut network_fees = Vec::new();
for adapter in adapters {
let network_fee = adapter.estimate_fees(&create_settlement(tx)?).await?;
network_fees.push(network_fee);
}
let total_network = network_fees.iter().sum();
Ok(FeeEstimate {
base_fee: base,
size_fee: size,
priority_fee,
network_fees,
total: base + size + priority_fee + total_network,
})
}Fee Optimization
Optimization Strategies
Strategies:
Batch transactions
Optimize transaction size
Choose optimal priority
Time transactions
Batch Optimization
Batch Fees:
fn calculate_batch_fees(txs: &[Transaction]) -> u64 {
let base = calculate_base_fee(); // One base fee for batch
let total_size = txs.iter().map(|tx| estimate_size(tx)).sum();
let size_fee = calculate_size_fee(total_size);
base + size_fee // Reduced fees for batching
}Fee Refunds
Refund Scenarios
Refund Cases:
Transaction failure
Network errors
Invalid transactions
Timeout
Refund Process:
fn process_refund(failed_tx: &Transaction) -> Result<Refund> {
// Refund fees for failed transaction
let refund_amount = calculate_fees(failed_tx).total;
create_refund_note(refund_amount)
}Fee Transparency
Fee Display
Display Format:
Breakdown by component
Total fee
Fee in different units
Historical comparison
Fee Reporting
Reporting:
Per-transaction fees
Aggregate fees
Fee trends
Cost analysis
Conclusion
The fee model provides:
Fairness: Transparent fee structure
Efficiency: Optimized fees
Flexibility: Multiple payment methods
Economics: Sustainable model
Transparency: Clear fee breakdown
Understanding fees is essential for economic analysis.
Last updated
