Skip to content

Latest commit

 

History

History
703 lines (557 loc) · 20.1 KB

BondPoolLibV1.md

File metadata and controls

703 lines (557 loc) · 20.1 KB

BondPoolLibV1.sol

View Source: contracts/libraries/BondPoolLibV1.sol

BondPoolLibV1

Contract Members

Constants & Variables

bytes32 public constant NS_BOND_TO_CLAIM;
bytes32 public constant NS_BOND_CONTRIBUTION;
bytes32 public constant NS_BOND_LP_TOKEN;
bytes32 public constant NS_LQ_TREASURY;
bytes32 public constant NS_BOND_DISCOUNT_RATE;
bytes32 public constant NS_BOND_MAX_UNIT;
bytes32 public constant NS_BOND_VESTING_TERM;
bytes32 public constant NS_BOND_UNLOCK_DATE;
bytes32 public constant NS_BOND_TOTAL_NPM_ALLOCATED;
bytes32 public constant NS_BOND_TOTAL_NPM_DISTRIBUTED;

Functions

calculateTokensForLpInternal

Calculates the discounted NPM token to be given for the NPM/Stablecoin Uniswap v2 LP token units.

function calculateTokensForLpInternal(IStore s, uint256 lpTokens) public view
returns(uint256)

Arguments

Name Type Description
s IStore Specify store instance
lpTokens uint256 Enter the NPM/Stablecoin Uniswap v2 LP token units
Source Code
function calculateTokensForLpInternal(IStore s, uint256 lpTokens) public view returns (uint256) {
    uint256 dollarValue = s.convertNpmLpUnitsToStabelcoinInternal(lpTokens);

    uint256 npmPrice = s.getNpmPriceInternal(1 ether);
    uint256 discount = _getDiscountRate(s);
    uint256 discountedNpmPrice = (npmPrice * (ProtoUtilV1.MULTIPLIER - discount)) / ProtoUtilV1.MULTIPLIER;

    uint256 npmForContribution = (dollarValue * 1 ether) / discountedNpmPrice;

    return npmForContribution;
  }

getBondPoolInfoInternal

Gets the bond pool information

function getBondPoolInfoInternal(IStore s, address you) external view
returns(info struct IBondPool.BondPoolInfoType)

Arguments

Name Type Description
s IStore Provide a store instance
you address
Source Code
function getBondPoolInfoInternal(IStore s, address you) external view returns (IBondPool.BondPoolInfoType memory info) {
    info.lpToken = _getLpTokenAddress(s);

    info.marketPrice = s.getNpmPriceInternal(1 ether);
    info.discountRate = _getDiscountRate(s);
    info.vestingTerm = _getVestingTerm(s);
    info.maxBond = _getMaxBondInUnit(s);
    info.totalNpmAllocated = _getTotalNpmAllocated(s);
    info.totalNpmDistributed = _getTotalNpmDistributed(s);
    info.npmAvailable = IERC20(s.getNpmTokenInstanceInternal()).balanceOf(address(this));

    info.bondContribution = _getYourBondContribution(s, you); // total lp tokens contributed by you
    info.claimable = _getYourBondClaimable(s, you); // your total claimable NPM tokens at the end of the vesting period or "unlock date"
    info.unlockDate = _getYourBondUnlockDate(s, you); // your vesting period end or "unlock date"
  }

_getLpTokenAddress

Gets the NPM/Stablecoin Uniswap v2 LP token address

function _getLpTokenAddress(IStore s) private view
returns(address)

Arguments

Name Type Description
s IStore
Source Code
function _getLpTokenAddress(IStore s) private view returns (address) {
    return s.getAddressByKey(BondPoolLibV1.NS_BOND_LP_TOKEN);
  }

_getYourBondContribution

Gets your unsettled bond contribution amount.

function _getYourBondContribution(IStore s, address you) private view
returns(uint256)

Arguments

Name Type Description
s IStore
you address
Source Code
function _getYourBondContribution(IStore s, address you) private view returns (uint256) {
    return s.getUintByKey(keccak256(abi.encodePacked(BondPoolLibV1.NS_BOND_CONTRIBUTION, you)));
  }

_getYourBondClaimable

Gets your claimable discounted NPM bond amount.

function _getYourBondClaimable(IStore s, address you) private view
returns(uint256)

Arguments

Name Type Description
s IStore
you address
Source Code
function _getYourBondClaimable(IStore s, address you) private view returns (uint256) {
    return s.getUintByKey(keccak256(abi.encodePacked(BondPoolLibV1.NS_BOND_TO_CLAIM, you)));
  }

_getYourBondUnlockDate

Returns the date when your discounted NPM token bond is unlocked for claim.

function _getYourBondUnlockDate(IStore s, address you) private view
returns(uint256)

Arguments

Name Type Description
s IStore
you address
Source Code
function _getYourBondUnlockDate(IStore s, address you) private view returns (uint256) {
    return s.getUintByKey(keccak256(abi.encodePacked(BondPoolLibV1.NS_BOND_UNLOCK_DATE, you)));
  }

_getDiscountRate

Returns the NPM token bond discount rate

function _getDiscountRate(IStore s) private view
returns(uint256)

Arguments

Name Type Description
s IStore
Source Code
function _getDiscountRate(IStore s) private view returns (uint256) {
    return s.getUintByKey(NS_BOND_DISCOUNT_RATE);
  }

_getVestingTerm

Returns the bond vesting term

function _getVestingTerm(IStore s) private view
returns(uint256)

Arguments

Name Type Description
s IStore
Source Code
function _getVestingTerm(IStore s) private view returns (uint256) {
    return s.getUintByKey(NS_BOND_VESTING_TERM);
  }

_getMaxBondInUnit

Returns the maximum NPM token units that can be bonded at a time

function _getMaxBondInUnit(IStore s) private view
returns(uint256)

Arguments

Name Type Description
s IStore
Source Code
function _getMaxBondInUnit(IStore s) private view returns (uint256) {
    return s.getUintByKey(NS_BOND_MAX_UNIT);
  }

_getTotalNpmAllocated

Returns the total NPM tokens allocated for the bond

function _getTotalNpmAllocated(IStore s) private view
returns(uint256)

Arguments

Name Type Description
s IStore
Source Code
function _getTotalNpmAllocated(IStore s) private view returns (uint256) {
    return s.getUintByKey(NS_BOND_TOTAL_NPM_ALLOCATED);
  }

_getTotalNpmDistributed

Returns the total bonded NPM tokens distributed till date.

function _getTotalNpmDistributed(IStore s) private view
returns(uint256)

Arguments

Name Type Description
s IStore
Source Code
function _getTotalNpmDistributed(IStore s) private view returns (uint256) {
    return s.getUintByKey(NS_BOND_TOTAL_NPM_DISTRIBUTED);
  }

createBondInternal

Create a new NPM/stablecoin LP token bond

function createBondInternal(IStore s, uint256 lpTokens, uint256 minNpmDesired) external nonpayable
returns(npmToVest uint256, unlockDate uint256)

Arguments

Name Type Description
s IStore Specify store instance
lpTokens uint256 Enter the total units of NPM/Stablecoin Uniswap v2 tokens to be bonded
minNpmDesired uint256 Enter the minimum NPM tokens you desire for the given LP tokens. This transaction will revert if the final NPM bond is less than your specified value.
Source Code
function createBondInternal(
    IStore s,
    uint256 lpTokens,
    uint256 minNpmDesired
  ) external returns (uint256 npmToVest, uint256 unlockDate) {
    s.mustNotBePaused();

    npmToVest = calculateTokensForLpInternal(s, lpTokens);

    require(npmToVest <= _getMaxBondInUnit(s), "Bond too big");
    require(npmToVest >= minNpmDesired, "Min bond `minNpmDesired` failed");
    require(_getNpmBalance(s) >= npmToVest + _getBondCommitment(s), "NPM balance insufficient to bond");

    // Pull the tokens from the requester's account
    IERC20(s.getAddressByKey(BondPoolLibV1.NS_BOND_LP_TOKEN)).ensureTransferFrom(msg.sender, s.getAddressByKey(BondPoolLibV1.NS_LQ_TREASURY), lpTokens);

    // Commitment: Total NPM to reserve for bond claims
    s.addUintByKey(BondPoolLibV1.NS_BOND_TO_CLAIM, npmToVest);

    // Your bond to claim later
    bytes32 k = keccak256(abi.encodePacked(BondPoolLibV1.NS_BOND_TO_CLAIM, msg.sender));
    s.addUintByKey(k, npmToVest);

    // Amount contributed
    k = keccak256(abi.encodePacked(BondPoolLibV1.NS_BOND_CONTRIBUTION, msg.sender));
    s.addUintByKey(k, lpTokens);

    // unlock date
    unlockDate = block.timestamp + _getVestingTerm(s); // solhint-disable-line

    // Unlock date
    k = keccak256(abi.encodePacked(BondPoolLibV1.NS_BOND_UNLOCK_DATE, msg.sender));
    s.setUintByKey(k, unlockDate);
  }

_getNpmBalance

Gets the NPM token balance of this contract. Please also see _getBondCommitment to check the total NPM tokens already allocated to the bonders to be claimed later.

function _getNpmBalance(IStore s) private view
returns(uint256)

Arguments

Name Type Description
s IStore Specify store instance
Source Code
function _getNpmBalance(IStore s) private view returns (uint256) {
    return IERC20(s.getNpmTokenInstanceInternal()).balanceOf(address(this));
  }

_getBondCommitment

Returns the bond commitment amount.

function _getBondCommitment(IStore s) private view
returns(uint256)

Arguments

Name Type Description
s IStore
Source Code
function _getBondCommitment(IStore s) private view returns (uint256) {
    return s.getUintByKey(BondPoolLibV1.NS_BOND_TO_CLAIM);
  }

claimBondInternal

Enables the caller to claim their bond after the lockup period.

function claimBondInternal(IStore s) external nonpayable
returns(npmToTransfer uint256)

Arguments

Name Type Description
s IStore
Source Code
function claimBondInternal(IStore s) external returns (uint256 npmToTransfer) {
    s.mustNotBePaused();

    npmToTransfer = _getYourBondClaimable(s, msg.sender); // npmToTransfer

    // Commitment: Reduce NPM reserved for claims
    s.subtractUintByKey(BondPoolLibV1.NS_BOND_TO_CLAIM, npmToTransfer);

    // Clear the claim amount
    s.deleteUintByKey(keccak256(abi.encodePacked(BondPoolLibV1.NS_BOND_TO_CLAIM, msg.sender)));

    uint256 unlocksOn = _getYourBondUnlockDate(s, msg.sender);

    // Clear the unlock date
    s.deleteUintByKey(keccak256(abi.encodePacked(BondPoolLibV1.NS_BOND_UNLOCK_DATE, msg.sender)));

    require(block.timestamp >= unlocksOn, "Still vesting"); // solhint-disable-line
    require(npmToTransfer > 0, "Nothing to claim");

    s.addUintByKey(BondPoolLibV1.NS_BOND_TOTAL_NPM_DISTRIBUTED, npmToTransfer);
    IERC20(s.getNpmTokenInstanceInternal()).ensureTransfer(msg.sender, npmToTransfer);
  }

setupBondPoolInternal

Sets up the bond pool

function setupBondPoolInternal(IStore s, struct IBondPool.SetupBondPoolArgs args) external nonpayable

Arguments

Name Type Description
s IStore
args struct IBondPool.SetupBondPoolArgs
Source Code
function setupBondPoolInternal(IStore s, IBondPool.SetupBondPoolArgs calldata args) external {
    if (args.lpToken != address(0)) {
      s.setAddressByKey(BondPoolLibV1.NS_BOND_LP_TOKEN, args.lpToken);
    }

    if (args.treasury != address(0)) {
      s.setAddressByKey(BondPoolLibV1.NS_LQ_TREASURY, args.treasury);
    }

    if (args.bondDiscountRate > 0) {
      s.setUintByKey(BondPoolLibV1.NS_BOND_DISCOUNT_RATE, args.bondDiscountRate);
    }

    if (args.maxBondAmount > 0) {
      s.setUintByKey(BondPoolLibV1.NS_BOND_MAX_UNIT, args.maxBondAmount);
    }

    if (args.vestingTerm > 0) {
      s.setUintByKey(BondPoolLibV1.NS_BOND_VESTING_TERM, args.vestingTerm);
    }

    if (args.npmToTopUpNow > 0) {
      IERC20(s.getNpmTokenInstanceInternal()).ensureTransferFrom(msg.sender, address(this), args.npmToTopUpNow);
      s.addUintByKey(BondPoolLibV1.NS_BOND_TOTAL_NPM_ALLOCATED, args.npmToTopUpNow);
    }
  }

Contracts