Skip to content

Latest commit

 

History

History
712 lines (576 loc) · 20.8 KB

StakingPoolCoreLibV1.md

File metadata and controls

712 lines (576 loc) · 20.8 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 = getTotalStakedInternal(s, key);
    uint256 target = getTargetInternal(s, key);

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

    return target - totalStaked;
  }

getTargetInternal

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

Arguments

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

getRewardPlatformFeeInternal

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

Arguments

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

getTotalStakedInternal

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

Arguments

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

getRewardPerBlockInternal

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

Arguments

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

getLockupPeriodInBlocksInternal

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

Arguments

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

getRewardTokenBalanceInternal

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

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function getRewardTokenBalanceInternal(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);
  }

ensureValidStakingPoolInternal

function ensureValidStakingPoolInternal(IStore s, bytes32 key) external view

Arguments

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

checkIfStakingPoolExistsInternal

function checkIfStakingPoolExistsInternal(IStore s, bytes32 key) public view
returns(bool)

Arguments

Name Type Description
s IStore
key bytes32
Source Code
function checkIfStakingPoolExistsInternal(IStore s, bytes32 key) public view returns (bool) {
    return s.getBoolByKeys(NS_POOL, key);
  }

validateAddOrEditPoolInternal

function validateAddOrEditPoolInternal(IStore s, struct IStakingPools.AddOrEditPoolArgs args) public view
returns(bool)

Arguments

Name Type Description
s IStore
args struct IStakingPools.AddOrEditPoolArgs
Source Code
function validateAddOrEditPoolInternal(IStore s, IStakingPools.AddOrEditPoolArgs calldata args) public view returns (bool) {
    require(args.key > 0, "Invalid key");

    bool exists = checkIfStakingPoolExistsInternal(s, args.key);

    if (exists == false) {
      require(bytes(args.name).length > 0, "Invalid name");
      require(args.stakingToken != address(0), "Invalid staking token");
      // require(addresses[1] != address(0), "Invalid staking token pair"); // A POD doesn't have any pair with stablecion
      require(args.rewardToken != address(0), "Invalid reward token");
      require(args.uniRewardTokenDollarPair != address(0), "Invalid reward token pair");
      require(args.lockupPeriod > 0, "Provide lockup period in blocks");
      require(args.rewardTokenToDeposit > 0, "Provide reward token allocation");
      require(args.rewardPerBlock > 0, "Provide reward per block");
      require(args.stakingTarget > 0, "Please provide staking target");
    }

    return exists;
  }

addOrEditPoolInternal

Adds or edits the pool by key

function addOrEditPoolInternal(IStore s, struct IStakingPools.AddOrEditPoolArgs args) external nonpayable

Arguments

Name Type Description
s IStore
args struct IStakingPools.AddOrEditPoolArgs
Source Code
function addOrEditPoolInternal(IStore s, IStakingPools.AddOrEditPoolArgs calldata args) external {
    // @suppress-zero-value-check The uint values are checked in the function `validateAddOrEditPoolInternal`
    bool poolExists = validateAddOrEditPoolInternal(s, args);

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

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

    _updatePoolValues(s, args);

    if (args.rewardTokenToDeposit > 0) {
      IERC20(args.rewardToken).ensureTransferFrom(msg.sender, address(this), args.rewardTokenToDeposit);
    }
  }

_updatePoolValues

Updates the values of a staking pool by the given key

function _updatePoolValues(IStore s, struct IStakingPools.AddOrEditPoolArgs args) private nonpayable

Arguments

Name Type Description
s IStore Provide an instance of the store
args struct IStakingPools.AddOrEditPoolArgs
Source Code
function _updatePoolValues(IStore s, IStakingPools.AddOrEditPoolArgs calldata args) private {
    if (args.stakingTarget > 0) {
      s.setUintByKeys(NS_POOL_STAKING_TARGET, args.key, args.stakingTarget);
    }

    if (args.maxStake > 0) {
      s.setUintByKeys(NS_POOL_MAX_STAKE, args.key, args.maxStake);
    }

    if (args.platformFee > 0) {
      s.setUintByKeys(NS_POOL_REWARD_PLATFORM_FEE, args.key, args.platformFee);
    }

    if (args.rewardPerBlock > 0) {
      s.setUintByKeys(NS_POOL_REWARD_PER_BLOCK, args.key, args.rewardPerBlock);
    }

    if (args.lockupPeriod > 0) {
      s.setUintByKeys(NS_POOL_LOCKUP_PERIOD_IN_BLOCKS, args.key, args.lockupPeriod);
    }

    if (args.rewardTokenToDeposit > 0) {
      s.addUintByKeys(NS_POOL_REWARD_TOKEN_DEPOSITS, args.key, args.rewardTokenToDeposit);
      s.addUintByKeys(NS_POOL_REWARD_TOKEN_BALANCE, args.key, args.rewardTokenToDeposit);
    }
  }

_initializeNewPool

Initializes a new pool by the given key. Assumes that the pool does not exist.

function _initializeNewPool(IStore s, struct IStakingPools.AddOrEditPoolArgs args) private nonpayable

Arguments

Name Type Description
s IStore Provide an instance of the store
args struct IStakingPools.AddOrEditPoolArgs
Source Code
function _initializeNewPool(IStore s, IStakingPools.AddOrEditPoolArgs calldata args) private {
    s.setAddressByKeys(NS_POOL_STAKING_TOKEN, args.key, args.stakingToken);
    s.setAddressByKeys(NS_POOL_STAKING_TOKEN_UNI_STABLECOIN_PAIR, args.key, args.uniStakingTokenDollarPair);
    s.setAddressByKeys(NS_POOL_REWARD_TOKEN, args.key, args.rewardToken);
    s.setAddressByKeys(NS_POOL_REWARD_TOKEN_UNI_STABLECOIN_PAIR, args.key, args.uniRewardTokenDollarPair);

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

Contracts