Technical Implementation
How to implement staking for your protocol, from quick start guides to in-depth technical details.
Last updated
Was this helpful?
How to implement staking for your protocol, from quick start guides to in-depth technical details.
Last updated
Was this helpful?
To implement Tally's staking system, you'll need the following:
Compatible tokens:
A standard ERC20 token to use as the staking token. Typically, the staking token is the native token of the protocol, like UNI.
One or more ERC20 tokens to distribute as rewards. e.g. WETH or the native token of the protocol for inflationary rewards.
Note: Rebasing tokens or non-standard ERC20 tokens won't work.
Reward source:
Rewards come from protocol revenue, treasury funds, or minting new tokens.
Deploy with Tally's assistance
For the fastest and most reliable implementation:
Contact Tally's team at
Tell Tally about your staking and reward tokens
Tally will guide you through the deployment process
Get a fully-configured staking user interface on Tally
Here's an architecture diagram of the staking smart contracts:
Staking is out-of-the-box compatible with existing `ERC20Votes` governance tokens. It supports `ERC20Votes` delegation with the "surrogate factory" pattern. Staking creates a surrogate contract for each delegate. It delegates voting power in each surrogate to the delegate.
When Staker receives rewards, it distributes them over a period of time, e.g. 30 days. Distributing over time gives unstaked tokenholders a chance to stake. A smooth schedule also minimizes discontinuities from flash staking.
Any instance of Staker needs these pieces to work.
Staker Contract: The main contract that handles staking, reward distribution, and voting power delegation
Earning Power Calculator: Determines how rewards are distributed to stakers
Delegation Surrogate: Manages governance voting power for staked tokens
Reward Notifier(s): Connect reward sources to the staking system
Instances of Staker can add these extensions for extra features.
StakerPermitAndStake: Adds EIP-2612 permit functionality for better UX
StakerOnBehalf: Enables signature-based execution of staking actions
StakerCapDeposits: Enforces a cap on the total stake amount
The LST, also called stGOV, is the easiest way to get rewards from staking.
The LST is, of course, liquid. Staking positions can be transferred without unstaking.
The LST auto-compounds rewards. Holders will automatically accrue the rewards without having to call claim()
The LST keeps governance power active. When the LST is in DeFi, cold storage, or a centralized exchange, the LST provides a backup plan for governance power.
The Staker
contract is the core of the system. It manages:
Staking deposits and withdrawals
Reward distribution over time
Delegation of voting power
Earning power calculation
Staker uses a streaming reward mechanism, where rewards are added as lump sums, then distributed evenly over time. This gives stakers time to respond to changes in reward rates.
Staker uses a concept called "Earning Power" to distribute rewards. Every depositor gets Earning Power. Their share of the reward is their earning power divided by the total earning power in Staker, over time. The earning power calculator determines which depositors are eligible for rewards and how much they earn
Flat Earning Power
Oracle-based Earning Power
An oracle puts scores onchain
The calculator turns earning power on and off based on whether an address's score exceeds a configurable threshold
Staker uses the earning power over time to distribute rewards.
. Tally provides two implementations:
Reward notifiers are responsible for informing the staking contract about new rewards:
Staker uses the “surrogate pattern” to make staking compatible with governance tokens. That way, tokenholders don’t have to choose between earning rewards and doing governance.
Staker creates a surrogate contract for each delegate (address receiving voting power)
Surrogate deposits and withdrawals are fully controlled by the staking system. Staker does all the accounting.
The surrogate contract holds staked tokens and delegates all its voting power to the chosen delegatee. That way, staking is compatible with the underlying governance token.
Note that these surrogate contracts allow tokenholders to split up their voting power. i.e. partial delegation
Create custom earning power calculators to incentivize specific behaviors:
Activity-based rewards: Require governance participation to earn full rewards.
Time-weighted staking: Increase rewards for long-term stakers
Protocol usage rewards: Tie rewards to protocol usage
Admin controls:
The admin of the staking contracts can’t touch staked assets. They do control some system parameters:
Add and remove reward sources, by enabling and disabling reward notifiers
Set the eligibility criteria for rewards, by changing the earning power calculator
Change the emergency pause guardian.
Override eligibility for a particular address.
Set claim fee parameters
Upgrade strategy:
These contracts could be deployed immutable or with the upgradeable proxy pattern
To migrate an immutable Staker:
Deploy a new staking contract
Send rewards there
Have tokenholders migrate to the new one
To upgrade-in-place a proxy contract:
Use initializers instead of constructors for key params at deployment
Check the storage slots carefully to avoid corrupting state
Emergency measures:
The oracle-based calculator has failsafes in case the oracle misbehaves or goes offline:
If the oracle misbehaves by posting incorrect scores, a `PauseGuardian` can pause the system, reverting it to flat earning power.
If the oracle goes offline, the calculator also automatically reverts to using flat earning power.
The oracle can be replaced by Staker's admin.
Deposit: ~100,000-150,000 gas
Claiming rewards: ~60,000-100,000 gas
Withdrawal: ~80,000-120,000 gas
For optimal performance:
Distribute rewards at reasonable intervals, e.g., weekly or monthly
The earning power calculator should wait between updates, e.g. daily
The earning power calculator shouldn’t make lots of small updates, especially networks with high gas costs
Anyone can update earning power as it changes, but someone needs to do it. Staker provides “tips” as incentive for bots to do updates. If MEV bots do not know about the incentive, consider running the staker bots script directly.
These incentives don’t work testnets or for tokens with no market value.
See the
The staking contracts have modules for , hooking up , and . Protocol teams can assemble a staking system from these audited pieces.
Staker is built on. Unistaker is based on Synthetix's.
UniStaker and Staker have been audited several times. The audit reports are.
The backup is a configurable strategy for keeping voting power active in governance. For example, see the , which only votes on proposals that have lots of consensus.
Access the open-source .
: Simple 1:1 mapping where earning power equals staked amount.
: More advanced calculator where earning power depends on the delegate's activity score. Here’s how it works:
holds rewards directly and distributes them by calling transfer()
relies on an approve()
, so that it can call transferFrom()
on the reward source
calls mint()
on a token contract.