Managing staking and rewards
Last updated
Last updated
The FeeManager and NexStaking contracts work in tandem to manage staking, rewards distribution, and token swapping. Below is a simplified explanation of how these two contracts interact and the roles each plays.
Staking and Unstaking (Handled by NexStaking) The NexStaking contract primarily handles the process of users staking and unstaking tokens. It manages user positions, deposits tokens into vaults, and keeps track of each user’s stake. Users can "stake" (deposit) tokens into the contract by following these steps: A. The user calls the stake() function in the NexStaking contract. B. The contract then deposits the staked tokens into an ERC4626 vault (a vault that manages these tokens securely). C. The user’s staking details, including the token type, the amount, and the vault, are recorded in a special tracking system (called the _positions mapping), which keeps track of who owns what.
Unstaking Process Users withdraw their staked tokens, along with any accumulated rewards, following the steps: A. The user calls the unstake() function in NexStaking to withdraw their tokens. B. If the user requests rewards in a different token (for example, they want to receive rewards in USDC instead of ANFI), the SwapHelpers library is used to convert the reward tokens into the desired token type. C. The user's shares are redeemed, and the tokens are returned from the vault.
Reward Distribution (Handled by FeeManager, see image below) The FeeManager contract is responsible for handling and distributing rewards, as well as managing how tokens are swapped between different types. Swapping Reward Tokens: A. The FeeManager contract collects various types of reward tokens. B. It converts all these different reward tokens into WETH (Wrapped Ether) using a function called _swapRewardTokensToWETH(). Fee and Reward Splitting Once the reward tokens are converted into WETH, the rewards are split: A. 50% of the WETH is set aside for the owner of the contract. This portion is swapped into USDC and transferred to the owner's account. Funds are used in a non-profit way to, see "Nex Value flow" B. 50% is reserved for the staking pools. This portion is distributed to the users who have staked tokens in the pools, based on the value of their stakes. Distributing WETH to Pools A. The FeeManager calculates the weight of each pool using the calculateWeightOfPools() function. This function looks at the total value of tokens in each pool and determines how much each pool should receive, relative to the others. B. The WETH is then swapped into the specific tokens that each pool holds (index tokens) and transferred to the vaults where the users’ staked tokens are stored. These vaults are managed by the NexStaking contract, which knows where to send the rewards based on the user's stake.
For a complete actively updated overview, check our figma (including the not shown here APY prediction method): https://www.figma.com/board/y117165nHz9RWwVeAJgRE0/staking-contract-v2?node-id=0-1&t=ROT9U3HDyNroWTkp-1