From 7d3222cfc0b117a534d73e9adf9efd60b6a0a687 Mon Sep 17 00:00:00 2001 From: kifen Date: Wed, 15 Dec 2021 02:23:45 +0100 Subject: [PATCH] Implement accumulated rewards --- contracts/StakingGrayblock.sol | 21 ++++++++++++--------- contracts/lib/IterableMapping.sol | 12 ++++++------ scripts/deploy-stakepool.js | 18 +++++++----------- 3 files changed, 25 insertions(+), 26 deletions(-) diff --git a/contracts/StakingGrayblock.sol b/contracts/StakingGrayblock.sol index 17c9f39..b1de0b8 100644 --- a/contracts/StakingGrayblock.sol +++ b/contracts/StakingGrayblock.sol @@ -12,16 +12,13 @@ contract GrayblockStaking is ReentrancyGuard, Ownable { using SafeMath for uint256; using IterableMapping for IterableMapping.Map; - /// @notice Event emitted only on construction. To be used by indexers - event GrayblockStakingContractDeployed(); - event TradedTokenPut(address engeryDeveloper, uint256 amount); event Staked(address staker, uint256 amount); event UnStaked(address staker, uint256 amount); - event AllocationUpdated(); + event AllocationUpdated(uint256 allocationAmount, uint256 accumulated); event ClaimReward(address _staker, uint256 _amount); string public name; @@ -50,6 +47,8 @@ contract GrayblockStaking is ReentrancyGuard, Ownable { /// @notice Total staked project token balance uint256 public totalStakedBalance; + uint256 private accumulatedPoolReward; + /** * @notice Constructor * @param _tradedToken Traded Token Instance @@ -69,8 +68,6 @@ contract GrayblockStaking is ReentrancyGuard, Ownable { factory = _factory; feeBps = 100; name = _name; - - emit GrayblockStakingContractDeployed(); } /** @@ -161,11 +158,17 @@ contract GrayblockStaking is ReentrancyGuard, Ownable { /** * @notice Our Backend calls this function every 6hrs to calcuate the reward for every user */ - function updateAllocation(uint256 rewardAmount) external onlyOwner { + function updateAllocation(uint256 _amount) external onlyOwner { uint256 tradedTokenBalance = tradedToken.balanceOf(address(this)); - require(tradedTokenBalance >= rewardAmount, "not enough reward"); + require(tradedTokenBalance >= _amount, "not enough reward"); + + if (stakeInfos.size() == 0) { + accumulatedPoolReward.add(_amount); + return; + } + uint256 rewardAmount = _amount.add(accumulatedPoolReward); for (uint256 i = 0; i < stakeInfos.size(); i++) { address key = stakeInfos.getKeyAtIndex(i); IterableMapping.StakeInfo storage stakeInfo = stakeInfos.values[ @@ -176,7 +179,7 @@ contract GrayblockStaking is ReentrancyGuard, Ownable { ); } - emit AllocationUpdated(); + emit AllocationUpdated(_amount, accumulatedPoolReward); } /** diff --git a/contracts/lib/IterableMapping.sol b/contracts/lib/IterableMapping.sol index 789fa3e..4b77ac0 100644 --- a/contracts/lib/IterableMapping.sol +++ b/contracts/lib/IterableMapping.sol @@ -15,7 +15,7 @@ library IterableMapping { } function get(Map storage map, address key) - public + internal view returns (StakeInfo memory) { @@ -23,7 +23,7 @@ library IterableMapping { } function getIndexOfKey(Map storage map, address key) - public + internal view returns (int256) { @@ -34,14 +34,14 @@ library IterableMapping { } function getKeyAtIndex(Map storage map, uint256 index) - public + internal view returns (address) { return map.keys[index]; } - function size(Map storage map) public view returns (uint256) { + function size(Map storage map) internal view returns (uint256) { return map.keys.length; } @@ -49,7 +49,7 @@ library IterableMapping { Map storage map, address key, StakeInfo memory val - ) public { + ) internal { if (map.inserted[key]) { map.values[key] = val; } else { @@ -60,7 +60,7 @@ library IterableMapping { } } - function remove(Map storage map, address key) public { + function remove(Map storage map, address key) internal { if (!map.inserted[key]) { return; } diff --git a/scripts/deploy-stakepool.js b/scripts/deploy-stakepool.js index 599dc83..550ffe2 100644 --- a/scripts/deploy-stakepool.js +++ b/scripts/deploy-stakepool.js @@ -14,19 +14,15 @@ async function main() { // manually to make sure everything is compiled // await hre.run('compile'); - const IterableMapping = await hre.ethers.getContractFactory( - "IterableMapping" - ); - const iterableMapping = await IterableMapping.deploy(); - await iterableMapping.deployed(); - console.log("Lib IterableMapping deployed to:", iterableMapping.address); + // const IterableMapping = await hre.ethers.getContractFactory( + // "IterableMapping" + // ); + // const iterableMapping = await IterableMapping.deploy(); + // await iterableMapping.deployed(); + // console.log("Lib IterableMapping deployed to:", iterableMapping.address); console.log("Deploying stake pool contract..."); - const Pools = await hre.ethers.getContractFactory("GrayblockStaking", { - libraries: { - IterableMapping: iterableMapping.address, - }, - }); + const Pools = await hre.ethers.getContractFactory("GrayblockStaking"); const pools = await Pools.deploy( process.env.TRADE_TOKEN,