How Staking Works
This page explains the technical details of how the staking system operates.Architecture Overview
Reward Mechanism
The staking contract uses the Synthetix StakingRewards pattern, which is battle-tested and gas-efficient.Reward Per Token
Instead of tracking rewards for each user individually, the contract tracks a globalrewardPerToken value that accumulates over time:
User Earnings
When a user wants to know their earned rewards:- Staking/unstaking is O(1) gas cost regardless of number of stakers
- Rewards are calculated on-demand, not stored per-user
Lock Period Implementation
The 3-day lock period is implemented per-stake:Reward Distribution Cycle
- Treasury accumulates fees from vault performance
- Treasury redeems vault tokens to USDC via EasySwapper
- Treasury calls
notifyRewardAmount(usdcAmount)on the staking contract - Contract calculates new reward rate:
rewardRate = amount / rewardsDuration - Stakers earn rewards proportionally over the 7-day period
Extending Reward Periods
If new rewards are added before the current period ends:APY Calculation
The displayed APY is calculated as:- Total USDC rewards distributed
- Total TST staked
- Token prices
Smart Contract Functions
User Functions
| Function | Description |
|---|---|
stake(amount) | Stake TST tokens |
withdraw(amount) | Withdraw staked tokens (after lock) |
getReward() | Claim earned USDC rewards |
exit() | Withdraw all + claim rewards |
View Functions
| Function | Description |
|---|---|
balanceOf(address) | User’s staked balance |
earned(address) | User’s claimable rewards |
lockEnd(address) | Timestamp when lock expires |
canWithdraw(address) | Whether user can withdraw |
totalSupply() | Total staked TST |
rewardRate() | Current reward rate per second |
Admin Functions
| Function | Description |
|---|---|
notifyRewardAmount(reward) | Add new rewards (REWARDS_DISTRIBUTOR_ROLE) |
setRewardsDuration(duration) | Change reward period length |
setLockDuration(duration) | Change lock period length |
pause() / unpause() | Emergency controls |
Security Features
- UUPS Upgradeable: Contract can be upgraded if needed
- Access Control: Role-based permissions for admin functions
- Pausable: Emergency pause capability
- Reentrancy Guard: Protection against reentrancy attacks
- Lock Period: Prevents flash loan attacks