Skip to content
This repository has been archived by the owner on Jul 20, 2021. It is now read-only.

Commit

Permalink
added vault snapshot
Browse files Browse the repository at this point in the history
  • Loading branch information
senamakel committed Mar 7, 2021
1 parent 43a8308 commit 37216df
Show file tree
Hide file tree
Showing 4 changed files with 125 additions and 39 deletions.
2 changes: 2 additions & 0 deletions contracts/boardoom/core/Vault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ contract Vault is AccessControl, StakingTimelock, Operator {

uint256 internal _totalSupply;
bool public enableDeposits = true;
// bool public updateSnapshot = true;

uint256 internal _totalBondedSupply;

mapping(address => uint256) internal _balances;
Expand Down
78 changes: 39 additions & 39 deletions contracts/boardoom/core/VaultBoardroom.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pragma solidity ^0.8.0;

import {IERC20} from '@openzeppelin/contracts/contracts/token/ERC20/IERC20.sol';
import {Vault} from './Vault.sol';
import {IVault} from '../../interfaces/IVault.sol';
import {SafeMath} from '@openzeppelin/contracts/contracts/math/SafeMath.sol';
import {Safe112} from '../../lib/Safe112.sol';
import {ContractGuard} from '../../utils/ContractGuard.sol';
Expand All @@ -25,7 +25,7 @@ contract VaultBoardroom is ContractGuard, Operator, IBoardroom {
bool public everyoneNewDirector = true;

// The vault which has state of the stakes.
Vault public vault;
IVault public vault;
IERC20 public token;
uint256 public currentEpoch = 1;

Expand Down Expand Up @@ -57,7 +57,7 @@ contract VaultBoardroom is ContractGuard, Operator, IBoardroom {
event RewardPaid(address indexed user, uint256 reward);
event RewardAdded(address indexed user, uint256 reward);

constructor(IERC20 token_, Vault vault_) {
constructor(IERC20 token_, IVault vault_) {
token = token_;
vault = vault_;

Expand Down Expand Up @@ -149,7 +149,7 @@ contract VaultBoardroom is ContractGuard, Operator, IBoardroom {
return boardHistory[directors[director].lastSnapshotIndex];
}

function claimAndReinvestReward(Vault _vault) external virtual {
function claimAndReinvestReward(IVault _vault) external virtual {
uint256 reward = _claimReward(msg.sender);
// NOTE: amount has to be approved from the frontend.
_vault.bondFor(msg.sender, reward);
Expand All @@ -163,41 +163,41 @@ contract VaultBoardroom is ContractGuard, Operator, IBoardroom {
uint256 latestRPS = getLatestSnapshot().rewardPerShare;
uint256 storedRPS = getLastSnapshotOf(director).rewardPerShare;

// If this is 0, that means we are claiming for the first time.
// That could mean a couple of things:
// - 1. We had bonded before this boardroom was live and are claiming in this boardroom for the firstime.
// - 2. We have bonded after this boardroom was live and are claiming in this boardroom for the firsttime.
// - 3. We had bonded before this boardroom was live and are claiming for the first time ever.
if (storedRPS == 0) {
// Get the lastSnapshot user has done any activity from
// the previous boardrooms(one was vested, other wasn't).
IVestedVaultBoardroom.Boardseat memory vestedBoardseat =
vestedVaultBoardroom.directors(director);
Boardseat memory prevSeat = prevVaultBoardroom.directors(director);

// If the snapshot index is 0, that means we haven't done any activity
// in these boardrooms.
// NOTE: this won't detect the case wherein user has bonded before 1st epoch
// and not done anything after that as the lastSnapshotIndex would be 0 for
// this case.
if (vestedBoardseat.lastSnapshotIndex != 0) storedRPS = 0;
else if (prevSeat.lastSnapshotIndex != 0) storedRPS = 0;
else {
// If we have done any activity in the vault before the first epoch
// then we claim rewards from all the epoch.
// NOTE: ideally the activity should be bonding only.
if (directors[director].isFirstVaultActivityBeforeFirstEpoch) {
storedRPS = 0;
} else {
uint256 firstActivityEpoch =
directors[director].firstEpochWhenDoingVaultActivity;

// Get the epoch at which this activity was done.
// claim rewards till that epoch only.
storedRPS = boardHistory[firstActivityEpoch].rewardPerShare;
}
}
}
// // If this is 0, that means we are claiming for the first time.
// // That could mean a couple of things:
// // - 1. We had bonded before this boardroom was live and are claiming in this boardroom for the firstime.
// // - 2. We have bonded after this boardroom was live and are claiming in this boardroom for the firsttime.
// // - 3. We had bonded before this boardroom was live and are claiming for the first time ever.
// if (storedRPS == 0) {
// // Get the lastSnapshot user has done any activity from
// // the previous boardrooms(one was vested, other wasn't).
// IVestedVaultBoardroom.Boardseat memory vestedBoardseat =
// vestedVaultBoardroom.directors(director);
// Boardseat memory prevSeat = prevVaultBoardroom.directors(director);

// // If the snapshot index is 0, that means we haven't done any activity
// // in these boardrooms.
// // NOTE: this won't detect the case wherein user has bonded before 1st epoch
// // and not done anything after that as the lastSnapshotIndex would be 0 for
// // this case.
// if (vestedBoardseat.lastSnapshotIndex != 0) storedRPS = 0;
// else if (prevSeat.lastSnapshotIndex != 0) storedRPS = 0;
// else {
// // If we have done any activity in the vault before the first epoch
// // then we claim rewards from all the epoch.
// // NOTE: ideally the activity should be bonding only.
// if (directors[director].isFirstVaultActivityBeforeFirstEpoch) {
// storedRPS = 0;
// } else {
// uint256 firstActivityEpoch =
// directors[director].firstEpochWhenDoingVaultActivity;

// // Get the epoch at which this activity was done.
// // claim rewards till that epoch only.
// storedRPS = boardHistory[firstActivityEpoch].rewardPerShare;
// }
// }
// }

return
getLastEpochBalance(director)
Expand Down
63 changes: 63 additions & 0 deletions contracts/boardoom/core/VaultSnapshot.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import {IERC20} from '@openzeppelin/contracts/contracts/token/ERC20/IERC20.sol';
import {
AccessControl
} from '@openzeppelin/contracts/contracts/access/AccessControl.sol';
import {Operator} from '../../owner/Operator.sol';
import {SafeMath} from '@openzeppelin/contracts/contracts/math/SafeMath.sol';
import {StakingTimelock} from '../../timelock/StakingTimelock.sol';
import {IVaultBoardroom} from '../../interfaces/IVaultBoardroom.sol';
import {IVault} from '../../interfaces/IVault.sol';

contract VaultSnapshot is Operator, IVault {
using SafeMath for uint256;

uint256 internal _totalSupply;

mapping(address => uint256) internal _balances;

function totalSupply() external view override returns (uint256) {
return _totalSupply;
}

function balanceOf(address who) external view override returns (uint256) {
return _balances[who];
}

function totalBondedSupply() external view override returns (uint256) {
return _totalSupply;
}

function balanceWithoutBonded(address who)
external
view
override
returns (uint256)
{
return _balances[who];
}

function setBalances(address[] memory who, uint256[] memory amt)
public
onlyOwner
{
for (uint256 i = 0; i < who.length; i++) {
_balances[who[i]] = amt[i];
}
}

function setTotalSupply(uint256 amt) public onlyOwner {
_totalSupply = amt;
}

function bond(uint256 amount) external virtual override {}

function bondFor(address who, uint256 amount) external virtual override {}

function unbond(uint256 amount) external virtual override {}

function withdraw() external virtual override {}
}
21 changes: 21 additions & 0 deletions contracts/interfaces/IVault.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

interface IVault {
function totalSupply() external view returns (uint256);

function balanceOf(address who) external view returns (uint256);

function totalBondedSupply() external view returns (uint256);

function balanceWithoutBonded(address who) external view returns (uint256);

function bond(uint256 amount) external;

function bondFor(address who, uint256 amount) external;

function unbond(uint256 amount) external;

function withdraw() external;
}

0 comments on commit 37216df

Please sign in to comment.