Skip to content
This repository has been archived by the owner on Feb 11, 2022. It is now read-only.

Latest commit

 

History

History
605 lines (500 loc) · 17.1 KB

StakingPoolLibV1.md

File metadata and controls

605 lines (500 loc) · 17.1 KB

StakingPoolLibV1.sol

View Source: contracts/libraries/StakingPoolLibV1.sol

StakingPoolLibV1

Functions

getInfoInternal

Gets the info of a given staking pool by key

function getInfoInternal(IStore s, bytes32 key, address you) external view
returns(name string, addresses address[], values uint256[])

Arguments

Name Type Description
s IStore Specify the store instance
key bytes32 Provide the staking pool key to fetch info for
you address Specify the address to customize the info for
Source Code
function getInfoInternal(
    IStore s,
    bytes32 key,
    address you
  )
    external
    view
    returns (
      string memory name,
      address[] memory addresses,
      uint256[] memory values
    )
  {
    addresses = new address[](4);
    values = new uint256[](15);

    name = s.getStringByKeys(StakingPoolCoreLibV1.NS_POOL, key);

    addresses[0] = s.getStakingTokenAddressInternal(key);
    addresses[1] = s.getStakingTokenStablecoinPairAddressInternal(key);
    addresses[2] = s.getRewardTokenAddressInternal(key);
    addresses[3] = s.getRewardTokenStablecoinPairAddressInternal(key);

    values[0] = s.getTotalStaked(key);
    values[1] = s.getTarget(key);
    values[2] = s.getMaximumStakeInternal(key);
    values[3] = getPoolStakeBalanceInternal(s, key);
    values[4] = getPoolCumulativeDeposits(s, key);
    values[5] = s.getRewardPerBlock(key);
    values[6] = s.getRewardPlatformFee(key);
    values[7] = s.getLockupPeriodInBlocks(key);
    values[8] = s.getRewardTokenBalance(key);
    values[9] = getAccountStakingBalanceInternal(s, key, you);
    values[10] = getTotalBlocksSinceLastRewardInternal(s, key, you);
    values[11] = calculateRewardsInternal(s, key, you);
    values[12] = canWithdrawFromBlockHeightInternal(s, key, you);
    values[13] = getLastDepositHeight(s, key, you);
    values[14] = getLastRewardHeight(s, key, you);
  }

getPoolStakeBalanceInternal

function getPoolStakeBalanceInternal(IStore s, bytes32 key) public view
returns(uint256)

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function getPoolStakeBalanceInternal(IStore s, bytes32 key) public view returns (uint256) {
    uint256 totalStake = s.getUintByKeys(StakingPoolCoreLibV1.NS_POOL_STAKING_TOKEN_BALANCE, key);
    return totalStake;
  }

getPoolCumulativeDeposits

function getPoolCumulativeDeposits(IStore s, bytes32 key) public view
returns(uint256)

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function getPoolCumulativeDeposits(IStore s, bytes32 key) public view returns (uint256) {
    uint256 totalStake = s.getUintByKeys(StakingPoolCoreLibV1.NS_POOL_CUMULATIVE_STAKING_AMOUNT, key);
    return totalStake;
  }

getAccountStakingBalanceInternal

function getAccountStakingBalanceInternal(IStore s, bytes32 key, address account) public view
returns(uint256)

Arguments

Name Type Description
s IStore
key bytes32
account address
Source Code
function getAccountStakingBalanceInternal(
    IStore s,
    bytes32 key,
    address account
  ) public view returns (uint256) {
    return s.getUintByKeys(StakingPoolCoreLibV1.NS_POOL_STAKING_TOKEN_BALANCE, key, account);
  }

getTotalBlocksSinceLastRewardInternal

function getTotalBlocksSinceLastRewardInternal(IStore s, bytes32 key, address account) public view
returns(uint256)

Arguments

Name Type Description
s IStore
key bytes32
account address
Source Code
function getTotalBlocksSinceLastRewardInternal(
    IStore s,
    bytes32 key,
    address account
  ) public view returns (uint256) {
    uint256 from = getLastRewardHeight(s, key, account);

    if (from == 0) {
      return 0;
    }

    return block.number - from;
  }

canWithdrawFromBlockHeightInternal

function canWithdrawFromBlockHeightInternal(IStore s, bytes32 key, address account) public view
returns(uint256)

Arguments

Name Type Description
s IStore
key bytes32
account address
Source Code
function canWithdrawFromBlockHeightInternal(
    IStore s,
    bytes32 key,
    address account
  ) public view returns (uint256) {
    uint256 lastDepositHeight = getLastDepositHeight(s, key, account);
    uint256 lockupPeriod = s.getLockupPeriodInBlocks(key);

    return lastDepositHeight + lockupPeriod;
  }

getLastDepositHeight

function getLastDepositHeight(IStore s, bytes32 key, address account) public view
returns(uint256)

Arguments

Name Type Description
s IStore
key bytes32
account address
Source Code
function getLastDepositHeight(
    IStore s,
    bytes32 key,
    address account
  ) public view returns (uint256) {
    return s.getUintByKeys(StakingPoolCoreLibV1.NS_POOL_DEPOSIT_HEIGHTS, key, account);
  }

getLastRewardHeight

function getLastRewardHeight(IStore s, bytes32 key, address account) public view
returns(uint256)

Arguments

Name Type Description
s IStore
key bytes32
account address
Source Code
function getLastRewardHeight(
    IStore s,
    bytes32 key,
    address account
  ) public view returns (uint256) {
    return s.getUintByKeys(StakingPoolCoreLibV1.NS_POOL_REWARD_HEIGHTS, key, account);
  }

calculateRewardsInternal

function calculateRewardsInternal(IStore s, bytes32 key, address account) public view
returns(uint256)

Arguments

Name Type Description
s IStore
key bytes32
account address
Source Code
function calculateRewardsInternal(
    IStore s,
    bytes32 key,
    address account
  ) public view returns (uint256) {
    uint256 totalBlocks = getTotalBlocksSinceLastRewardInternal(s, key, account);

    if (totalBlocks == 0) {
      return 0;
    }

    uint256 rewardPerBlock = s.getRewardPerBlock(key);
    uint256 myStake = getAccountStakingBalanceInternal(s, key, account);
    return (myStake * rewardPerBlock * totalBlocks) / 1 ether;
  }

withdrawRewardsInternal

function withdrawRewardsInternal(IStore s, bytes32 key, address account) public nonpayable
returns(rewardToken address, rewards uint256, platformFee uint256)

Arguments

Name Type Description
s IStore
key bytes32
account address
Source Code
function withdrawRewardsInternal(
    IStore s,
    bytes32 key,
    address account
  )
    public
    returns (
      address rewardToken,
      uint256 rewards,
      uint256 platformFee
    )
  {
    rewards = calculateRewardsInternal(s, key, account);

    s.setUintByKeys(StakingPoolCoreLibV1.NS_POOL_REWARD_HEIGHTS, key, account, block.number);

    if (rewards == 0) {
      return (address(0), 0, 0);
    }

    rewardToken = s.getAddressByKeys(StakingPoolCoreLibV1.NS_POOL_REWARD_TOKEN, key);

    // Update (decrease) the balance of reward token
    s.subtractUintByKeys(StakingPoolCoreLibV1.NS_POOL_REWARD_TOKEN_BALANCE, key, rewards);

    // Update total rewards given
    s.addUintByKeys(StakingPoolCoreLibV1.NS_POOL_TOTAL_REWARD_GIVEN, key, account, rewards); // To this account
    s.addUintByKeys(StakingPoolCoreLibV1.NS_POOL_TOTAL_REWARD_GIVEN, key, rewards); // To everyone

    platformFee = (rewards * s.getRewardPlatformFee(key)) / ProtoUtilV1.MULTIPLIER;

    IERC20(rewardToken).ensureTransfer(msg.sender, rewards - platformFee);
    IERC20(rewardToken).ensureTransfer(s.getTreasury(), rewards);
  }

depositInternal

function depositInternal(IStore s, bytes32 key, uint256 amount) external nonpayable
returns(stakingToken address)

Arguments

Name Type Description
s IStore
key bytes32
amount uint256
Source Code
function depositInternal(
    IStore s,
    bytes32 key,
    uint256 amount
  ) external returns (address stakingToken) {
    require(key > 0, "Invalid key");
    require(amount > 0, "Enter an amount");
    require(amount <= s.getMaximumStakeInternal(key), "Stake too high");
    require(amount <= s.getAvailableToStakeInternal(key), "Target achieved or cap exceeded");

    stakingToken = s.getStakingTokenAddressInternal(key);

    // First withdraw your rewards
    withdrawRewardsInternal(s, key, msg.sender);

    // Individual state
    s.addUintByKeys(StakingPoolCoreLibV1.NS_POOL_STAKING_TOKEN_BALANCE, key, msg.sender, amount);
    s.setUintByKeys(StakingPoolCoreLibV1.NS_POOL_DEPOSIT_HEIGHTS, key, msg.sender, block.number);

    // Global state
    s.addUintByKeys(StakingPoolCoreLibV1.NS_POOL_STAKING_TOKEN_BALANCE, key, amount);
    s.addUintByKeys(StakingPoolCoreLibV1.NS_POOL_CUMULATIVE_STAKING_AMOUNT, key, amount);

    IERC20(stakingToken).ensureTransferFrom(msg.sender, address(this), amount);
  }

withdrawInternal

function withdrawInternal(IStore s, bytes32 key, uint256 amount) external nonpayable
returns(stakingToken address)

Arguments

Name Type Description
s IStore
key bytes32
amount uint256
Source Code
function withdrawInternal(
    IStore s,
    bytes32 key,
    uint256 amount
  ) external returns (address stakingToken) {
    require(key > 0, "Invalid key");
    require(amount > 0, "Enter an amount");

    require(getAccountStakingBalanceInternal(s, key, msg.sender) >= amount, "Insufficient balance");
    require(block.number > canWithdrawFromBlockHeightInternal(s, key, msg.sender), "Withdrawal too early");

    stakingToken = s.getStakingTokenAddressInternal(key);

    // First withdraw your rewards
    withdrawRewardsInternal(s, key, msg.sender);

    // Individual state
    s.subtractUintByKeys(StakingPoolCoreLibV1.NS_POOL_STAKING_TOKEN_BALANCE, key, msg.sender, amount);

    // Global state
    s.subtractUintByKeys(StakingPoolCoreLibV1.NS_POOL_STAKING_TOKEN_BALANCE, key, amount);

    IERC20(stakingToken).ensureTransfer(msg.sender, amount);
  }

Contracts