Skip to content

Latest commit

 

History

History
412 lines (339 loc) · 12.2 KB

CoverStake.md

File metadata and controls

412 lines (339 loc) · 12.2 KB

Cover Stake (CoverStake.sol)

View Source: contracts/core/lifecycle/CoverStake.sol

↗ Extends: ICoverStake, Recoverable

CoverStake

When you create a new cover, you have to specify the amount of NPM tokens you wish to stake as a cover creator.

To demonstrate support for a cover pool, anyone can add and remove NPM stakes (minimum required). The higher the sake, the more visibility the contract gets if there are multiple cover contracts with the same name or similar terms. Even when there are no duplicate contract, a higher stake would normally imply a better cover pool commitment.

Functions

Constructs this contract

function (IStore store) public nonpayable Recoverable 

Arguments

Name Type Description
store IStore Provide the store contract instance
Source Code
constructor(IStore store) Recoverable(store) {}

increaseStake

Increase the stake of the given cover pool

function increaseStake(bytes32 coverKey, address account, uint256 amount, uint256 fee) external nonpayable nonReentrant 

Arguments

Name Type Description
coverKey bytes32 Enter the cover key
account address Enter the account from where the NPM tokens will be transferred
amount uint256 Enter the amount of stake
fee uint256 Enter the fee amount. Note: do not enter the fee if you are directly calling this function.
Source Code
function increaseStake(
    bytes32 coverKey,
    address account,
    uint256 amount,
    uint256 fee
  ) external override nonReentrant {
    s.mustNotBePaused();
    s.mustBeValidCoverKey(coverKey);
    s.senderMustBeCoverContract();

    require(amount >= fee, "Invalid fee");

    s.npmToken().ensureTransferFrom(msg.sender, address(this), amount);

    if (fee > 0) {
      s.npmToken().ensureTransfer(s.getBurnAddress(), fee);
      emit FeeBurned(coverKey, fee);
    }

    // @suppress-subtraction Checked usage. Fee is always less than amount
    // if we reach this far.
    s.addUintByKeys(ProtoUtilV1.NS_COVER_STAKE, coverKey, amount - fee);
    s.addUintByKeys(ProtoUtilV1.NS_COVER_STAKE_OWNED, coverKey, account, amount - fee);

    emit StakeAdded(coverKey, account, amount - fee);
  }

decreaseStake

Decreases the stake from the given cover pool. A cover creator can withdraw their full stake after 365 days

function decreaseStake(bytes32 coverKey, uint256 amount) external nonpayable nonReentrant 

Arguments

Name Type Description
coverKey bytes32 Enter the cover key
amount uint256 Enter the amount of stake to decrease
Source Code
function decreaseStake(bytes32 coverKey, uint256 amount) external override nonReentrant {
    s.mustNotBePaused();
    s.mustBeValidCoverKey(coverKey);
    s.mustEnsureAllProductsAreNormal(coverKey);

    uint256 drawingPower = _getDrawingPower(coverKey, msg.sender);
    require(amount > 0, "Please specify amount");
    require(drawingPower >= amount, "Exceeds your drawing power");

    // @suppress-subtraction
    s.subtractUintByKeys(ProtoUtilV1.NS_COVER_STAKE, coverKey, amount);
    s.subtractUintByKeys(ProtoUtilV1.NS_COVER_STAKE_OWNED, coverKey, msg.sender, amount);

    s.npmToken().ensureTransfer(msg.sender, amount);

    s.updateStateAndLiquidity(coverKey);

    emit StakeRemoved(coverKey, msg.sender, amount);
  }

stakeOf

Gets the stake of an account for the given cover key

function stakeOf(bytes32 coverKey, address account) public view
returns(uint256)

Arguments

Name Type Description
coverKey bytes32 Enter the cover key
account address Specify the account to obtain the stake of

Returns

Returns the total stake of the specified account on the given cover key

Source Code
function stakeOf(bytes32 coverKey, address account) public view override returns (uint256) {
    return s.getUintByKeys(ProtoUtilV1.NS_COVER_STAKE_OWNED, coverKey, account);
  }

_getDrawingPower

Gets the drawing power of (the stake amount that can be withdrawn from) an account.

function _getDrawingPower(bytes32 coverKey, address account) private view
returns(uint256)

Arguments

Name Type Description
coverKey bytes32 Enter the cover key
account address Specify the account to obtain the drawing power of

Returns

Returns the drawing power of the specified account on the given cover key

Source Code
function _getDrawingPower(bytes32 coverKey, address account) private view returns (uint256) {
    uint256 createdAt = s.getCoverCreationDate(coverKey);
    uint256 yourStake = stakeOf(coverKey, account);
    bool isOwner = account == s.getCoverOwner(coverKey);

    uint256 minStakeRequired = block.timestamp > createdAt + 365 days ? 0 : s.getMinCoverCreationStake(); // solhint-disable-line

    return isOwner ? yourStake - minStakeRequired : yourStake;
  }

version

Version number of this contract

function version() external pure
returns(bytes32)

Arguments

Name Type Description
Source Code
function version() external pure override returns (bytes32) {
    return "v0.1";
  }

getName

Name of this contract

function getName() external pure
returns(bytes32)

Arguments

Name Type Description
Source Code
function getName() external pure override returns (bytes32) {
    return ProtoUtilV1.CNAME_COVER_STAKE;
  }

Contracts