View Source: contracts/libraries/StakingPoolLibV1.sol
StakingPoolLibV1
- getInfoInternal(IStore s, bytes32 key, address you)
- getPoolStakeBalanceInternal(IStore s, bytes32 key)
- getPoolCumulativeDeposits(IStore s, bytes32 key)
- getAccountStakingBalanceInternal(IStore s, bytes32 key, address account)
- getTotalBlocksSinceLastRewardInternal(IStore s, bytes32 key, address account)
- canWithdrawFromBlockHeightInternal(IStore s, bytes32 key, address account)
- getLastDepositHeight(IStore s, bytes32 key, address account)
- getLastRewardHeight(IStore s, bytes32 key, address account)
- calculateRewardsInternal(IStore s, bytes32 key, address account)
- withdrawRewardsInternal(IStore s, bytes32 key, address account)
- depositInternal(IStore s, bytes32 key, uint256 amount)
- withdrawInternal(IStore s, bytes32 key, uint256 amount)
Gets the info of a given staking pool by key
function getInfoInternal(IStore s, bytes32 key, address you) external view
returns(info struct IStakingPools.StakingPoolInfoType)
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 (IStakingPools.StakingPoolInfoType memory info) {
bool valid = s.checkIfStakingPoolExists(key);
if (valid) {
info.name = s.getStringByKeys(StakingPoolCoreLibV1.NS_POOL, key);
info.stakingToken = s.getStakingTokenAddressInternal(key);
info.stakingTokenStablecoinPair = s.getStakingTokenStablecoinPairAddressInternal(key);
info.rewardToken = s.getRewardTokenAddressInternal(key);
info.rewardTokenStablecoinPair = s.getRewardTokenStablecoinPairAddressInternal(key);
info.totalStaked = s.getTotalStaked(key);
info.target = s.getTarget(key);
info.maximumStake = s.getMaximumStakeInternal(key);
info.stakeBalance = getPoolStakeBalanceInternal(s, key);
info.cumulativeDeposits = getPoolCumulativeDeposits(s, key);
info.rewardPerBlock = s.getRewardPerBlock(key);
info.platformFee = s.getRewardPlatformFee(key);
info.lockupPeriod = s.getLockupPeriodInBlocks(key);
info.rewardTokenBalance = s.getRewardTokenBalance(key);
info.accountStakeBalance = getAccountStakingBalanceInternal(s, key, you);
info.totalBlockSinceLastReward = getTotalBlocksSinceLastRewardInternal(s, key, you);
info.rewards = calculateRewardsInternal(s, key, you);
info.canWithdrawFromBlockHeight = canWithdrawFromBlockHeightInternal(s, key, you);
info.lastDepositHeight = getLastDepositHeight(s, key, you);
info.lastRewardHeight = getLastRewardHeight(s, key, you);
}
}
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;
}
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;
}
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);
}
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;
}
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);
if (lastDepositHeight == 0) {
return 0;
}
uint256 lockupPeriod = s.getLockupPeriodInBlocks(key);
return lastDepositHeight + lockupPeriod;
}
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);
}
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);
}
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);
uint256 rewards = (myStake * rewardPerBlock * totalBlocks) / 1 ether;
uint256 poolBalance = s.getRewardTokenBalance(key);
return rewards > poolBalance ? poolBalance : rewards;
}
Withdraws the rewards of the caller (if any or if available).
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
)
{
require(s.getRewardPlatformFee(key) <= ProtoUtilV1.MULTIPLIER, "Invalid reward platform fee");
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
// @suppress-division Checked side effects. If the reward platform fee is zero
// or a very small number, platform fee becomes zero because of data loss
platformFee = (rewards * s.getRewardPlatformFee(key)) / ProtoUtilV1.MULTIPLIER;
// @suppress-subtraction If `getRewardPlatformFee` is 100%, the following can result in zero value.
if (rewards - platformFee > 0) {
IERC20(rewardToken).ensureTransfer(msg.sender, rewards - platformFee);
}
if (platformFee > 0) {
IERC20(rewardToken).ensureTransfer(s.getTreasury(), platformFee);
}
}
Deposit the specified amount of staking token to the specified pool.
function depositInternal(IStore s, bytes32 key, uint256 amount) external nonpayable
returns(stakingToken address, rewardToken address, rewards uint256, rewardsPlatformFee uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
key | bytes32 | |
amount | uint256 |
Source Code
function depositInternal(
IStore s,
bytes32 key,
uint256 amount
)
external
returns (
address stakingToken,
address rewardToken,
uint256 rewards,
uint256 rewardsPlatformFee
)
{
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
(rewardToken, rewards, rewardsPlatformFee) = 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);
}
Withdraw the specified amount of staking token from the specified pool.
function withdrawInternal(IStore s, bytes32 key, uint256 amount) external nonpayable
returns(stakingToken address, rewardToken address, rewards uint256, rewardsPlatformFee uint256)
Arguments
Name | Type | Description |
---|---|---|
s | IStore | |
key | bytes32 | |
amount | uint256 |
Source Code
function withdrawInternal(
IStore s,
bytes32 key,
uint256 amount
)
external
returns (
address stakingToken,
address rewardToken,
uint256 rewards,
uint256 rewardsPlatformFee
)
{
require(amount > 0, "Please specify 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
(rewardToken, rewards, rewardsPlatformFee) = withdrawRewardsInternal(s, key, msg.sender);
// @suppress-subtraction The maximum amount that can be withdrawn is the staked balance
// and therefore underflow is not possible.
// 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);
}
- AaveStrategy
- AccessControl
- AccessControlLibV1
- Address
- BaseLibV1
- BokkyPooBahsDateTimeLibrary
- BondPool
- BondPoolBase
- BondPoolLibV1
- CompoundStrategy
- Context
- Cover
- CoverBase
- CoverLibV1
- CoverReassurance
- CoverStake
- CoverUtilV1
- cxToken
- cxTokenFactory
- cxTokenFactoryLibV1
- Delayable
- Destroyable
- ERC165
- ERC20
- FakeAaveLendingPool
- FakeCompoundDaiDelegator
- FakePriceOracle
- FakeRecoverable
- FakeStore
- FakeToken
- FakeUniswapPair
- FakeUniswapV2FactoryLike
- FakeUniswapV2PairLike
- FakeUniswapV2RouterLike
- FaultyAaveLendingPool
- FaultyCompoundDaiDelegator
- Finalization
- ForceEther
- Governance
- GovernanceUtilV1
- IAaveV2LendingPoolLike
- IAccessControl
- IBondPool
- IClaimsProcessor
- ICompoundERC20DelegatorLike
- ICover
- ICoverReassurance
- ICoverStake
- ICxToken
- ICxTokenFactory
- IERC165
- IERC20
- IERC20Detailed
- IERC20Metadata
- IERC3156FlashBorrower
- IERC3156FlashLender
- IFinalization
- IGovernance
- ILendingStrategy
- ILiquidityEngine
- IMember
- INeptuneRouterV1
- InvalidStrategy
- IPausable
- IPolicy
- IPolicyAdmin
- IPriceOracle
- IProtocol
- IRecoverable
- IReporter
- IResolution
- IResolvable
- IStakingPools
- IStore
- IStoreLike
- IUniswapV2FactoryLike
- IUniswapV2PairLike
- IUniswapV2RouterLike
- IUnstakable
- IVault
- IVaultDelegate
- IVaultFactory
- IWitness
- LiquidityEngine
- MaliciousToken
- MockAccessControlUser
- MockCoverUtilUser
- MockCxToken
- MockCxTokenPolicy
- MockCxTokenStore
- MockFlashBorrower
- MockLiquidityEngineUser
- MockProcessorStore
- MockProcessorStoreLib
- MockProtocol
- MockRegistryClient
- MockStore
- MockStoreKeyUtilUser
- MockValidationLibUser
- MockVault
- MockVaultLibUser
- NeptuneRouterV1
- NPM
- NpmDistributor
- NTransferUtilV2
- NTransferUtilV2Intermediate
- Ownable
- Pausable
- Policy
- PolicyAdmin
- PolicyHelperV1
- PoorMansERC20
- POT
- PriceLibV1
- Processor
- ProtoBase
- Protocol
- ProtoUtilV1
- Recoverable
- ReentrancyGuard
- RegistryLibV1
- Reporter
- Resolution
- Resolvable
- RoutineInvokerLibV1
- SafeERC20
- StakingPoolBase
- StakingPoolCoreLibV1
- StakingPoolInfo
- StakingPoolLibV1
- StakingPoolReward
- StakingPools
- Store
- StoreBase
- StoreKeyUtil
- StrategyLibV1
- Strings
- TimelockController
- Unstakable
- ValidationLibV1
- Vault
- VaultBase
- VaultDelegate
- VaultDelegateBase
- VaultDelegateWithFlashLoan
- VaultFactory
- VaultFactoryLibV1
- VaultLibV1
- VaultLiquidity
- VaultStrategy
- WithFlashLoan
- WithPausability
- WithRecovery
- Witness