-
Notifications
You must be signed in to change notification settings - Fork 1
Eth staking mechanism
Eth2.0 staking pool
Period: Validators get rewarded once per epoch (1 epoch = 384 s = 6.5 minutes). In other words, the minimum lock-up period will be 1 epoch. https://www.fingerlakes1.com/2021/06/28/is-etherium-2-0-staking-a-worthwhile-investment-in-2021/ https://launchpad.ethereum.org/en/
Basic contract
invest() called by investor to invest some eth (payable), must be before end of the period. Note: fee is being charged to investor, the fee amount is transfered to the owner of the contract (good idea?)
finalize() called by anyone, mark the investing period as over and compute the staking gains (or new total amount)
getChange() called by investor to get principal + staking gains (must be at the end of the period)
deposit() called by admin/owner of the staking pool, the investment period must not be running, to invest/stake the eth amount in the contract in eth2.0
Personal note: try to have the contract's balance paying for the gas fee associated with the call to the deposit function on the official ethereum staking contract.
Does the ethereum staking pool contract return funds+profit automatically at the end of the period? If so, through what function?
How to make the staked capital roll after the 7 days? Call deposit() again at the end of the period. Not enough: the amounts invested by users must be adjusted to account for/add the gains they earned. => should be done in finalize() Is there an event that eth2.0 staking contract emits that lets staking pools know it's the end of a period? If so, a backend service can listen to this event, when received, it can call finalize.
src: https://www.youtube.com/watch?v=gq3LhxkDwfY
Here's a more elaborate staking pool contract, with bells and whistles. Differences:
- make a distinction between deposit ether vs stake ether (not super needed IMHO)
- allow user to see their staked amount (just a public view function returning the amount staked of the user from a mapping, nothing crazy)
- prevent admin/owner to take out more from the contract than the fees (but maybe it's best to have a contract owning this all together, or make the finalize function send remaining fee for the period to the owner)
- important: able to receive funds (amount invested + staking gains) from the official Ethereum staking pool contract => How?
https://github.com/AntoniosHadji/ethereum-staking-smart-contract
Synthetix staking contract (staking and interest earn on ERC20 token, not Ether): https://github.com/Synthetixio/synthetix/blob/develop/contracts/StakingRewards.sol
In this staking implementation, the author stores the staking users/addresses in an array/list. It's actually interesting in that it makes it easy to get the complete list of staking users/addresses. Note: the staked amounts are stored in a mapping: mapping(address => uint256) internal stakes;
src: https://hackernoon.com/implementing-staking-in-solidity-1687302a82cf