BaseTreasury is an abstract base contract that provides common functionality for all treasury implementations in the Oak Network protocol. It defines the standard interface for fee distribution, fund management, and campaign state handling that all treasury types inherit.
Overview
Key Features
- Abstract Base Contract: Extended by AllOrNothing and other treasury types
- Fee Distribution: Standard fee disbursement logic
- Access Control: Inherits campaign-specific access control
- State Management: Pausable and cancellable functionality
- Success Condition: Abstract method for funding model-specific logic
- Safe Token Handling: Uses SafeERC20 for secure transfers
Constants
PERCENT_DIVIDER
ZERO_BYTES
State Variables
Platform Configuration
| Variable | Type | Description |
|---|---|---|
PLATFORM_HASH | bytes32 | Platform identifier |
PLATFORM_FEE_PERCENT | uint256 | Platform fee percentage |
TOKEN | IERC20 | Campaign token contract |
Funding State
| Variable | Type | Description |
|---|---|---|
s_pledgedAmount | uint256 | Total pledged amount |
s_feesDisbursed | bool | Whether fees have been distributed |
Functions
Initialization
Init Base Contract
platformHash: Platform identifierinfoAddress: CampaignInfo contract address
- Initializes campaign access checker
- Sets platform hash
- Loads token address from campaign info
- Sets platform fee percentage
- All treasury implementations in their
initialize()functions
Query Functions
Get Platform Hash
- Platform identifier for this treasury
Get Platform Fee Percent
- Platform fee percentage in basis points
Fee Distribution
Disburse Fees
- Checks success condition (must be met)
- Calculates protocol and platform fee shares
- Transfers protocol fee to protocol admin
- Transfers platform fee to platform admin
- Marks fees as disbursed
- Emits
FeesDisbursedevent
- Campaign must not be paused or cancelled
- Success condition must be met
- Implementation must override
_checkSuccessCondition()
Success Condition Check
- True if campaign succeeded, false otherwise
- AllOrNothing: Returns
totalRaised >= goal
Withdrawal
Withdraw
- Transfers all remaining funds to campaign owner
- Requires fees to be disbursed first
- Emits
WithdrawalSuccessfulevent
- Only callable by campaign owner
- Campaign must not be paused or cancelled
- Fees must be disbursed
Modifiers
When Campaign Not Paused
When Campaign Not Cancelled
Events
FeesDisbursed
WithdrawalSuccessful
SuccessConditionNotFulfilled
Errors
TreasuryTransferFailed
TreasurySuccessConditionNotFulfilled
TreasuryFeeNotDisbursed
TreasuryCampaignInfoIsPaused
Usage Examples
Implementing a Custom Treasury
Fee Distribution Flow
Campaign State Checks
Security Considerations
Safe Token Transfers
- Uses
SafeERC20for all token transfers - Prevents silent failures
- Provides descriptive error messages
Access Control
- Campaign owner only for withdrawal
- Success condition must be met before fee distribution
- Paused/cancelled campaigns cannot disburse fees or withdraw
Fee Calculation
- Fee percentages stored in basis points (1/10000)
- Precise integer math, no rounding errors
- Percentages are immutable once set
State Protection
- Fees can only be disbursed once
- Withdrawal requires fees to be disbursed
- Campaign state checked before all operations
Integration Notes
With CampaignInfo
With GlobalParams
Inheritance Pattern
Best Practices
Custom Success Conditions
State Management
- Always check if fees are disbursed before withdrawal
- Track pledge amounts correctly
- Update state before external calls (CEI pattern)
Error Handling
- Use descriptive error messages
- Check all preconditions
- Emit events for all state changes
Abstract Function Requirements
Must Override
- AllOrNothing: Checks if total >= goal
Optional Override
- Custom fee calculations needed
- Different withdrawal logic required
- Additional state tracking necessary
Next Steps
- AllOrNothing - Implementation example
- CampaignAccessChecker - Access control utilities
- PausableCancellable - State management utilities