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

Latest commit

 

History

History
692 lines (562 loc) · 19.4 KB

StakingPoolCoreLibV1.md

File metadata and controls

692 lines (562 loc) · 19.4 KB

StakingPoolCoreLibV1.sol

View Source: contracts/libraries/StakingPoolCoreLibV1.sol

StakingPoolCoreLibV1

Contract Members

Constants & Variables

bytes32 public constant NS_POOL;
bytes32 public constant NS_POOL_NAME;
bytes32 public constant NS_POOL_LOCKED;
bytes32 public constant NS_POOL_LOCKUP_PERIOD_IN_BLOCKS;
bytes32 public constant NS_POOL_STAKING_TARGET;
bytes32 public constant NS_POOL_CUMULATIVE_STAKING_AMOUNT;
bytes32 public constant NS_POOL_STAKING_TOKEN;
bytes32 public constant NS_POOL_STAKING_TOKEN_UNI_STABLECOIN_PAIR;
bytes32 public constant NS_POOL_REWARD_TOKEN;
bytes32 public constant NS_POOL_REWARD_TOKEN_UNI_STABLECOIN_PAIR;
bytes32 public constant NS_POOL_STAKING_TOKEN_BALANCE;
bytes32 public constant NS_POOL_REWARD_TOKEN_DEPOSITS;
bytes32 public constant NS_POOL_REWARD_TOKEN_DISTRIBUTION;
bytes32 public constant NS_POOL_MAX_STAKE;
bytes32 public constant NS_POOL_REWARD_PER_BLOCK;
bytes32 public constant NS_POOL_REWARD_PLATFORM_FEE;
bytes32 public constant NS_POOL_REWARD_TOKEN_BALANCE;
bytes32 public constant NS_POOL_DEPOSIT_HEIGHTS;
bytes32 public constant NS_POOL_REWARD_HEIGHTS;
bytes32 public constant NS_POOL_TOTAL_REWARD_GIVEN;

Functions

getAvailableToStakeInternal

Reports the remaining amount of tokens that can be staked in this pool

function getAvailableToStakeInternal(IStore s, bytes32 key) external view
returns(uint256)

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function getAvailableToStakeInternal(IStore s, bytes32 key) external view returns (uint256) {
    uint256 totalStaked = getTotalStaked(s, key);
    uint256 target = getTarget(s, key);

    if (totalStaked >= target) {
      return 0;
    }

    return target - totalStaked;
  }

getTarget

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

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function getTarget(IStore s, bytes32 key) public view returns (uint256) {
    return s.getUintByKeys(NS_POOL_STAKING_TARGET, key);
  }

getRewardPlatformFee

function getRewardPlatformFee(IStore s, bytes32 key) external view
returns(uint256)

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function getRewardPlatformFee(IStore s, bytes32 key) external view returns (uint256) {
    return s.getUintByKeys(NS_POOL_REWARD_PLATFORM_FEE, key);
  }

getTotalStaked

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

Arguments

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

getRewardPerBlock

function getRewardPerBlock(IStore s, bytes32 key) external view
returns(uint256)

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function getRewardPerBlock(IStore s, bytes32 key) external view returns (uint256) {
    return s.getUintByKeys(NS_POOL_REWARD_PER_BLOCK, key);
  }

getLockupPeriodInBlocks

function getLockupPeriodInBlocks(IStore s, bytes32 key) external view
returns(uint256)

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function getLockupPeriodInBlocks(IStore s, bytes32 key) external view returns (uint256) {
    return s.getUintByKeys(NS_POOL_LOCKUP_PERIOD_IN_BLOCKS, key);
  }

getRewardTokenBalance

function getRewardTokenBalance(IStore s, bytes32 key) external view
returns(uint256)

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function getRewardTokenBalance(IStore s, bytes32 key) external view returns (uint256) {
    return s.getUintByKeys(NS_POOL_REWARD_TOKEN_BALANCE, key);
  }

getMaximumStakeInternal

function getMaximumStakeInternal(IStore s, bytes32 key) external view
returns(uint256)

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function getMaximumStakeInternal(IStore s, bytes32 key) external view returns (uint256) {
    return s.getUintByKeys(NS_POOL_MAX_STAKE, key);
  }

getStakingTokenAddressInternal

function getStakingTokenAddressInternal(IStore s, bytes32 key) external view
returns(address)

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function getStakingTokenAddressInternal(IStore s, bytes32 key) external view returns (address) {
    return s.getAddressByKeys(NS_POOL_STAKING_TOKEN, key);
  }

getStakingTokenStablecoinPairAddressInternal

function getStakingTokenStablecoinPairAddressInternal(IStore s, bytes32 key) external view
returns(address)

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function getStakingTokenStablecoinPairAddressInternal(IStore s, bytes32 key) external view returns (address) {
    return s.getAddressByKeys(NS_POOL_STAKING_TOKEN_UNI_STABLECOIN_PAIR, key);
  }

getRewardTokenAddressInternal

function getRewardTokenAddressInternal(IStore s, bytes32 key) external view
returns(address)

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function getRewardTokenAddressInternal(IStore s, bytes32 key) external view returns (address) {
    return s.getAddressByKeys(NS_POOL_REWARD_TOKEN, key);
  }

getRewardTokenStablecoinPairAddressInternal

function getRewardTokenStablecoinPairAddressInternal(IStore s, bytes32 key) external view
returns(address)

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function getRewardTokenStablecoinPairAddressInternal(IStore s, bytes32 key) external view returns (address) {
    return s.getAddressByKeys(NS_POOL_REWARD_TOKEN_UNI_STABLECOIN_PAIR, key);
  }

ensureValidStakingPool

function ensureValidStakingPool(IStore s, bytes32 key) external view

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function ensureValidStakingPool(IStore s, bytes32 key) external view {
    require(s.getBoolByKeys(NS_POOL, key), "Pool invalid or closed");
  }

validateAddOrEditPoolInternal

function validateAddOrEditPoolInternal(IStore s, bytes32 key, string name, address[] addresses, uint256[] values) public view
returns(bool)

Arguments

Name Type Description
s IStore
key bytes32
name string
addresses address[]
values uint256[]
Source Code
function validateAddOrEditPoolInternal(
    IStore s,
    bytes32 key,
    string memory name,
    address[] memory addresses,
    uint256[] memory values
  ) public view returns (bool) {
    require(key > 0, "Invalid key");

    bool exists = s.getBoolByKeys(NS_POOL, key);

    if (exists == false) {
      require(bytes(name).length > 0, "Invalid name");
      require(addresses[0] != address(0), "Invalid staking token");
      // require(addresses[1] != address(0), "Invalid staking token pair"); // A POD doesn't have any pair with stablecion
      require(addresses[2] != address(0), "Invalid reward token");
      require(addresses[3] != address(0), "Invalid reward token pair");
      require(values[4] > 0, "Provide lockup period in blocks");
      require(values[5] > 0, "Provide reward token balance");
      require(values[3] > 0, "Provide reward per block");
      require(values[0] > 0, "Please provide staking target");
    }

    return exists;
  }

addOrEditPoolInternal

Adds or edits the pool by key

function addOrEditPoolInternal(IStore s, bytes32 key, string name, address[] addresses, uint256[] values) external nonpayable

Arguments

Name Type Description
s IStore
key bytes32 Enter the key of the pool you want to create or edit
name string Enter a name for this pool
addresses address[] [0] stakingToken The token which is staked in this pool
values uint256[] [0] stakingTarget Specify the target amount in the staking token. You can not exceed the target.
Source Code
function addOrEditPoolInternal(
    IStore s,
    bytes32 key,
    string memory name,
    address[] memory addresses,
    uint256[] memory values
  ) external {
    bool poolExists = validateAddOrEditPoolInternal(s, key, name, addresses, values);

    if (poolExists == false) {
      _initializeNewPool(s, key, addresses);
    }

    if (bytes(name).length > 0) {
      s.setStringByKeys(NS_POOL, key, name);
    }

    _updatePoolValues(s, key, values);

    // If `values[5] --> rewardTokenDeposit` is specified, the contract
    // pulls the reward tokens to this contract address
    if (values[5] > 0) {
      IERC20(addresses[2]).ensureTransferFrom(msg.sender, address(this), values[5]);
    }
  }

_updatePoolValues

Updates the values of a staking pool by the given key

function _updatePoolValues(IStore s, bytes32 key, uint256[] values) private nonpayable

Arguments

Name Type Description
s IStore Provide an instance of the store
key bytes32 Enter the key of the pool you want to create or edit
values uint256[] [0] stakingTarget Specify the target amount in the staking token. You can not exceed the target.
Source Code
function _updatePoolValues(
    IStore s,
    bytes32 key,
    uint256[] memory values
  ) private {
    if (values[0] > 0) {
      s.setUintByKeys(NS_POOL_STAKING_TARGET, key, values[0]);
    }

    if (values[1] > 0) {
      s.setUintByKeys(NS_POOL_MAX_STAKE, key, values[1]);
    }

    if (values[2] > 0) {
      s.setUintByKeys(NS_POOL_REWARD_PLATFORM_FEE, key, values[2]);
    }

    if (values[3] > 0) {
      s.setUintByKeys(NS_POOL_REWARD_PER_BLOCK, key, values[3]);
    }

    if (values[4] > 0) {
      s.setUintByKeys(NS_POOL_LOCKUP_PERIOD_IN_BLOCKS, key, values[4]);
    }

    if (values[5] > 0) {
      s.addUintByKeys(NS_POOL_REWARD_TOKEN_DEPOSITS, key, values[5]);
      s.addUintByKeys(NS_POOL_REWARD_TOKEN_BALANCE, key, values[5]);
    }
  }

_initializeNewPool

Initializes a new pool by the given key. Assumes that the pool does not exist. Warning: this feature should not be accessible outside of this library.

function _initializeNewPool(IStore s, bytes32 key, address[] addresses) private nonpayable

Arguments

Name Type Description
s IStore Provide an instance of the store
key bytes32 Enter the key of the pool you want to create or edit
addresses address[] [0] stakingToken The token which is staked in this pool
Source Code
function _initializeNewPool(
    IStore s,
    bytes32 key,
    address[] memory addresses
  ) private {
    s.setAddressByKeys(NS_POOL_STAKING_TOKEN, key, addresses[0]);
    s.setAddressByKeys(NS_POOL_STAKING_TOKEN_UNI_STABLECOIN_PAIR, key, addresses[1]);
    s.setAddressByKeys(NS_POOL_REWARD_TOKEN, key, addresses[2]);
    s.setAddressByKeys(NS_POOL_REWARD_TOKEN_UNI_STABLECOIN_PAIR, key, addresses[3]);

    s.setBoolByKeys(NS_POOL, key, true);
  }

Contracts