-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #129 from defactor-com/docs/staking
docs(staking): create introduction page #128
- Loading branch information
Showing
4 changed files
with
135 additions
and
42 deletions.
There are no files selected for viewing
97 changes: 97 additions & 0 deletions
97
docs/engage/smart-contracts/staking-contract/introduction.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
--- | ||
id: staking-smart-contract | ||
title: Introduction | ||
sidebar_position: 1 | ||
tags: | ||
- Staking | ||
--- | ||
|
||
`Staking.sol` this contract allows users to create, manage, and interact with stakes to pools using `ERC20` tokens standard. | ||
|
||
## Contract versions | ||
|
||
This contract has one audited version, but there are some development in progress to use the same base contract but with a variable reward rate. | ||
|
||
For this first iteration, the contract will only have a fixed reward rate and in future versions, the variable reward rate will be integrated. | ||
|
||
## Staking Smart Contract Documentation | ||
|
||
### Overview | ||
|
||
The Staking contract allows users to stake ERC20 tokens and earn rewards based on predefined plans or new plans created by the admin. | ||
|
||
### Dependencies | ||
|
||
- `SafeERC20Upgradeable`: Safe operations for ERC20 tokens. | ||
- `AccessControlUpgradeable`: Role-based access control. | ||
- `PausableUpgradeable`: Emergency stop mechanism. | ||
|
||
### Constants | ||
|
||
- `PERCENTAGE_MULTIPLIER`: Multiplier for percentage calculations (100). | ||
- `MIN_STAKE_AMOUNT`: Minimum amount required to stake (1000 ether). | ||
|
||
### Roles | ||
|
||
Staking contract has two main roles: | ||
|
||
#### Admin | ||
|
||
Responsible for managing the contract. The admin can add new plans, pause and unpause the contract for any security reason, withdraw funds from the contract and set staking end dates and rewards. | ||
|
||
#### Client | ||
|
||
The client can stake tokens, un-stake tokens, claim rewards, re-stake and withdraw the earnings. | ||
|
||
### Plan | ||
|
||
The contract supports multiple staking plans. Each plan has a unique id, lock duration, and apy. Every stakes must be associated with a plan. Interactions with the plans are limited by the global values of `stakingEndTime` and `rewardsEndTime`, for example, no stakes are allowed if `stakingEndTime` is in the past. | ||
|
||
When the contract is instantiated, by default, three plans are created with 0, 90 and 180 lock duration with 5, 10 and 25 apy configuration respectively. | ||
|
||
The values for a plan are: | ||
|
||
- `id`: Unique identifier for the plan. | ||
- `lockDuration`: Duration in seconds for the stake to be locked. | ||
- `apy`: Annual percentage yield for the plan. | ||
- `status`: Status of the plan (active or inactive). The state of the plans can change if `setDates` function is called. | ||
|
||
### Stake | ||
|
||
By default, when a user stakes, its status is `STAKED` (locked). After the lock duration ends, the status changes to `UNLOCKED`. The user can then claim rewards, optionally, the user can un-stake. | ||
|
||
- `planId`: The plan the stake will belong. | ||
- `amount`: The amount of tokens to stake. | ||
- `claimed`: The amount of tokens claimed. | ||
- `status`: The status of the stake (STAKED, UNLOCKED, UNSTAKED). | ||
- `unstaked`: A flag to indicate if the stake was un-staked. | ||
- `stakeTime`: The time the stake was created. | ||
|
||
### Functions | ||
|
||
#### Admin | ||
|
||
- `addPlan`: Add a new plan to the contract. | ||
- `setDates`: Set the staking end time and rewards end time. If dates are set in the past, `stake` and `restake` functions will start reverting with the error `StakingHasEnded`. | ||
- `pause`: Pause the contract. If paused, the contract will revert all transactions for `stake`, `unstake`, `restake` and `claimRewards`. | ||
- `unpause`: Unpause the contract. | ||
- `withdraw`: Withdraw tokens from the contract. | ||
|
||
#### User | ||
|
||
- `stake`: Stake tokens. The user must approve the contract to spend the tokens. | ||
- `unstake`: Un-stake tokens. The user can only un-stake after the lock duration ends and the stake is not already un-staked. | ||
- `restake`: Re-stake tokens. The user can only re-stake after the lock duration ends and the stake is not already un-staked. This action is equivalent to un-stake and stake again. | ||
|
||
### Rewards | ||
|
||
Every stake will earn rewards based on the plan's apy, even if the stake plan period is over it will keep earning rewards using the 5 apy from the default first plan that is created when the contract is instantiated. The rewards are calculated based on the time the stake was created. The rewards are calculated using the following formula: | ||
|
||
```solidity | ||
plan_rewards = (staked_amount * apy * time) / (100 * 365 days) | ||
after_plan_rewards = (staked_amount * 5 * time_after) / (100 * 365 days) // 5 is the default apy | ||
rewards = plan_rewards + after_plan_rewards - claimed | ||
``` | ||
|
||
> Note: Some attributes and status in plan and stake entities are extended by the backend to give a more precise information. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,38 @@ | ||
[ | ||
{ | ||
"id": "engage-dev", | ||
"name": "Engage Development Instance", | ||
"description": "The development environment for Defactor Engage.", | ||
"web": "https://engage-dev.defactor.dev", | ||
"api": "https://engage-dev-api.defactor.dev", | ||
"testnet": true, | ||
"chains": [ | ||
{ | ||
"id": "", | ||
"contract": "" | ||
} | ||
] | ||
}, | ||
{ | ||
"id": "engage-staging", | ||
"name": "Engage Staging Instance", | ||
"description": "The staging environment for Defactor Engage.", | ||
"web": "https://engage.defactor.dev", | ||
"api": "https://engage-api.defactor.dev", | ||
"testnet": true, | ||
"chains": [ | ||
{ | ||
"id": "", | ||
"contract": "" | ||
} | ||
] | ||
} | ||
] | ||
{ | ||
"id": "engage-dev", | ||
"name": "Engage Development Instance", | ||
"description": "The development environment for Defactor Engage.", | ||
"web": "https://engage-dev.defactor.dev", | ||
"api": "https://engage-dev-api.defactor.dev", | ||
"testnet": true, | ||
"chains": [ | ||
{ | ||
"id": "Amoy", | ||
"type": "Staking", | ||
"contract": "0x59d529885f6394E365F79B44bD0bA800B49bF186", | ||
"factr": "0x8574299682D036F88195a2685601D90300E21Bca" | ||
}, | ||
{ | ||
"id": "Sepolia", | ||
"type": "Staking", | ||
"contract": "0xB0eae41071bDE1D4FA116059526aD7ce88F2b462", | ||
"factr": "0x7D5c1468D8bE9f0F4FaD26F2Cb7e6b2ed9042577" | ||
} | ||
] | ||
}, | ||
{ | ||
"id": "engage-staging", | ||
"name": "Engage Staging Instance", | ||
"description": "The staging environment for Defactor Engage.", | ||
"web": "https://engage.defactor.dev", | ||
"api": "https://engage-api.defactor.dev", | ||
"testnet": true, | ||
"chains": [ | ||
{ | ||
"id": "", | ||
"contract": "" | ||
} | ||
] | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters