Staking contract with unstaking feature along with reward distribution for different ERC20 tokens
-
It's a Staking contract for multiple tokens.
-
User gets rewards based on the current staking amount, reward interval, token's reward rate, staked time duration.
-
CONS:
- Currently, if a person stakes x at t = t1, y at t = t1+1, then total staked is taken as (x + y). And this is wrong because the time difference for staking is not taken into consideration. So, ideally a person can stake all at once at the end of year & then gain the rewards based on the entire staked amount (which actually got added on 12th month & the 1st staking happened at 1st month). So, this is absolutely wrong calculation. The solution is to create this data structure:
NOW:
struct Record {
uint256 stakedAmount;
uint256 stakedAt;
uint256 unstakedAmount;
uint256 unstakedAt;
uint256 rewardAmount;
}
//implement your code here for "records", a mapping of token addresses and
// user addresses to an user Record struct
mapping(address => mapping(address => Record)) public records;
UPDATED to:
struct Record {
uint256 stakedAmount;
uint256 unstakedAmount;
uint256 unstakedAt;
uint256 rewardAmount;
}
//implement your code here for "records", a mapping of token addresses and
// user addresses to an user Record struct
// mapping (token => user => stakedAt => Record)
mapping(address => mapping(address => mapping (uint256 => Record))) public records;
$ npm i
$ npx hardhat compile
$ npx hardhat test
// on terminal-1
$ npx hardhat node
// on terminal-2
$ npx hardhat run deployment/hardhat/deploy.ts --network localhost
- Environment variables
- Create a
.env
file with its values:
- Create a
INFURA_API_KEY=[YOUR_INFURA_API_KEY_HERE]
DEPLOYER_PRIVATE_KEY=[YOUR_DEPLOYER_PRIVATE_KEY_without_0x]
REPORT_GAS=<true_or_false>
- Deploy the contracts
$ npx hardhat run deployment/testnet/rinkeby/deploy.ts --network rinkeby
- Environment variables
- Create a
.env
file with its values:
- Create a
INFURA_API_KEY=[YOUR_INFURA_API_KEY_HERE]
DEPLOYER_PRIVATE_KEY=[YOUR_DEPLOYER_PRIVATE_KEY_without_0x]
REPORT_GAS=<true_or_false>
- Deploy the token on one-chain
$ npx hardhat run deployment/mainnet/ETH/deploy.ts --network mainnet