A Clarity smart contract implementing a decentralized yield farming protocol that allows users to deposit token pairs and earn rewards through staking mechanisms.
The Yield Farming Protocol enables users to deposit two different tokens into farming vaults and receive yield rewards based on their staking duration and share allocation. The protocol implements a time-locked staking mechanism with administrative controls for managing yield rates and protocol status.
- Dual Token Staking: Deposit pairs of tokens (primary and secondary) to earn yield
- Share-Based Rewards: Proportional reward distribution based on liquidity share allocation
- Time-Lock Security: 24-hour lock period prevents immediate withdrawals
- Administrative Controls: Protocol admin can adjust yield rates and manage protocol status
- Custom Minimum Function: Implements efficient minimum value calculation
- Fee Accumulation: Built-in fee collection mechanism (0.3% protocol fee)
- Yield Positions Map: Tracks individual farmer stakes and positions
- Farming Vaults Map: Manages token reserves and share distributions
- Fungible Token Trait: Interface for token contract interactions
- Administrative Functions: Owner-only protocol management
MIN_DEPOSIT_AMOUNT: 100,000 units minimum depositLOCKUP_DURATION: 144 blocks (~24 hours)BASE_YIELD_RATE: 100 (1.00x base multiplier)PROTOCOL_FEE_RATE: 30 (0.3% fee)
(deposit-tokens primary-token secondary-token primary-amount secondary-amount)- Deposits token pairs into the farming vault
- Calculates and allocates proportional shares
- Initiates lock period for the position
- Requirements: Minimum deposit amounts, no existing position
(withdraw-tokens primary-token secondary-token)- Withdraws deposited tokens plus earned yield rewards
- Requires lock period to have expired
- Automatically calculates and includes yield rewards
- Requirements: Active position, lock period expired
(get-farmer-position farmer-principal)Returns detailed information about a farmer's position including amounts, shares, and timing.
(get-vault-details vault-id)Returns vault information including total reserves, outstanding shares, and accumulated fees.
(compute-share-allocation primary-deposit secondary-deposit)Calculates the number of shares that would be allocated for given deposit amounts.
(transfer-admin-rights new-admin)Transfers protocol administration rights to a new address (admin-only).
(adjust-yield-rate new-rate)Updates the protocol yield rate multiplier (admin-only).
(toggle-protocol-status)Enables/disables the protocol (admin-only).
| Code | Error | Description |
|---|---|---|
| u1 | ERR-UNAUTHORIZED-ACCESS | Caller not authorized for admin functions |
| u2 | ERR-INSUFFICIENT-RESERVES | Not enough reserves for operation |
| u3 | ERR-EXISTING-POSITION | User already has an active position |
| u4 | ERR-NO-ACTIVE-POSITION | User has no active farming position |
| u5 | ERR-BELOW-MINIMUM-THRESHOLD | Deposit below minimum required amount |
| u6 | ERR-STILL-LOCKED | Position still within lock period |
| u7 | ERR-INVALID-TOKEN-PAIR | Invalid or mismatched token contracts |
| u8 | ERR-COMPUTATION-FAILED | Mathematical calculation error |
| u9 | ERR-INVALID-ADDRESS | Invalid principal address |
| u10 | ERR-OWNERSHIP-VALIDATION-FAILED | Admin validation requirements not met |
- Time-Lock Protection: 24-hour mandatory lock period prevents flash loan attacks
- Minimum Deposit Requirements: Prevents dust attacks and ensures meaningful participation
- Admin Validation: New admin must have active, unlocked position
- Token Contract Validation: Ensures only approved token contracts are used
- Overflow Protection: Safe arithmetic operations with proper error handling
;; Deposit tokens to start farming
(contract-call? .yield-farming-protocol deposit-tokens
.primary-token
.secondary-token
u1000000 ;; 1M primary tokens
u500000) ;; 500K secondary tokens
;; After lock period, withdraw with rewards
(contract-call? .yield-farming-protocol withdraw-tokens
.primary-token
.secondary-token)