Skip to content

Commit

Permalink
Implement accumulated rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
Kifen committed Dec 15, 2021
1 parent 39dcaf1 commit 7d3222c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 26 deletions.
21 changes: 12 additions & 9 deletions contracts/StakingGrayblock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand All @@ -69,8 +68,6 @@ contract GrayblockStaking is ReentrancyGuard, Ownable {
factory = _factory;
feeBps = 100;
name = _name;

emit GrayblockStakingContractDeployed();
}

/**
Expand Down Expand Up @@ -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[
Expand All @@ -176,7 +179,7 @@ contract GrayblockStaking is ReentrancyGuard, Ownable {
);
}

emit AllocationUpdated();
emit AllocationUpdated(_amount, accumulatedPoolReward);
}

/**
Expand Down
12 changes: 6 additions & 6 deletions contracts/lib/IterableMapping.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ library IterableMapping {
}

function get(Map storage map, address key)
public
internal
view
returns (StakeInfo memory)
{
return map.values[key];
}

function getIndexOfKey(Map storage map, address key)
public
internal
view
returns (int256)
{
Expand All @@ -34,22 +34,22 @@ 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;
}

function add(
Map storage map,
address key,
StakeInfo memory val
) public {
) internal {
if (map.inserted[key]) {
map.values[key] = val;
} else {
Expand All @@ -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;
}
Expand Down
18 changes: 7 additions & 11 deletions scripts/deploy-stakepool.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 7d3222c

Please sign in to comment.