AllOrNothing treasury contract implements an “all-or-nothing” crowdfunding model where campaign creators only receive funds if the campaign reaches its goal. If the goal is not met, all backers can claim refunds. This contract also manages a reward system where backers receive NFTs representing their pledges.
Overview
Key Features
- All-or-Nothing Model: Funds only released if goal is met
- Reward System: Backers receive rewards based on pledge amount
- NFT Receipts: ERC721 tokens track backer pledges
- Refund Mechanism: Backers can claim refunds if campaign fails
- Fee Distribution: Protocol and platform fees automatically distributed
- Shipping Support: Separate tracking for pledge amount and shipping fees
State Variables
Pledge Tracking
| Variable | Type | Description |
|---|---|---|
s_tokenToTotalCollectedAmount | mapping(uint256 => uint256) | Total collected amount (pledge + shipping) per token |
s_tokenToPledgedAmount | mapping(uint256 => uint256) | Pledged amount per token ID |
s_tokenIdCounter | Counters.Counter | Counter for generating unique token IDs |
Reward Management
| Variable | Type | Description |
|---|---|---|
s_reward | mapping(bytes32 => Reward) | Reward details by name |
s_rewardCounter | Counters.Counter | Counter for tracking rewards |
Token Metadata
| Variable | Type | Description |
|---|---|---|
s_name | string | Treasury contract name |
s_symbol | string | Treasury contract symbol |
Functions
Initialization
Constructor
- Initializes ERC721 base contract
- Empty name/symbol set in constructor (set during initialization)
Initialize
_platformHash: Platform identifier_infoAddress: CampaignInfo contract address_name: Treasury contract name_symbol: Treasury contract symbol
- Initializes base treasury contract
- Sets name and symbol for ERC721
- Initializes campaign connection
- Can only be called once (initializer modifier)
- Name and symbol must be non-empty strings
Reward Management
Get Reward
rewardName: Reward identifier
- Complete reward structure with value, items, quantities
- Query reward details before pledging
- Check available reward tiers
- Display reward information to users
Add Rewards
rewardNames: Array of reward identifiersrewards: Array of reward structures
- Adds rewards to campaign
- Emits
RewardsAddedevent
- Only callable by campaign owner
- Must be called before campaign launch
- Reward names must be unique
- Arrays must have matching lengths
Remove Reward
rewardName: Reward identifier to remove
- Removes reward from campaign
- Emits
RewardRemovedevent
- Only callable by campaign owner
- Must be before launch
- Reward must exist
Contribution Functions
Pledge For A Reward
rewardName: Reward tier identifieramount: Pledge amount (must meet reward minimum)shippingFee: Shipping fee (can be zero)
tokenId: ERC721 token ID representing pledge
- Transfers tokens from backer
- Mints ERC721 receipt NFT
- Records pledge and shipping amounts
- Emits
Receiptevent
- Campaign must be active (not paused/cancelled)
- Campaign must be between launch and deadline
- Amount must meet minimum for reward tier
- Reward must exist
- Backer must approve token transfer
Check Success Condition
- True if total raised >= campaign goal
- Compares total pledged amount to goal from CampaignInfo
- Used to determine if fees should be disbursed
- All funds remain if condition not met
Withdrawal Functions
Withdraw Funds
- Transfers all collected funds to campaign owner
- Can only be called if campaign successful
- Only callable by campaign owner
- Campaign must have reached goal
- Funds must be successfully disbursed (fees paid)
Claim Refund
tokenId: ERC721 token ID of pledge
- Burns the NFT receipt
- Transfers pledged amount back to backer
- Emits
RefundClaimedevent
- Campaign must not have reached goal
- Token must not already be claimed
- Token must be owned by caller or approved
- Campaign must be past deadline
Fee Distribution
Disburse Fees
- Distributes protocol and platform fees
- Transfers remaining funds to campaign owner
- Marks fees as disbursed
- Campaign must have reached goal
- Fees must not already be disbursed
- Protocol fee → Protocol treasury
- Platform fee → Platform treasury
- Remaining funds → Campaign owner
Query Functions
Get Pledge Amount
tokenId: ERC721 token ID
- Pledged amount for the token
Get Total Collected Amount
tokenId: ERC721 token ID
- Total amount (pledge + shipping) for the token
Get All Rewards
- Array of reward names and corresponding reward structures
- Display all available rewards
- Show complete reward catalog
Name & Symbol
- ERC721 name and symbol
Data Structures
Reward
rewardValue: Minimum pledge amount to receive this rewardisRewardTier: Whether this tier offers physical/digital rewardsitemId: Identifiers of items included in rewarditemValue: Individual item valuesitemQuantity: Number of each item included
Events
Receipt
RewardsAdded
RewardRemoved
RefundClaimed
Errors
AllOrNothingUnAuthorized
AllOrNothingInvalidInput
AllOrNothingNotSuccessful
AllOrNothingNotClaimable
AllOrNothingRewardExists
Usage Examples
Adding Rewards
Making a Pledge
Checking Campaign Status
Claiming Refunds
Withdrawing Funds (Owner)
Querying Rewards
Funding Model Behavior
Successful Campaign
Failed Campaign
Security Considerations
Token Safety
- Uses SafeERC20 for safe token transfers
- No reentrancy vulnerabilities
- Checks-Effects-Interactions pattern
Access Control
- Campaign owner has limited access (rewards, withdrawal)
- Only valid rewards can be added
- Only before launch can rewards be modified
Pledge Protection
- Cannot pledge after deadline
- Cannot pledge less than reward minimum
- Total amount (pledge + shipping) recorded separately
Refund Safety
- Can only claim refund once
- Token must be owned by claimer
- Refunds only available if goal not met
Integration Notes
With CampaignInfo
With GlobalParams
Event Monitoring
Best Practices
Reward Design
- Clear, achievable reward tiers
- Reasonable minimum pledge amounts
- Include physical items, digital goods, or experiences
- Consider shipping costs in reward pricing
Campaign Management
- Set realistic funding goals
- Add rewards before launch
- Cannot modify rewards after launch
- Monitor campaign progress
User Experience
- Display all rewards clearly
- Show funding progress
- Make refund process easy
- Provide clear deadlines
Next Steps
- BaseTreasury - Treasury base contract
- TreasuryFactory - Treasury deployment
- CampaignInfo - Campaign contract reference