A DeFi staking contract where users deposit tokens to earn rewards over time.
- Time-based reward calculations
- Reward per token accounting
- Lock duration enforcement
- Compound rewards
- APR calculation
- Admin controls with pause functionality
- Modifier chaining
- Stake: Users deposit tokens into the pool
- Accrue: Rewards accumulate based on stake amount and time
- Claim: Users claim earned rewards at any time
- Unstake: After lock period, users can withdraw principal
Rewards are distributed per second based on:
rewardRate: Tokens distributed per secondstakedBalance: User's share of the pooltotalStaked: Total tokens in the pool
userReward = (stakedBalance / totalStaked) * rewardRate * time
stake(amount)- Deposit tokens (minimum required)unstake(amount)- Withdraw after lock periodclaimRewards()- Claim accumulated rewardscompoundRewards()- Add rewards to stake
earned(account)- Calculate pending rewardsrewardPerToken()- Current reward rate per tokentimeUntilUnlock(account)- Seconds until unstake allowedgetAPR()- Current annual percentage rategetPoolStats()- Pool configurationgetUserInfo(user)- User's staking details
setRewardRate(rate)- Adjust reward distributionsetMinimumStake(amount)- Set minimum stakesetLockDuration(duration)- Set lock periodsetPaused(paused)- Pause/unpause pool
// Deploy pool
constructor(stakingToken, rewardToken, 100) // 100 tokens/second
// User stakes 1000 tokens
stake(1000)
// After 1 day, check rewards
earned(userAddress) // Returns accumulated rewards
// Claim rewards
claimRewards()
// After lock period, unstake
unstake(1000)solscript check staking.sol
solscript build staking.sol- Ensure reward token supply covers emissions
- Set appropriate lock durations
- Monitor total staked vs rewards balance
- Consider emergency withdrawal mechanism