SNT Staking contracts
Go to file
Ricardo Guilherme Schmidt b9a83c9d61
Refactor(StakeManager) remove unused Account struct
2024-09-29 17:49:07 -03:00
.github chore: update pull request template and README.md 2024-09-25 11:29:22 +02:00
.vscode refactor: migrate repository to foundry-template (#6) 2023-09-12 18:37:30 +02:00
certora chore(StakeManager.spec): fix wrong Sload requirement 2024-09-28 23:24:45 -03:00
contracts Refactor(StakeManager) remove unused Account struct 2024-09-29 17:49:07 -03:00
lib refactor: migrate repository to foundry-template (#6) 2023-09-12 18:37:30 +02:00
script feat(script): add deployment script for new `StakeManager`s 2024-02-29 11:19:18 -03:00
test refactor(StakeManager): optimize epoch finalization and execution of accounts and epochs 2024-09-26 15:41:35 -03:00
.editorconfig refactor: migrate repository to foundry-template (#6) 2023-09-12 18:37:30 +02:00
.env.example refactor: migrate repository to foundry-template (#6) 2023-09-12 18:37:30 +02:00
.gas-report refactor(StakeManager): optimize account storage by splitting commonly changing values of rarely changing values 2024-09-29 17:46:32 -03:00
.gas-snapshot refactor(StakeManager): optimize account storage by splitting commonly changing values of rarely changing values 2024-09-29 17:46:32 -03:00
.gitattributes refactor: migrate repository to foundry-template (#6) 2023-09-12 18:37:30 +02:00
.gitignore chore(ci): add certora CI integration (#40) 2023-12-11 15:10:41 +01:00
.gitmodules refactor: migrate repository to foundry-template (#6) 2023-09-12 18:37:30 +02:00
.prettier.yml refactor: migrate repository to foundry-template (#6) 2023-09-12 18:37:30 +02:00
.prettierignore refactor: migrate repository to foundry-template (#6) 2023-09-12 18:37:30 +02:00
.solhint.json refactor: migrate repository to foundry-template (#6) 2023-09-12 18:37:30 +02:00
LICENSE Initial commit 2023-05-05 11:12:51 -03:00
PROPERTIES.md refactor: migrate repository to foundry-template (#6) 2023-09-12 18:37:30 +02:00
README.md chore: improve comments and documentation in README.md 2024-09-25 07:00:02 -03:00
codecov.yml refactor: migrate repository to foundry-template (#6) 2023-09-12 18:37:30 +02:00
foundry.toml chore(foundry.toml): update fuzzing settings 2024-09-23 06:10:17 -03:00
package.json chore: update project description and author information, update README.md 2024-09-25 11:29:22 +02:00
pnpm-lock.yaml chore: add adorno command to package.json 2024-09-10 13:05:06 +02:00
remappings.txt fix(remappings): add trailing slash to OZ remapping (#26) 2023-10-10 13:52:10 +02:00
slither.config.json refactor: migrate repository to foundry-template (#6) 2023-09-12 18:37:30 +02:00

README.md

@logos-co/stakingGithub Actions Codecov Foundry

Overview

This project is a smart contract system built for secure token staking and reward management on the Ethereum blockchain. It provides essential components for handling staking operations, managing rewards, and ensuring the security of staked assets. The system is designed to be robust, leveraging mathematical precision for reward calculations and secure vaults for asset protection.

Key Components

  • StakeManager: This contract handles the core staking operations. It is responsible for managing the staking process, calculating rewards, and tracking expired staking periods using advanced mathematical calculations. The contract utilizes OpenZeppelins ERC20 and math utilities to ensure accuracy and security.
  • StakeVault: This contract is responsible for securing user stakes. It ensures the security of user deposits by allowing only the owner to control the vault. The vault communicates with the StakeManager to handle staking and unstaking operations, while ensuring both the safety of funds and the validity of the vaults code.

Features

  • Secure Staking: Users can securely stake tokens, with all interactions managed by the StakeVault and governed by the StakeManager. The StakeVault is owned by the user, and only the owner is permitted to perform actions on it. It stores the staked tokens and StakeManager verifies the StakeVault codehash to ensure that the code managing the tokens is valid and secure.
  • Multiplier Points: The Annual Percentage Yield (APY) is achieved through an internal balance of Multiplier Points, which grow over time based on the amount of staked tokens. Rewards are proportional to the user's balance of Multiplier Points and stake.
  • Stake Locking: Users can lock their stake to start with an increased balance of Multiplier Points, thereby enhancing their potential rewards.
  • Expiring Stake: The accumulation of Multiplier Points continues up to a predefined limit set by the contract. After reaching this limit, users can still earn rewards, but their Multiplier Points will no longer increase.
  • Epoch-Based Rewards: Rewards are tied to the specific epoch in which they were deposited. Users who don't claim their rewards immediately are not at a disadvantage, as their rewards are securely stored and can be claimed at any time, regardless of how much time has passed or the claims of other users.
  • Balance Estimation: The system provides precise estimates of a user's Multiplier Point balance by managing expiring stakes and calculating Multiplier Points per epoch. This allows external systems to efficiently operate on top of these values without needing to process individual accounts or epochs.
  • Integration with ERC20 Tokens: Built on top of the OpenZeppelin ERC20 token standard, the system ensures compatibility with any ERC20-compliant token.
  • Opt-in Migration: Updated versions of the StakeManager can be deployed at any time, but users are not required to migrate. They have the option to opt-out of the migration, even if they have locked funds, which would release their stake immediately.

Installation

To install the dependencies for this project, run the following command:

pnpm install

Usage

Contract Setup

To install the system on the blockchain, follow these steps:

  1. Deploy StakeManager: Begin by deploying the StakeManager contract.
  2. Deploy Sample VaultManager: Next, deploy a sample VaultManager contract on any chain (this can be on a development network or a testnet).
  3. Configure Codehash: Once the VaultManager is deployed, retrieve its codehash and configure it in the StakeManager using the setVault(bytes32) function.
  4. Optional - Use VaultFactory: The VaultFactory contract can be optionally used to optimize gas costs during deployments and to facilitate the creation of valid vaults.

Staking Tokens

To stake tokens, the StakeVault contract interacts with the StakeManager. You can call the function StakeVault.stake(uint256 _amount, uint256 _secondsToLock) with the desired amount and lock duration. Before staking, it is required to call approve on the StakeToken to authorize the StakeVault address to manage your tokens.

The minimum _secondsToLock is defined by the contract settings, and the minimum _amount required to stake is set to ensure it generates Multiplier Points in every epoch.

Tokens are never deposited directly or transferred to the StakeManager. Additionally, tokens should not be sent directly to the StakeVault address. Always use the approve method followed by the stake function to properly stake tokens.

Note that staking will automatically process epochs. Refer to the Manually Processing Epochs section for more information.tion.

Claiming Rewards

Rewards are claimed directly in the StakeManager contract. To claim them, call StakeManager.executeAccount(address _vault) and pass the address of the StakeVault. If many epochs have passed without executeAccount being called, the function may become expensive. In such cases, use StakeManager.executeAccount(address _vault, uint256 _limitEpoch) with a limit on the epoch to process rewards in smaller transactions, allowing continuation in later transactions.

Both functions process rewards similarly, but the latter allows incremental processing. Rewards are sent exclusively to the owner of the StakeVault.

Any address can execute the claim of rewards on behalf of the vault owner.

Note that claiming rewards will automatically process epochs. Refer to the Manually Processing Epochs section for more information.

Unstaking

To unstake tokens, users call StakeVault.unstake(uint256 amount). Unstaking reduces the user's balance on the StakeManager in proportion to the staked amount and time spent staking. Users can only unstake if their balance is not locked.

Note that unstaking will automatically process epochs. Refer to the Manually Processing Epochs section for more information.

Start Migration

The owner of the StakeManager can initiate migration at any time. To do this, deploy a new StakeManager, passing the address of the old StakeManager in the constructor. Then, call the active StakeManager.startMigration(newStakeManager). Users with locked balances can either withdraw their funds or continue under the new contract specifications.

Rewards reserved for past epochs will remain in the old contract, and users can claim them before opting in or out of the migration. Only rewards that have not yet been reserved to any epoch will be transferred to the new StakeManager.

Note that starting migration will automatically process epochs. Refer to the Manually Processing Epochs section for more information.

Opt-In or Opt-Out Migration

Users can accept or reject the new contract by calling StakeVault.acceptMigration() or StakeVault.leave(). If users have pending rewards in the old contract, those rewards will be claimed before opting in or out. Users with locked balances will have the option to leave, even if their balance is still locked.

Note that opting in or out of migration will automatically claim rewards. Refer to the Claiming Rewards section for more information.

Manually Processing Epochs

Epoch processing is required for any contract operation. If the contract becomes obsolete and users stop interacting with it, epoch processing may become expensive to perform in a single transaction. Although processing epochs is cheaper than processing accounts, this has been considered.

The functions StakeManager.executeEpoch() and StakeManager.executeEpoch(uint256 _limitEpoch) can be called to manually process epochs before calling executeAccount.

Note that StakeManager.executeAccount(address _vault, uint256 _limitEpoch) will internally call StakeManager.executeEpoch(uint256 _limitEpoch) with the same _limitEpoch. You can check the latest epoch that can be processed by calling the view function StakeManager.newEpoch(). Functions without the _limitEpoch parameter internally use StakeManager.newEpoch() as the limit, so calls like stakeManager.executeAccount(userVault) and stakeManager.executeEpoch() are equivalent to stakeManager.executeAccount(userVault, stakeManager.newEpoch()) and stakeManager.executeEpoch(stakeManager.newEpoch()).

Development

This is a list of the most frequently needed commands.

Build

Build the contracts:

$ forge build

Clean

Delete the build artifacts and cache directories:

$ forge clean

Compile

Compile the contracts:

$ forge build

Coverage

Get a test coverage report:

$ forge coverage

Deploy

Deploy to Anvil:

$ forge script script/Deploy.s.sol --broadcast --fork-url http://localhost:8545

For this script to work, you need to have a MNEMONIC environment variable set to a valid BIP39 mnemonic.

For instructions on how to deploy to a testnet or mainnet, check out the Solidity Scripting tutorial.

Format

Format the contracts:

$ forge fmt

Gas Usage

Get a gas report:

pnpm gas-report

Get a gas snapshot:

forge snapshot

Lint

Lint the contracts:

$ pnpm lint

Test

Run the tests:

$ forge test

Prepare to commit

Formats, generates snapshot and gas report:

pnpm adorno

License

This project is licensed under MIT.