Contract Name:
StakingPool
Contract Source Code:
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (token/ERC721/IERC721.sol)
pragma solidity ^0.8.0;
import "../../utils/introspection/IERC165.sol";
/**
* @dev Required interface of an ERC721 compliant contract.
*/
interface IERC721 is IERC165 {
/**
* @dev Emitted when `tokenId` token is transferred from `from` to `to`.
*/
event Transfer(address indexed from, address indexed to, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables `approved` to manage the `tokenId` token.
*/
event Approval(address indexed owner, address indexed approved, uint256 indexed tokenId);
/**
* @dev Emitted when `owner` enables or disables (`approved`) `operator` to manage all of its assets.
*/
event ApprovalForAll(address indexed owner, address indexed operator, bool approved);
/**
* @dev Returns the number of tokens in ``owner``'s account.
*/
function balanceOf(address owner) external view returns (uint256 balance);
/**
* @dev Returns the owner of the `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function ownerOf(uint256 tokenId) external view returns (address owner);
/**
* @dev Safely transfers `tokenId` token from `from` to `to`.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(
address from,
address to,
uint256 tokenId,
bytes calldata data
) external;
/**
* @dev Safely transfers `tokenId` token from `from` to `to`, checking first that contract recipients
* are aware of the ERC721 protocol to prevent tokens from being forever locked.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must exist and be owned by `from`.
* - If the caller is not `from`, it must have been allowed to move this token by either {approve} or {setApprovalForAll}.
* - If `to` refers to a smart contract, it must implement {IERC721Receiver-onERC721Received}, which is called upon a safe transfer.
*
* Emits a {Transfer} event.
*/
function safeTransferFrom(
address from,
address to,
uint256 tokenId
) external;
/**
* @dev Transfers `tokenId` token from `from` to `to`.
*
* WARNING: Usage of this method is discouraged, use {safeTransferFrom} whenever possible.
*
* Requirements:
*
* - `from` cannot be the zero address.
* - `to` cannot be the zero address.
* - `tokenId` token must be owned by `from`.
* - If the caller is not `from`, it must be approved to move this token by either {approve} or {setApprovalForAll}.
*
* Emits a {Transfer} event.
*/
function transferFrom(
address from,
address to,
uint256 tokenId
) external;
/**
* @dev Gives permission to `to` to transfer `tokenId` token to another account.
* The approval is cleared when the token is transferred.
*
* Only a single account can be approved at a time, so approving the zero address clears previous approvals.
*
* Requirements:
*
* - The caller must own the token or be an approved operator.
* - `tokenId` must exist.
*
* Emits an {Approval} event.
*/
function approve(address to, uint256 tokenId) external;
/**
* @dev Approve or remove `operator` as an operator for the caller.
* Operators can call {transferFrom} or {safeTransferFrom} for any token owned by the caller.
*
* Requirements:
*
* - The `operator` cannot be the caller.
*
* Emits an {ApprovalForAll} event.
*/
function setApprovalForAll(address operator, bool _approved) external;
/**
* @dev Returns the account approved for `tokenId` token.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function getApproved(uint256 tokenId) external view returns (address operator);
/**
* @dev Returns if the `operator` is allowed to manage all of the assets of `owner`.
*
* See {setApprovalForAll}
*/
function isApprovedForAll(address owner, address operator) external view returns (bool);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/introspection/IERC165.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC165 standard, as defined in the
* https://eips.ethereum.org/EIPS/eip-165[EIP].
*
* Implementers can declare support of contract interfaces, which can then be
* queried by others ({ERC165Checker}).
*
* For an implementation, see {ERC165}.
*/
interface IERC165 {
/**
* @dev Returns true if this contract implements the interface defined by
* `interfaceId`. See the corresponding
* https://eips.ethereum.org/EIPS/eip-165#how-interfaces-are-identified[EIP section]
* to learn more about how these ids are created.
*
* This function call must use less than 30 000 gas.
*/
function supportsInterface(bytes4 interfaceId) external view returns (bool);
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.18;
abstract contract Multicall {
error RevertedWithoutReason(uint index);
// WARNING: Do not set this function as payable
function multicall(bytes[] calldata data) external returns (bytes[] memory results) {
uint callCount = data.length;
results = new bytes[](callCount);
for (uint i = 0; i < callCount; i++) {
(bool ok, bytes memory result) = address(this).delegatecall(data[i]);
if (!ok) {
uint length = result.length;
// 0 length returned from empty revert() / require(false)
if (length == 0) {
revert RevertedWithoutReason(i);
}
assembly {
revert(add(result, 0x20), length)
}
}
results[i] = result;
}
}
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.5.0;
import "./IStakingPoolFactory.sol";
/**
* @dev IStakingPoolFactory is missing the changeOperator() and operator() functions.
* @dev Any change to the original interface will affect staking pool addresses
* @dev This interface is created to add the missing functions so it can be used in other contracts.
*/
interface ICompleteStakingPoolFactory is IStakingPoolFactory {
function operator() external view returns (address);
function changeOperator(address newOperator) external;
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.5.0;
import "./ICoverNFT.sol";
import "./IStakingNFT.sol";
import "./IStakingPool.sol";
import "./ICompleteStakingPoolFactory.sol";
/* io structs */
enum ClaimMethod {
IndividualClaims,
YieldTokenIncidents
}
struct PoolAllocationRequest {
uint40 poolId;
bool skip;
uint coverAmountInAsset;
}
struct BuyCoverParams {
uint coverId;
address owner;
uint24 productId;
uint8 coverAsset;
uint96 amount;
uint32 period;
uint maxPremiumInAsset;
uint8 paymentAsset;
uint16 commissionRatio;
address commissionDestination;
string ipfsData;
}
/* storage structs */
struct PoolAllocation {
uint40 poolId;
uint96 coverAmountInNXM;
uint96 premiumInNXM;
uint24 allocationId;
}
struct CoverData {
uint24 productId;
uint8 coverAsset;
uint96 amountPaidOut;
}
struct CoverSegment {
uint96 amount;
uint32 start;
uint32 period; // seconds
uint32 gracePeriod; // seconds
uint24 globalRewardsRatio;
uint24 globalCapacityRatio;
}
interface ICover {
/* ========== DATA STRUCTURES ========== */
/* internal structs */
struct RequestAllocationVariables {
uint previousPoolAllocationsLength;
uint previousPremiumInNXM;
uint refund;
uint coverAmountInNXM;
}
/* storage structs */
struct ActiveCover {
// Global active cover amount per asset.
uint192 totalActiveCoverInAsset;
// The last time activeCoverExpirationBuckets was updated
uint64 lastBucketUpdateId;
}
/* ========== VIEWS ========== */
function coverData(uint coverId) external view returns (CoverData memory);
function coverDataCount() external view returns (uint);
function coverSegmentsCount(uint coverId) external view returns (uint);
function coverSegments(uint coverId) external view returns (CoverSegment[] memory);
function coverSegmentWithRemainingAmount(
uint coverId,
uint segmentId
) external view returns (CoverSegment memory);
function recalculateActiveCoverInAsset(uint coverAsset) external;
function totalActiveCoverInAsset(uint coverAsset) external view returns (uint);
function getGlobalCapacityRatio() external view returns (uint);
function getGlobalRewardsRatio() external view returns (uint);
function getGlobalMinPriceRatio() external pure returns (uint);
function getGlobalCapacityAndPriceRatios() external view returns (
uint _globalCapacityRatio,
uint _globalMinPriceRatio
);
function GLOBAL_MIN_PRICE_RATIO() external view returns (uint);
/* === MUTATIVE FUNCTIONS ==== */
function buyCover(
BuyCoverParams calldata params,
PoolAllocationRequest[] calldata coverChunkRequests
) external payable returns (uint coverId);
function burnStake(
uint coverId,
uint segmentId,
uint amount
) external returns (address coverOwner);
function changeStakingPoolFactoryOperator() external;
function coverNFT() external returns (ICoverNFT);
function stakingNFT() external returns (IStakingNFT);
function stakingPoolFactory() external returns (ICompleteStakingPoolFactory);
/* ========== EVENTS ========== */
event CoverEdited(uint indexed coverId, uint indexed productId, uint indexed segmentId, address buyer, string ipfsMetadata);
// Auth
error OnlyOwnerOrApproved();
// Cover details
error CoverPeriodTooShort();
error CoverPeriodTooLong();
error CoverOutsideOfTheGracePeriod();
error CoverAmountIsZero();
// Products
error ProductNotFound();
error ProductDeprecated();
error UnexpectedProductId();
// Cover and payment assets
error CoverAssetNotSupported();
error InvalidPaymentAsset();
error UnexpectedCoverAsset();
error UnexpectedEthSent();
error EditNotSupported();
// Price & Commission
error PriceExceedsMaxPremiumInAsset();
error CommissionRateTooHigh();
// ETH transfers
error InsufficientEthSent();
error SendingEthToPoolFailed();
error SendingEthToCommissionDestinationFailed();
error ReturningEthRemainderToSenderFailed();
// Misc
error ExpiredCoversCannotBeEdited();
error CoverNotYetExpired(uint coverId);
error InsufficientCoverAmountAllocated();
error UnexpectedPoolId();
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.5.0;
import "@openzeppelin/contracts-v4/token/ERC721/IERC721.sol";
interface ICoverNFT is IERC721 {
function isApprovedOrOwner(address spender, uint tokenId) external returns (bool);
function mint(address to) external returns (uint tokenId);
function changeOperator(address newOperator) external;
function changeNFTDescriptor(address newNFTDescriptor) external;
function totalSupply() external view returns (uint);
function name() external view returns (string memory);
error NotOperator();
error NotMinted();
error WrongFrom();
error InvalidRecipient();
error InvalidNewOperatorAddress();
error InvalidNewNFTDescriptorAddress();
error NotAuthorized();
error UnsafeRecipient();
error AlreadyMinted();
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.5.0;
import "./ICover.sol";
/* io structs */
struct ProductInitializationParams {
uint productId;
uint8 weight;
uint96 initialPrice;
uint96 targetPrice;
}
/* storage structs */
struct Product {
uint16 productType;
address yieldTokenAddress;
// cover assets bitmap. each bit represents whether the asset with
// the index of that bit is enabled as a cover asset for this product
uint32 coverAssets;
uint16 initialPriceRatio;
uint16 capacityReductionRatio;
bool isDeprecated;
bool useFixedPrice;
}
struct ProductType {
uint8 claimMethod;
uint32 gracePeriod;
}
interface ICoverProducts {
/* storage structs */
struct Metadata {
string ipfsHash;
uint timestamp;
}
/* io structs */
struct ProductParam {
string productName;
uint productId;
string ipfsMetadata;
Product product;
uint[] allowedPools;
}
struct ProductTypeParam {
string productTypeName;
uint productTypeId;
string ipfsMetadata;
ProductType productType;
}
/* ========== VIEWS ========== */
function getProductType(uint productTypeId) external view returns (ProductType memory);
function getProductTypeName(uint productTypeId) external view returns (string memory);
function getProductTypeCount() external view returns (uint);
function getProductTypes() external view returns (ProductType[] memory);
function getProduct(uint productId) external view returns (Product memory);
function getProductName(uint productTypeId) external view returns (string memory);
function getProductCount() external view returns (uint);
function getProducts() external view returns (Product[] memory);
// add grace period function?
function getProductWithType(uint productId) external view returns (Product memory, ProductType memory);
function getLatestProductMetadata(uint productId) external view returns (Metadata memory);
function getLatestProductTypeMetadata(uint productTypeId) external view returns (Metadata memory);
function getProductMetadata(uint productId) external view returns (Metadata[] memory);
function getProductTypeMetadata(uint productTypeId) external view returns (Metadata[] memory);
function getAllowedPools(uint productId) external view returns (uint[] memory _allowedPools);
function getAllowedPoolsCount(uint productId) external view returns (uint);
function isPoolAllowed(uint productId, uint poolId) external view returns (bool);
function requirePoolIsAllowed(uint[] calldata productIds, uint poolId) external view;
function getCapacityReductionRatios(uint[] calldata productIds) external view returns (uint[] memory);
function getInitialPrices(uint[] calldata productIds) external view returns (uint[] memory);
function prepareStakingProductsParams(
ProductInitializationParams[] calldata params
) external returns (
ProductInitializationParams[] memory validatedParams
);
/* === MUTATIVE FUNCTIONS ==== */
function setProductTypes(ProductTypeParam[] calldata productTypes) external;
function setProducts(ProductParam[] calldata params) external;
/* ========== EVENTS ========== */
event ProductSet(uint id);
event ProductTypeSet(uint id);
// Products and product types
error ProductNotFound();
error ProductTypeNotFound();
error ProductDeprecated();
error PoolNotAllowedForThisProduct(uint productId);
error StakingPoolDoesNotExist();
error MismatchedArrayLengths();
error MetadataRequired();
// Misc
error UnsupportedCoverAssets();
error InitialPriceRatioBelowGlobalMinPriceRatio();
error InitialPriceRatioAbove100Percent();
error CapacityReductionRatioAbove100Percent();
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.5.0;
interface ISAFURAMaster {
function tokenAddress() external view returns (address);
function owner() external view returns (address);
function emergencyAdmin() external view returns (address);
function masterInitialized() external view returns (bool);
function isInternal(address _add) external view returns (bool);
function isPause() external view returns (bool check);
function isMember(address _add) external view returns (bool);
function checkIsAuthToGoverned(address _add) external view returns (bool);
function getLatestAddress(bytes2 _contractName) external view returns (address payable contractAddress);
function contractAddresses(bytes2 code) external view returns (address payable);
function upgradeMultipleContracts(
bytes2[] calldata _contractCodes,
address payable[] calldata newAddresses
) external;
function removeContracts(bytes2[] calldata contractCodesToRemove) external;
function addNewInternalContracts(
bytes2[] calldata _contractCodes,
address payable[] calldata newAddresses,
uint[] calldata _types
) external;
function updateOwnerParameters(bytes8 code, address payable val) external;
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.5.0;
interface ISAFURAToken {
function burn(uint256 amount) external returns (bool);
function burnFrom(address from, uint256 value) external returns (bool);
function operatorTransfer(address from, uint256 value) external returns (bool);
function mint(address account, uint256 amount) external;
function isLockedForMV(address member) external view returns (uint);
function whiteListed(address member) external view returns (bool);
function addToWhiteList(address _member) external returns (bool);
function removeFromWhiteList(address _member) external returns (bool);
function changeOperator(address _newOperator) external returns (bool);
function lockForMemberVote(address _of, uint _days) external;
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `recipient`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address recipient, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `sender` to `recipient` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.5.0;
import "@openzeppelin/contracts-v4/token/ERC721/IERC721.sol";
interface IStakingNFT is IERC721 {
function isApprovedOrOwner(address spender, uint tokenId) external returns (bool);
function mint(uint poolId, address to) external returns (uint tokenId);
function changeOperator(address newOperator) external;
function changeNFTDescriptor(address newNFTDescriptor) external;
function totalSupply() external returns (uint);
function tokenInfo(uint tokenId) external view returns (uint poolId, address owner);
function stakingPoolOf(uint tokenId) external view returns (uint poolId);
function stakingPoolFactory() external view returns (address);
function name() external view returns (string memory);
error NotOperator();
error NotMinted();
error WrongFrom();
error InvalidRecipient();
error InvalidNewOperatorAddress();
error InvalidNewNFTDescriptorAddress();
error NotAuthorized();
error UnsafeRecipient();
error AlreadyMinted();
error NotStakingPool();
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.5.0;
/* structs for io */
struct AllocationRequest {
uint productId;
uint coverId;
uint allocationId;
uint period;
uint gracePeriod;
bool useFixedPrice;
uint previousStart;
uint previousExpiration;
uint previousRewardsRatio;
uint globalCapacityRatio;
uint capacityReductionRatio;
uint rewardRatio;
uint globalMinPrice;
}
struct BurnStakeParams {
uint allocationId;
uint productId;
uint start;
uint period;
uint deallocationAmount;
}
interface IStakingPool {
/* structs for storage */
// stakers are grouped in tranches based on the timelock expiration
// tranche index is calculated based on the expiration date
// the initial proposal is to have 4 tranches per year (1 tranche per quarter)
struct Tranche {
uint128 stakeShares;
uint128 rewardsShares;
}
struct ExpiredTranche {
uint96 accNxmPerRewardShareAtExpiry;
uint96 stakeAmountAtExpiry; // nxm total supply is 6.7e24 and uint96.max is 7.9e28
uint128 stakeSharesSupplyAtExpiry;
}
struct Deposit {
uint96 lastAccNxmPerRewardShare;
uint96 pendingRewards;
uint128 stakeShares;
uint128 rewardsShares;
}
function initialize(
bool isPrivatePool,
uint initialPoolFee,
uint maxPoolFee,
uint _poolId,
string memory ipfsDescriptionHash
) external;
function processExpirations(bool updateUntilCurrentTimestamp) external;
function requestAllocation(
uint amount,
uint previousPremium,
AllocationRequest calldata request
) external returns (uint premium, uint allocationId);
function burnStake(uint amount, BurnStakeParams calldata params) external;
function depositTo(
uint amount,
uint trancheId,
uint requestTokenId,
address destination
) external returns (uint tokenId);
function withdraw(
uint tokenId,
bool withdrawStake,
bool withdrawRewards,
uint[] memory trancheIds
) external returns (uint withdrawnStake, uint withdrawnRewards);
function isPrivatePool() external view returns (bool);
function isHalted() external view returns (bool);
function manager() external view returns (address);
function getPoolId() external view returns (uint);
function getPoolFee() external view returns (uint);
function getMaxPoolFee() external view returns (uint);
function getActiveStake() external view returns (uint);
function getStakeSharesSupply() external view returns (uint);
function getRewardsSharesSupply() external view returns (uint);
function getRewardPerSecond() external view returns (uint);
function getAccNxmPerRewardsShare() external view returns (uint);
function getLastAccNxmUpdate() external view returns (uint);
function getFirstActiveTrancheId() external view returns (uint);
function getFirstActiveBucketId() external view returns (uint);
function getNextAllocationId() external view returns (uint);
function getDeposit(uint tokenId, uint trancheId) external view returns (
uint lastAccNxmPerRewardShare,
uint pendingRewards,
uint stakeShares,
uint rewardsShares
);
function getTranche(uint trancheId) external view returns (
uint stakeShares,
uint rewardsShares
);
function getExpiredTranche(uint trancheId) external view returns (
uint accNxmPerRewardShareAtExpiry,
uint stakeAmountAtExpiry,
uint stakeShareSupplyAtExpiry
);
function setPoolFee(uint newFee) external;
function setPoolPrivacy(bool isPrivatePool) external;
function getActiveAllocations(
uint productId
) external view returns (uint[] memory trancheAllocations);
function getTrancheCapacities(
uint productId,
uint firstTrancheId,
uint trancheCount,
uint capacityRatio,
uint reductionRatio
) external view returns (uint[] memory trancheCapacities);
/* ========== EVENTS ========== */
event StakeDeposited(address indexed user, uint256 amount, uint256 trancheId, uint256 tokenId);
event DepositExtended(address indexed user, uint256 tokenId, uint256 initialTrancheId, uint256 newTrancheId, uint256 topUpAmount);
event PoolPrivacyChanged(address indexed manager, bool isPrivate);
event PoolFeeChanged(address indexed manager, uint newFee);
event PoolDescriptionSet(string ipfsDescriptionHash);
event Withdraw(address indexed user, uint indexed tokenId, uint tranche, uint amountStakeWithdrawn, uint amountRewardsWithdrawn);
event StakeBurned(uint amount);
event Deallocated(uint productId);
event BucketExpired(uint bucketId);
event TrancheExpired(uint trancheId);
// Auth
error OnlyCoverContract();
error OnlyStakingProductsContract();
error OnlyManager();
error PrivatePool();
error SystemPaused();
error PoolHalted();
// Fees
error PoolFeeExceedsMax();
error MaxPoolFeeAbove100();
// Voting
error NxmIsLockedForGovernanceVote();
error ManagerNxmIsLockedForGovernanceVote();
// Deposit
error InsufficientDepositAmount();
error RewardRatioTooHigh();
// Staking NFTs
error InvalidTokenId();
error NotTokenOwnerOrApproved();
error InvalidStakingPoolForToken();
// Tranche & capacity
error NewTrancheEndsBeforeInitialTranche();
error RequestedTrancheIsNotYetActive();
error RequestedTrancheIsExpired();
error InsufficientCapacity();
// Allocation
error AlreadyDeallocated(uint allocationId);
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.5.0;
interface IStakingPoolFactory {
function stakingPoolCount() external view returns (uint);
function beacon() external view returns (address);
function create(address beacon) external returns (uint poolId, address stakingPoolAddress);
event StakingPoolCreated(uint indexed poolId, address indexed stakingPoolAddress);
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.5.0;
import "./ICoverProducts.sol";
import "./IStakingPool.sol";
interface IStakingProducts {
struct StakedProductParam {
uint productId;
bool recalculateEffectiveWeight;
bool setTargetWeight;
uint8 targetWeight;
bool setTargetPrice;
uint96 targetPrice;
}
struct Weights {
uint32 totalEffectiveWeight;
uint32 totalTargetWeight;
}
struct StakedProduct {
uint16 lastEffectiveWeight;
uint8 targetWeight;
uint96 targetPrice;
uint96 bumpedPrice;
uint32 bumpedPriceUpdateTime;
}
/* ============= PRODUCT FUNCTIONS ============= */
function setProducts(uint poolId, StakedProductParam[] memory params) external;
function getProductTargetWeight(uint poolId, uint productId) external view returns (uint);
function getTotalTargetWeight(uint poolId) external view returns (uint);
function getTotalEffectiveWeight(uint poolId) external view returns (uint);
function getProduct(uint poolId, uint productId) external view returns (
uint lastEffectiveWeight,
uint targetWeight,
uint targetPrice,
uint bumpedPrice,
uint bumpedPriceUpdateTime
);
/* ============= PRICING FUNCTIONS ============= */
function getPremium(
uint poolId,
uint productId,
uint period,
uint coverAmount,
uint initialCapacityUsed,
uint totalCapacity,
uint globalMinPrice,
bool useFixedPrice,
uint nxmPerAllocationUnit,
uint allocationUnitsPerNxm
) external returns (uint premium);
function calculateFixedPricePremium(
uint coverAmount,
uint period,
uint fixedPrice,
uint nxmPerAllocationUnit,
uint targetPriceDenominator
) external pure returns (uint);
function calculatePremium(
StakedProduct memory product,
uint period,
uint coverAmount,
uint initialCapacityUsed,
uint totalCapacity,
uint targetPrice,
uint currentBlockTimestamp,
uint nxmPerAllocationUnit,
uint allocationUnitsPerNxm,
uint targetPriceDenominator
) external pure returns (uint premium, StakedProduct memory);
function calculatePremiumPerYear(
uint basePrice,
uint coverAmount,
uint initialCapacityUsed,
uint totalCapacity,
uint nxmPerAllocationUnit,
uint allocationUnitsPerNxm,
uint targetPriceDenominator
) external pure returns (uint);
// Calculates the premium for a given cover amount starting with the surge point
function calculateSurgePremium(
uint amountOnSurge,
uint totalCapacity,
uint allocationUnitsPerNxm
) external pure returns (uint);
/* ========== STAKING POOL CREATION ========== */
function stakingPool(uint poolId) external view returns (IStakingPool);
function getStakingPoolCount() external view returns (uint);
function createStakingPool(
bool isPrivatePool,
uint initialPoolFee,
uint maxPoolFee,
ProductInitializationParams[] calldata productInitParams,
string calldata ipfsDescriptionHash
) external returns (uint poolId, address stakingPoolAddress);
function changeStakingPoolFactoryOperator(address newOperator) external;
/* ============= EVENTS ============= */
event ProductUpdated(uint productId, uint8 targetWeight, uint96 targetPrice);
/* ============= ERRORS ============= */
// Auth
error OnlyStakingPool();
error OnlyCoverContract();
error OnlyManager();
// Products & weights
error MustSetPriceForNewProducts();
error MustSetWeightForNewProducts();
error TargetPriceTooHigh();
error TargetPriceBelowMin();
error TargetWeightTooHigh();
error MustRecalculateEffectiveWeight();
error TotalTargetWeightExceeded();
error TotalEffectiveWeightExceeded();
// Staking Pool creation
error ProductDoesntExistOrIsDeprecated();
error InvalidProductType();
error TargetPriceBelowGlobalMinPriceRatio();
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity >=0.5.0;
import "./ISAFURAToken.sol";
interface ITokenController {
struct StakingPoolNXMBalances {
uint128 rewards;
uint128 deposits;
}
struct CoverInfo {
uint16 claimCount;
bool hasOpenClaim;
bool hasAcceptedClaim;
uint96 requestedPayoutAmount;
// note: still 128 bits available here, can be used later
}
struct StakingPoolOwnershipOffer {
address proposedManager;
uint96 deadline;
}
function coverInfo(uint id) external view returns (
uint16 claimCount,
bool hasOpenClaim,
bool hasAcceptedClaim,
uint96 requestedPayoutAmount
);
function withdrawCoverNote(
address _of,
uint[] calldata _coverIds,
uint[] calldata _indexes
) external;
function changeOperator(address _newOperator) external;
function operatorTransfer(address _from, address _to, uint _value) external returns (bool);
function burnFrom(address _of, uint amount) external returns (bool);
function addToWhitelist(address _member) external;
function removeFromWhitelist(address _member) external;
function mint(address _member, uint _amount) external;
function lockForMemberVote(address _of, uint _days) external;
function withdrawClaimAssessmentTokens(address[] calldata users) external;
function getLockReasons(address _of) external view returns (bytes32[] memory reasons);
function totalSupply() external view returns (uint);
function totalBalanceOf(address _of) external view returns (uint amount);
function totalBalanceOfWithoutDelegations(address _of) external view returns (uint amount);
function getTokenPrice() external view returns (uint tokenPrice);
function token() external view returns (ISAFURAToken);
function getStakingPoolManager(uint poolId) external view returns (address manager);
function getManagerStakingPools(address manager) external view returns (uint[] memory poolIds);
function isStakingPoolManager(address member) external view returns (bool);
function getStakingPoolOwnershipOffer(uint poolId) external view returns (address proposedManager, uint deadline);
function transferStakingPoolsOwnership(address from, address to) external;
function assignStakingPoolManager(uint poolId, address manager) external;
function createStakingPoolOwnershipOffer(uint poolId, address proposedManager, uint deadline) external;
function acceptStakingPoolOwnershipOffer(uint poolId) external;
function cancelStakingPoolOwnershipOffer(uint poolId) external;
function mintStakingPoolNXMRewards(uint amount, uint poolId) external;
function burnStakingPoolNXMRewards(uint amount, uint poolId) external;
function depositStakedNXM(address from, uint amount, uint poolId) external;
function withdrawNXMStakeAndRewards(address to, uint stakeToWithdraw, uint rewardsToWithdraw, uint poolId) external;
function burnStakedNXM(uint amount, uint poolId) external;
function stakingPoolNXMBalances(uint poolId) external view returns(uint128 rewards, uint128 deposits);
function tokensLocked(address _of, bytes32 _reason) external view returns (uint256 amount);
function getWithdrawableCoverNotes(
address coverOwner
) external view returns (
uint[] memory coverIds,
bytes32[] memory lockReasons,
uint withdrawableAmount
);
function getPendingRewards(address member) external view returns (uint);
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.18;
/**
* @dev Simple library that defines min, max and babylonian sqrt functions
*/
library Math {
function min(uint a, uint b) internal pure returns (uint) {
return a < b ? a : b;
}
function max(uint a, uint b) internal pure returns (uint) {
return a > b ? a : b;
}
function sum(uint[] memory items) internal pure returns (uint) {
uint count = items.length;
uint total;
for (uint i = 0; i < count; i++) {
total += items[i];
}
return total;
}
function divRound(uint a, uint b) internal pure returns (uint) {
return (a + b / 2) / b;
}
function divCeil(uint a, uint b) internal pure returns (uint) {
return (a + b - 1) / b;
}
function roundUp(uint a, uint b) internal pure returns (uint) {
return divCeil(a, b) * b;
}
// babylonian method
function sqrt(uint y) internal pure returns (uint) {
if (y > 3) {
uint z = y;
uint x = y / 2 + 1;
while (x < z) {
z = x;
x = (y / x + x) / 2;
}
return z;
}
if (y != 0) {
return 1;
}
return 0;
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
/**
* @dev Wrappers over Solidity's uintXX casting operators with added overflow
* checks.
*
* Downcasting from uint256 in Solidity does not revert on overflow. This can
* easily result in undesired exploitation or bugs, since developers usually
* assume that overflows raise errors. `SafeCast` restores this intuition by
* reverting the transaction when such an operation overflows.
*
* Using this library instead of the unchecked operations eliminates an entire
* class of bugs, so it's recommended to use it always.
*/
library SafeUintCast {
/**
* @dev Returns the downcasted uint248 from uint256, reverting on
* overflow (when the input is greater than largest uint248).
*
* Counterpart to Solidity's `uint248` operator.
*
* Requirements:
*
* - input must fit into 248 bits
*/
function toUint248(uint256 value) internal pure returns (uint248) {
require(value < 2**248, "SafeCast: value doesn\'t fit in 248 bits");
return uint248(value);
}
/**
* @dev Returns the downcasted uint240 from uint256, reverting on
* overflow (when the input is greater than largest uint240).
*
* Counterpart to Solidity's `uint240` operator.
*
* Requirements:
*
* - input must fit into 240 bits
*/
function toUint240(uint256 value) internal pure returns (uint240) {
require(value < 2**240, "SafeCast: value doesn\'t fit in 240 bits");
return uint240(value);
}
/**
* @dev Returns the downcasted uint232 from uint256, reverting on
* overflow (when the input is greater than largest uint232).
*
* Counterpart to Solidity's `uint232` operator.
*
* Requirements:
*
* - input must fit into 232 bits
*/
function toUint232(uint256 value) internal pure returns (uint232) {
require(value < 2**232, "SafeCast: value doesn\'t fit in 232 bits");
return uint232(value);
}
/**
* @dev Returns the downcasted uint224 from uint256, reverting on
* overflow (when the input is greater than largest uint224).
*
* Counterpart to Solidity's `uint224` operator.
*
* Requirements:
*
* - input must fit into 224 bits
*/
function toUint224(uint256 value) internal pure returns (uint224) {
require(value < 2**224, "SafeCast: value doesn\'t fit in 224 bits");
return uint224(value);
}
/**
* @dev Returns the downcasted uint216 from uint256, reverting on
* overflow (when the input is greater than largest uint216).
*
* Counterpart to Solidity's `uint216` operator.
*
* Requirements:
*
* - input must fit into 216 bits
*/
function toUint216(uint256 value) internal pure returns (uint216) {
require(value < 2**216, "SafeCast: value doesn\'t fit in 216 bits");
return uint216(value);
}
/**
* @dev Returns the downcasted uint208 from uint256, reverting on
* overflow (when the input is greater than largest uint208).
*
* Counterpart to Solidity's `uint208` operator.
*
* Requirements:
*
* - input must fit into 208 bits
*/
function toUint208(uint256 value) internal pure returns (uint208) {
require(value < 2**208, "SafeCast: value doesn\'t fit in 208 bits");
return uint208(value);
}
/**
* @dev Returns the downcasted uint200 from uint256, reverting on
* overflow (when the input is greater than largest uint200).
*
* Counterpart to Solidity's `uint200` operator.
*
* Requirements:
*
* - input must fit into 200 bits
*/
function toUint200(uint256 value) internal pure returns (uint200) {
require(value < 2**200, "SafeCast: value doesn\'t fit in 200 bits");
return uint200(value);
}
/**
* @dev Returns the downcasted uint192 from uint256, reverting on
* overflow (when the input is greater than largest uint192).
*
* Counterpart to Solidity's `uint192` operator.
*
* Requirements:
*
* - input must fit into 192 bits
*/
function toUint192(uint256 value) internal pure returns (uint192) {
require(value < 2**192, "SafeCast: value doesn\'t fit in 192 bits");
return uint192(value);
}
/**
* @dev Returns the downcasted uint184 from uint256, reverting on
* overflow (when the input is greater than largest uint184).
*
* Counterpart to Solidity's `uint184` operator.
*
* Requirements:
*
* - input must fit into 184 bits
*/
function toUint184(uint256 value) internal pure returns (uint184) {
require(value < 2**184, "SafeCast: value doesn\'t fit in 184 bits");
return uint184(value);
}
/**
* @dev Returns the downcasted uint176 from uint256, reverting on
* overflow (when the input is greater than largest uint176).
*
* Counterpart to Solidity's `uint176` operator.
*
* Requirements:
*
* - input must fit into 176 bits
*/
function toUint176(uint256 value) internal pure returns (uint176) {
require(value < 2**176, "SafeCast: value doesn\'t fit in 176 bits");
return uint176(value);
}
/**
* @dev Returns the downcasted uint168 from uint256, reverting on
* overflow (when the input is greater than largest uint168).
*
* Counterpart to Solidity's `uint168` operator.
*
* Requirements:
*
* - input must fit into 168 bits
*/
function toUint168(uint256 value) internal pure returns (uint168) {
require(value < 2**168, "SafeCast: value doesn\'t fit in 168 bits");
return uint168(value);
}
/**
* @dev Returns the downcasted uint160 from uint256, reverting on
* overflow (when the input is greater than largest uint160).
*
* Counterpart to Solidity's `uint160` operator.
*
* Requirements:
*
* - input must fit into 160 bits
*/
function toUint160(uint256 value) internal pure returns (uint160) {
require(value < 2**160, "SafeCast: value doesn\'t fit in 160 bits");
return uint160(value);
}
/**
* @dev Returns the downcasted uint152 from uint256, reverting on
* overflow (when the input is greater than largest uint152).
*
* Counterpart to Solidity's `uint152` operator.
*
* Requirements:
*
* - input must fit into 152 bits
*/
function toUint152(uint256 value) internal pure returns (uint152) {
require(value < 2**152, "SafeCast: value doesn\'t fit in 152 bits");
return uint152(value);
}
/**
* @dev Returns the downcasted uint144 from uint256, reverting on
* overflow (when the input is greater than largest uint144).
*
* Counterpart to Solidity's `uint144` operator.
*
* Requirements:
*
* - input must fit into 144 bits
*/
function toUint144(uint256 value) internal pure returns (uint144) {
require(value < 2**144, "SafeCast: value doesn\'t fit in 144 bits");
return uint144(value);
}
/**
* @dev Returns the downcasted uint136 from uint256, reverting on
* overflow (when the input is greater than largest uint136).
*
* Counterpart to Solidity's `uint136` operator.
*
* Requirements:
*
* - input must fit into 136 bits
*/
function toUint136(uint256 value) internal pure returns (uint136) {
require(value < 2**136, "SafeCast: value doesn\'t fit in 136 bits");
return uint136(value);
}
/**
* @dev Returns the downcasted uint128 from uint256, reverting on
* overflow (when the input is greater than largest uint128).
*
* Counterpart to Solidity's `uint128` operator.
*
* Requirements:
*
* - input must fit into 128 bits
*/
function toUint128(uint256 value) internal pure returns (uint128) {
require(value < 2**128, "SafeCast: value doesn\'t fit in 128 bits");
return uint128(value);
}
/**
* @dev Returns the downcasted uint120 from uint256, reverting on
* overflow (when the input is greater than largest uint120).
*
* Counterpart to Solidity's `uint120` operator.
*
* Requirements:
*
* - input must fit into 120 bits
*/
function toUint120(uint256 value) internal pure returns (uint120) {
require(value < 2**120, "SafeCast: value doesn\'t fit in 120 bits");
return uint120(value);
}
/**
* @dev Returns the downcasted uint112 from uint256, reverting on
* overflow (when the input is greater than largest uint112).
*
* Counterpart to Solidity's `uint112` operator.
*
* Requirements:
*
* - input must fit into 112 bits
*/
function toUint112(uint256 value) internal pure returns (uint112) {
require(value < 2**112, "SafeCast: value doesn\'t fit in 112 bits");
return uint112(value);
}
/**
* @dev Returns the downcasted uint104 from uint256, reverting on
* overflow (when the input is greater than largest uint104).
*
* Counterpart to Solidity's `uint104` operator.
*
* Requirements:
*
* - input must fit into 104 bits
*/
function toUint104(uint256 value) internal pure returns (uint104) {
require(value < 2**104, "SafeCast: value doesn\'t fit in 104 bits");
return uint104(value);
}
/**
* @dev Returns the downcasted uint96 from uint256, reverting on
* overflow (when the input is greater than largest uint96).
*
* Counterpart to Solidity's `uint104` operator.
*
* Requirements:
*
* - input must fit into 96 bits
*/
function toUint96(uint256 value) internal pure returns (uint96) {
require(value < 2**96, "SafeCast: value doesn\'t fit in 96 bits");
return uint96(value);
}
/**
* @dev Returns the downcasted uint88 from uint256, reverting on
* overflow (when the input is greater than largest uint88).
*
* Counterpart to Solidity's `uint104` operator.
*
* Requirements:
*
* - input must fit into 88 bits
*/
function toUint88(uint256 value) internal pure returns (uint88) {
require(value < 2**88, "SafeCast: value doesn\'t fit in 88 bits");
return uint88(value);
}
/**
* @dev Returns the downcasted uint80 from uint256, reverting on
* overflow (when the input is greater than largest uint80).
*
* Counterpart to Solidity's `uint104` operator.
*
* Requirements:
*
* - input must fit into 80 bits
*/
function toUint80(uint256 value) internal pure returns (uint80) {
require(value < 2**80, "SafeCast: value doesn\'t fit in 80 bits");
return uint80(value);
}
/**
* @dev Returns the downcasted uint64 from uint256, reverting on
* overflow (when the input is greater than largest uint64).
*
* Counterpart to Solidity's `uint64` operator.
*
* Requirements:
*
* - input must fit into 64 bits
*/
function toUint64(uint256 value) internal pure returns (uint64) {
require(value < 2**64, "SafeCast: value doesn\'t fit in 64 bits");
return uint64(value);
}
/**
* @dev Returns the downcasted uint56 from uint256, reverting on
* overflow (when the input is greater than largest uint56).
*
* Counterpart to Solidity's `uint56` operator.
*
* Requirements:
*
* - input must fit into 56 bits
*/
function toUint56(uint256 value) internal pure returns (uint56) {
require(value < 2**56, "SafeCast: value doesn\'t fit in 56 bits");
return uint56(value);
}
/**
* @dev Returns the downcasted uint48 from uint256, reverting on
* overflow (when the input is greater than largest uint48).
*
* Counterpart to Solidity's `uint48` operator.
*
* Requirements:
*
* - input must fit into 48 bits
*/
function toUint48(uint256 value) internal pure returns (uint48) {
require(value < 2**48, "SafeCast: value doesn\'t fit in 48 bits");
return uint48(value);
}
/**
* @dev Returns the downcasted uint40 from uint256, reverting on
* overflow (when the input is greater than largest uint40).
*
* Counterpart to Solidity's `uint40` operator.
*
* Requirements:
*
* - input must fit into 40 bits
*/
function toUint40(uint256 value) internal pure returns (uint40) {
require(value < 2**40, "SafeCast: value doesn\'t fit in 40 bits");
return uint40(value);
}
/**
* @dev Returns the downcasted uint32 from uint256, reverting on
* overflow (when the input is greater than largest uint32).
*
* Counterpart to Solidity's `uint32` operator.
*
* Requirements:
*
* - input must fit into 32 bits
*/
function toUint32(uint256 value) internal pure returns (uint32) {
require(value < 2**32, "SafeCast: value doesn\'t fit in 32 bits");
return uint32(value);
}
/**
* @dev Returns the downcasted uint24 from uint256, reverting on
* overflow (when the input is greater than largest uint24).
*
* Counterpart to Solidity's `uint24` operator.
*
* Requirements:
*
* - input must fit into 24 bits
*/
function toUint24(uint256 value) internal pure returns (uint24) {
require(value < 2**24, "SafeCast: value doesn\'t fit in 24 bits");
return uint24(value);
}
/**
* @dev Returns the downcasted uint16 from uint256, reverting on
* overflow (when the input is greater than largest uint16).
*
* Counterpart to Solidity's `uint16` operator.
*
* Requirements:
*
* - input must fit into 16 bits
*/
function toUint16(uint256 value) internal pure returns (uint16) {
require(value < 2**16, "SafeCast: value doesn\'t fit in 16 bits");
return uint16(value);
}
/**
* @dev Returns the downcasted uint8 from uint256, reverting on
* overflow (when the input is greater than largest uint8).
*
* Counterpart to Solidity's `uint8` operator.
*
* Requirements:
*
* - input must fit into 8 bits.
*/
function toUint8(uint256 value) internal pure returns (uint8) {
require(value < 2**8, "SafeCast: value doesn\'t fit in 8 bits");
return uint8(value);
}
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.18;
/**
* @dev Simple library that defines basic math functions that allow overflow
*/
library UncheckedMath {
function uncheckedAdd(uint a, uint b) internal pure returns (uint) {
unchecked { return a + b; }
}
function uncheckedSub(uint a, uint b) internal pure returns (uint) {
unchecked { return a - b; }
}
function uncheckedMul(uint a, uint b) internal pure returns (uint) {
unchecked { return a * b; }
}
function uncheckedDiv(uint a, uint b) internal pure returns (uint) {
unchecked { return a / b; }
}
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.18;
import "../../abstract/Multicall.sol";
import "../../interfaces/IStakingPool.sol";
import "../../interfaces/IStakingNFT.sol";
import "../../interfaces/ITokenController.sol";
import "../../interfaces/ISAFURAMaster.sol";
import "../../interfaces/ISAFURAToken.sol";
import "../../interfaces/IStakingProducts.sol";
import "../../libraries/Math.sol";
import "../../libraries/UncheckedMath.sol";
import "../../libraries/SafeUintCast.sol";
import "./StakingTypesLib.sol";
// total stake = active stake + expired stake
// total capacity = active stake * global capacity factor
// total product capacity = total capacity * capacity reduction factor * product weight
// total product capacity = allocated product capacity + available product capacity
// on cover buys we allocate the available product capacity
// on cover expiration we deallocate the capacity and it becomes available again
contract StakingPool is IStakingPool, Multicall {
using StakingTypesLib for TrancheAllocationGroup;
using StakingTypesLib for TrancheGroupBucket;
using SafeUintCast for uint;
using UncheckedMath for uint;
/* storage */
// slot 1
// supply of pool stake shares used by tranches
uint128 internal stakeSharesSupply;
// supply of pool rewards shares used by tranches
uint128 internal rewardsSharesSupply;
// slot 2
// accumulated rewarded nxm per reward share
uint96 internal accNxmPerRewardsShare;
// currently active staked nxm amount
uint96 internal activeStake;
uint32 internal firstActiveTrancheId;
uint32 internal firstActiveBucketId;
// slot 3
// timestamp when accNxmPerRewardsShare was last updated
uint32 internal lastAccNxmUpdate;
// current nxm reward per second for the entire pool
// applies to active stake only and does not need update on deposits
uint96 internal rewardPerSecond;
uint40 internal poolId;
uint24 internal lastAllocationId;
bool public override isPrivatePool;
bool public override isHalted;
uint8 internal poolFee;
uint8 internal maxPoolFee;
// 32 bytes left in slot 3
// tranche id => tranche data
mapping(uint => Tranche) internal tranches;
// tranche id => expired tranche data
mapping(uint => ExpiredTranche) internal expiredTranches;
// reward bucket id => RewardBucket
mapping(uint => uint) public rewardPerSecondCut;
// product id => tranche group id => active allocations for a tranche group
mapping(uint => mapping(uint => TrancheAllocationGroup)) public trancheAllocationGroups;
// product id => bucket id => bucket tranche group id => tranche group's expiring cover amounts
mapping(uint => mapping(uint => mapping(uint => TrancheGroupBucket))) public expiringCoverBuckets;
// cover id => per tranche cover amounts (8 32-bit values, one per tranche, packed in a slot)
// starts with the first active tranche at the time of cover buy
mapping(uint => uint) public coverTrancheAllocations;
// token id => tranche id => deposit data
mapping(uint => mapping(uint => Deposit)) public deposits;
/* immutables */
IStakingNFT public immutable stakingNFT;
ISAFURAToken public immutable nxm;
ITokenController public immutable tokenController;
address public immutable coverContract;
ISAFURAMaster public immutable masterContract;
IStakingProducts public immutable stakingProducts;
/* constants */
// 7 * 13 = 91
uint public constant BUCKET_DURATION = 28 days;
uint public constant TRANCHE_DURATION = 91 days;
uint public constant MAX_ACTIVE_TRANCHES = 8; // 7 whole quarters + 1 partial quarter
uint public constant COVER_TRANCHE_GROUP_SIZE = 5;
uint public constant BUCKET_TRANCHE_GROUP_SIZE = 8;
uint public constant REWARD_BONUS_PER_TRANCHE_RATIO = 10_00; // 10.00%
uint public constant REWARD_BONUS_PER_TRANCHE_DENOMINATOR = 100_00;
uint public constant WEIGHT_DENOMINATOR = 100;
uint public constant REWARDS_DENOMINATOR = 100_00;
uint public constant POOL_FEE_DENOMINATOR = 100;
// denominators for cover contract parameters
uint public constant GLOBAL_CAPACITY_DENOMINATOR = 100_00;
uint public constant CAPACITY_REDUCTION_DENOMINATOR = 100_00;
// +2% for every 1%, ie +200% for 100%
// 1 nxm = 1e18
uint internal constant ONE_NXM = 1 ether;
// internally we store capacity using 2 decimals
// 1 nxm of capacity is stored as 100
uint public constant ALLOCATION_UNITS_PER_NXM = 100;
// given capacities have 2 decimals
// smallest unit we can allocate is 1e18 / 100 = 1e16 = 0.01 NXM
uint public constant NXM_PER_ALLOCATION_UNIT = ONE_NXM / ALLOCATION_UNITS_PER_NXM;
modifier onlyCoverContract {
if (msg.sender != coverContract) {
revert OnlyCoverContract();
}
_;
}
modifier onlyManager {
if (msg.sender != manager()) {
revert OnlyManager();
}
_;
}
modifier whenNotPaused {
if (masterContract.isPause()) {
revert SystemPaused();
}
_;
}
modifier whenNotHalted {
if (isHalted) {
revert PoolHalted();
}
_;
}
constructor (
address _stakingNFT,
address _token,
address _coverContract,
address _tokenController,
address _master,
address _stakingProducts
) {
stakingNFT = IStakingNFT(_stakingNFT);
nxm = ISAFURAToken(_token);
coverContract = _coverContract;
tokenController = ITokenController(_tokenController);
masterContract = ISAFURAMaster(_master);
stakingProducts = IStakingProducts(_stakingProducts);
}
function initialize(
bool _isPrivatePool,
uint _initialPoolFee,
uint _maxPoolFee,
uint _poolId,
string calldata ipfsDescriptionHash
) external {
if (msg.sender != address(stakingProducts)) {
revert OnlyStakingProductsContract();
}
if (_initialPoolFee > _maxPoolFee) {
revert PoolFeeExceedsMax();
}
if (_maxPoolFee >= 100) {
revert MaxPoolFeeAbove100();
}
isPrivatePool = _isPrivatePool;
poolFee = uint8(_initialPoolFee);
maxPoolFee = uint8(_maxPoolFee);
poolId = _poolId.toUint40();
emit PoolDescriptionSet(ipfsDescriptionHash);
}
// updateUntilCurrentTimestamp forces rewards update until current timestamp not just until
// bucket/tranche expiry timestamps. Must be true when changing shares or reward per second.
function processExpirations(bool updateUntilCurrentTimestamp) public {
uint _firstActiveBucketId = firstActiveBucketId;
uint _firstActiveTrancheId = firstActiveTrancheId;
uint currentBucketId = block.timestamp / BUCKET_DURATION;
uint currentTrancheId = block.timestamp / TRANCHE_DURATION;
// if the pool is new
if (_firstActiveBucketId == 0) {
_firstActiveBucketId = currentBucketId;
_firstActiveTrancheId = currentTrancheId;
}
// if a force update was not requested
if (!updateUntilCurrentTimestamp) {
bool canExpireBuckets = _firstActiveBucketId < currentBucketId;
bool canExpireTranches = _firstActiveTrancheId < currentTrancheId;
// and if there's nothing to expire
if (!canExpireBuckets && !canExpireTranches) {
// we can exit
return;
}
}
// SLOAD
uint _activeStake = activeStake;
uint _rewardPerSecond = rewardPerSecond;
uint _stakeSharesSupply = stakeSharesSupply;
uint _rewardsSharesSupply = rewardsSharesSupply;
uint _accNxmPerRewardsShare = accNxmPerRewardsShare;
uint _lastAccNxmUpdate = lastAccNxmUpdate;
// exit early if we already updated in the current block
if (_lastAccNxmUpdate == block.timestamp) {
return;
}
while (_firstActiveBucketId < currentBucketId || _firstActiveTrancheId < currentTrancheId) {
// what expires first, the bucket or the tranche?
bool bucketExpiresFirst;
{
uint nextBucketStart = (_firstActiveBucketId + 1) * BUCKET_DURATION;
uint nextTrancheStart = (_firstActiveTrancheId + 1) * TRANCHE_DURATION;
bucketExpiresFirst = nextBucketStart <= nextTrancheStart;
}
if (bucketExpiresFirst) {
// expire a bucket
// each bucket contains a reward reduction - we subtract it when the bucket *starts*!
++_firstActiveBucketId;
uint bucketStartTime = _firstActiveBucketId * BUCKET_DURATION;
uint elapsed = bucketStartTime - _lastAccNxmUpdate;
uint newAccNxmPerRewardsShare = _rewardsSharesSupply != 0
? elapsed * _rewardPerSecond * ONE_NXM / _rewardsSharesSupply
: 0;
_accNxmPerRewardsShare = _accNxmPerRewardsShare.uncheckedAdd(newAccNxmPerRewardsShare);
_rewardPerSecond -= rewardPerSecondCut[_firstActiveBucketId];
_lastAccNxmUpdate = bucketStartTime;
emit BucketExpired(_firstActiveBucketId - 1);
continue;
}
// expire a tranche
// each tranche contains shares - we expire them when the tranche *ends*
// TODO: check if we have to expire the tranche
{
uint trancheEndTime = (_firstActiveTrancheId + 1) * TRANCHE_DURATION;
uint elapsed = trancheEndTime - _lastAccNxmUpdate;
uint newAccNxmPerRewardsShare = _rewardsSharesSupply != 0
? elapsed * _rewardPerSecond * ONE_NXM / _rewardsSharesSupply
: 0;
_accNxmPerRewardsShare = _accNxmPerRewardsShare.uncheckedAdd(newAccNxmPerRewardsShare);
_lastAccNxmUpdate = trancheEndTime;
// SSTORE
expiredTranches[_firstActiveTrancheId] = ExpiredTranche(
_accNxmPerRewardsShare.toUint96(), // accNxmPerRewardShareAtExpiry
_activeStake.toUint96(), // stakeAmountAtExpiry
_stakeSharesSupply.toUint128() // stakeSharesSupplyAtExpiry
);
// SLOAD and then SSTORE zero to get the gas refund
Tranche memory expiringTranche = tranches[_firstActiveTrancheId];
delete tranches[_firstActiveTrancheId];
// the tranche is expired now so we decrease the stake and the shares supply
uint expiredStake = _stakeSharesSupply != 0
? (_activeStake * expiringTranche.stakeShares) / _stakeSharesSupply
: 0;
_activeStake -= expiredStake;
_stakeSharesSupply -= expiringTranche.stakeShares;
_rewardsSharesSupply -= expiringTranche.rewardsShares;
emit TrancheExpired(_firstActiveTrancheId);
// advance to the next tranche
_firstActiveTrancheId++;
}
// end while
}
if (updateUntilCurrentTimestamp) {
uint elapsed = block.timestamp - _lastAccNxmUpdate;
uint newAccNxmPerRewardsShare = _rewardsSharesSupply != 0
? elapsed * _rewardPerSecond * ONE_NXM / _rewardsSharesSupply
: 0;
_accNxmPerRewardsShare = _accNxmPerRewardsShare.uncheckedAdd(newAccNxmPerRewardsShare);
_lastAccNxmUpdate = block.timestamp;
}
firstActiveTrancheId = _firstActiveTrancheId.toUint32();
firstActiveBucketId = _firstActiveBucketId.toUint32();
activeStake = _activeStake.toUint96();
rewardPerSecond = _rewardPerSecond.toUint96();
accNxmPerRewardsShare = _accNxmPerRewardsShare.toUint96();
lastAccNxmUpdate = _lastAccNxmUpdate.toUint32();
stakeSharesSupply = _stakeSharesSupply.toUint128();
rewardsSharesSupply = _rewardsSharesSupply.toUint128();
}
function depositTo(
uint amount,
uint trancheId,
uint requestTokenId,
address destination
) public whenNotPaused whenNotHalted returns (uint tokenId) {
if (isPrivatePool && msg.sender != manager()) {
revert PrivatePool();
}
if (block.timestamp <= nxm.isLockedForMV(msg.sender) && msg.sender != manager()) {
revert NxmIsLockedForGovernanceVote();
}
{
uint _firstActiveTrancheId = block.timestamp / TRANCHE_DURATION;
uint maxTranche = _firstActiveTrancheId + MAX_ACTIVE_TRANCHES - 1;
if (amount == 0) {
revert InsufficientDepositAmount();
}
if (trancheId > maxTranche) {
revert RequestedTrancheIsNotYetActive();
}
if (trancheId < _firstActiveTrancheId) {
revert RequestedTrancheIsExpired();
}
// if the pool has no previous deposits
if (firstActiveTrancheId == 0) {
firstActiveTrancheId = _firstActiveTrancheId.toUint32();
firstActiveBucketId = (block.timestamp / BUCKET_DURATION).toUint32();
lastAccNxmUpdate = block.timestamp.toUint32();
} else {
processExpirations(true);
}
}
// storage reads
uint _activeStake = activeStake;
uint _stakeSharesSupply = stakeSharesSupply;
uint _rewardsSharesSupply = rewardsSharesSupply;
uint _accNxmPerRewardsShare = accNxmPerRewardsShare;
uint totalAmount;
// deposit to token id = 0 is not allowed
// we treat it as a flag to create a new token
if (requestTokenId == 0) {
address to = destination == address(0) ? msg.sender : destination;
tokenId = stakingNFT.mint(poolId, to);
} else {
// validate token id exists and belongs to this pool
// stakingPoolOf() reverts for non-existent tokens
if (stakingNFT.stakingPoolOf(requestTokenId) != poolId) {
revert InvalidStakingPoolForToken();
}
// validate only the token owner or an approved address can deposit
if (!stakingNFT.isApprovedOrOwner(msg.sender, requestTokenId)) {
revert NotTokenOwnerOrApproved();
}
tokenId = requestTokenId;
}
uint newStakeShares = _stakeSharesSupply == 0
? Math.sqrt(amount)
: _stakeSharesSupply * amount / _activeStake;
uint newRewardsShares;
// update deposit and pending reward
{
// conditional read
Deposit memory deposit = requestTokenId == 0
? Deposit(0, 0, 0, 0)
: deposits[tokenId][trancheId];
newRewardsShares = calculateNewRewardShares(
deposit.stakeShares, // initialStakeShares
newStakeShares, // newStakeShares
trancheId, // initialTrancheId
trancheId, // newTrancheId, the same as initialTrancheId in this case
block.timestamp
);
// if we're increasing an existing deposit
if (deposit.rewardsShares != 0) {
uint newEarningsPerShare = _accNxmPerRewardsShare.uncheckedSub(deposit.lastAccNxmPerRewardShare);
deposit.pendingRewards += (newEarningsPerShare * deposit.rewardsShares / ONE_NXM).toUint96();
}
deposit.stakeShares += newStakeShares.toUint128();
deposit.rewardsShares += newRewardsShares.toUint128();
deposit.lastAccNxmPerRewardShare = _accNxmPerRewardsShare.toUint96();
// store
deposits[tokenId][trancheId] = deposit;
}
// update pool manager's reward shares
{
Deposit memory feeDeposit = deposits[0][trancheId];
{
// create fee deposit reward shares
uint newFeeRewardShares = newRewardsShares * poolFee / (POOL_FEE_DENOMINATOR - poolFee);
newRewardsShares += newFeeRewardShares;
// calculate rewards until now
uint newRewardPerShare = _accNxmPerRewardsShare.uncheckedSub(feeDeposit.lastAccNxmPerRewardShare);
feeDeposit.pendingRewards += (newRewardPerShare * feeDeposit.rewardsShares / ONE_NXM).toUint96();
feeDeposit.lastAccNxmPerRewardShare = _accNxmPerRewardsShare.toUint96();
feeDeposit.rewardsShares += newFeeRewardShares.toUint128();
}
deposits[0][trancheId] = feeDeposit;
}
// update tranche
{
Tranche memory tranche = tranches[trancheId];
tranche.stakeShares += newStakeShares.toUint128();
tranche.rewardsShares += newRewardsShares.toUint128();
tranches[trancheId] = tranche;
}
totalAmount += amount;
_activeStake += amount;
_stakeSharesSupply += newStakeShares;
_rewardsSharesSupply += newRewardsShares;
// transfer nxm from the staker and update the pool deposit balance
tokenController.depositStakedNXM(msg.sender, totalAmount, poolId);
// update globals
activeStake = _activeStake.toUint96();
stakeSharesSupply = _stakeSharesSupply.toUint128();
rewardsSharesSupply = _rewardsSharesSupply.toUint128();
emit StakeDeposited(msg.sender, amount, trancheId, tokenId);
}
function getTimeLeftOfTranche(uint trancheId, uint blockTimestamp) internal pure returns (uint) {
uint endDate = (trancheId + 1) * TRANCHE_DURATION;
return endDate > blockTimestamp ? endDate - blockTimestamp : 0;
}
/// Calculates the amount of new reward shares based on the initial and new stake shares
///
/// @param initialStakeShares Amount of stake shares the deposit is already entitled to
/// @param stakeSharesIncrease Amount of additional stake shares the deposit will be entitled to
/// @param initialTrancheId The id of the initial tranche that defines the deposit period
/// @param newTrancheId The new id of the tranche that will define the deposit period
/// @param blockTimestamp The timestamp of the block when the new shares are recalculated
function calculateNewRewardShares(
uint initialStakeShares,
uint stakeSharesIncrease,
uint initialTrancheId,
uint newTrancheId,
uint blockTimestamp
) public pure returns (uint) {
uint timeLeftOfInitialTranche = getTimeLeftOfTranche(initialTrancheId, blockTimestamp);
uint timeLeftOfNewTranche = getTimeLeftOfTranche(newTrancheId, blockTimestamp);
// the bonus is based on the the time left and the total amount of stake shares (initial + new)
uint newBonusShares = (initialStakeShares + stakeSharesIncrease)
* REWARD_BONUS_PER_TRANCHE_RATIO
* timeLeftOfNewTranche
/ TRANCHE_DURATION
/ REWARD_BONUS_PER_TRANCHE_DENOMINATOR;
// for existing deposits, the previous bonus is deducted from the final amount
uint previousBonusSharesDeduction = initialStakeShares
* REWARD_BONUS_PER_TRANCHE_RATIO
* timeLeftOfInitialTranche
/ TRANCHE_DURATION
/ REWARD_BONUS_PER_TRANCHE_DENOMINATOR;
return stakeSharesIncrease + newBonusShares - previousBonusSharesDeduction;
}
function withdraw(
uint tokenId,
bool withdrawStake,
bool withdrawRewards,
uint[] memory trancheIds
) public whenNotPaused returns (uint withdrawnStake, uint withdrawnRewards) {
uint managerLockedInGovernanceUntil = nxm.isLockedForMV(manager());
// pass false as it does not modify the share supply nor the reward per second
processExpirations(true);
uint _accNxmPerRewardsShare = accNxmPerRewardsShare;
uint _firstActiveTrancheId = block.timestamp / TRANCHE_DURATION;
uint trancheCount = trancheIds.length;
for (uint j = 0; j < trancheCount; j++) {
uint trancheId = trancheIds[j];
Deposit memory deposit = deposits[tokenId][trancheId];
{
uint trancheRewardsToWithdraw;
uint trancheStakeToWithdraw;
// can withdraw stake only if the tranche is expired
if (withdrawStake && trancheId < _firstActiveTrancheId) {
// Deposit withdrawals are not permitted while the manager is locked in governance to
// prevent double voting.
if (managerLockedInGovernanceUntil > block.timestamp) {
revert ManagerNxmIsLockedForGovernanceVote();
}
// calculate the amount of nxm for this deposit
uint stake = expiredTranches[trancheId].stakeAmountAtExpiry;
uint _stakeSharesSupply = expiredTranches[trancheId].stakeSharesSupplyAtExpiry;
trancheStakeToWithdraw = stake * deposit.stakeShares / _stakeSharesSupply;
withdrawnStake += trancheStakeToWithdraw;
// mark as withdrawn
deposit.stakeShares = 0;
}
if (withdrawRewards) {
// if the tranche is expired, use the accumulator value saved at expiration time
uint accNxmPerRewardShareToUse = trancheId < _firstActiveTrancheId
? expiredTranches[trancheId].accNxmPerRewardShareAtExpiry
: _accNxmPerRewardsShare;
// calculate reward since checkpoint
uint newRewardPerShare = accNxmPerRewardShareToUse.uncheckedSub(deposit.lastAccNxmPerRewardShare);
trancheRewardsToWithdraw = newRewardPerShare * deposit.rewardsShares / ONE_NXM + deposit.pendingRewards;
withdrawnRewards += trancheRewardsToWithdraw;
// save checkpoint
deposit.lastAccNxmPerRewardShare = accNxmPerRewardShareToUse.toUint96();
deposit.pendingRewards = 0;
}
emit Withdraw(msg.sender, tokenId, trancheId, trancheStakeToWithdraw, trancheRewardsToWithdraw);
}
deposits[tokenId][trancheId] = deposit;
}
address destination = tokenId == 0
? manager()
: stakingNFT.ownerOf(tokenId);
tokenController.withdrawNXMStakeAndRewards(
destination,
withdrawnStake,
withdrawnRewards,
poolId
);
return (withdrawnStake, withdrawnRewards);
}
function requestAllocation(
uint amount,
uint previousPremium,
AllocationRequest calldata request
) external onlyCoverContract returns (uint premium, uint allocationId) {
// passing true because we change the reward per second
processExpirations(true);
// prevent allocation requests (edits and forced expirations) for expired covers
if (request.allocationId != 0) {
uint expirationBucketId = Math.divCeil(request.previousExpiration, BUCKET_DURATION);
if (coverTrancheAllocations[request.allocationId] == 0 || firstActiveBucketId >= expirationBucketId) {
revert AlreadyDeallocated(request.allocationId);
}
}
uint[] memory trancheAllocations = request.allocationId == 0
? getActiveAllocations(request.productId)
: getActiveAllocationsWithoutCover(
request.productId,
request.allocationId,
request.previousStart,
request.previousExpiration
);
// we are only deallocating
// rewards streaming is left as is
if (amount == 0) {
// store deallocated amount
updateStoredAllocations(
request.productId,
block.timestamp / TRANCHE_DURATION, // firstActiveTrancheId
trancheAllocations
);
// update coverTrancheAllocations when deallocating so we can track deallocation
delete coverTrancheAllocations[request.allocationId];
emit Deallocated(request.allocationId);
return (0, 0);
}
uint coverAllocationAmount;
uint initialCapacityUsed;
uint totalCapacity;
(
coverAllocationAmount,
initialCapacityUsed,
totalCapacity,
allocationId
) = allocate(amount, request, trancheAllocations);
// the returned premium value has 18 decimals
premium = stakingProducts.getPremium(
poolId,
request.productId,
request.period,
coverAllocationAmount,
initialCapacityUsed,
totalCapacity,
request.globalMinPrice,
request.useFixedPrice,
NXM_PER_ALLOCATION_UNIT,
ALLOCATION_UNITS_PER_NXM
);
// add new rewards
{
if (request.rewardRatio > REWARDS_DENOMINATOR) {
revert RewardRatioTooHigh();
}
uint expirationBucket = Math.divCeil(block.timestamp + request.period, BUCKET_DURATION);
uint rewardStreamPeriod = expirationBucket * BUCKET_DURATION - block.timestamp;
uint _rewardPerSecond = (premium * request.rewardRatio / REWARDS_DENOMINATOR) / rewardStreamPeriod;
// store
rewardPerSecondCut[expirationBucket] += _rewardPerSecond;
rewardPerSecond += _rewardPerSecond.toUint96();
uint rewardsToMint = _rewardPerSecond * rewardStreamPeriod;
tokenController.mintStakingPoolNXMRewards(rewardsToMint, poolId);
}
// remove previous rewards
if (previousPremium > 0) {
uint prevRewards = previousPremium * request.previousRewardsRatio / REWARDS_DENOMINATOR;
uint prevExpirationBucket = Math.divCeil(request.previousExpiration, BUCKET_DURATION);
uint rewardStreamPeriod = prevExpirationBucket * BUCKET_DURATION - request.previousStart;
uint prevRewardsPerSecond = prevRewards / rewardStreamPeriod;
// store
rewardPerSecondCut[prevExpirationBucket] -= prevRewardsPerSecond;
rewardPerSecond -= prevRewardsPerSecond.toUint96();
// prevRewardsPerSecond * rewardStreamPeriodLeft
uint rewardsToBurn = prevRewardsPerSecond * (prevExpirationBucket * BUCKET_DURATION - block.timestamp);
tokenController.burnStakingPoolNXMRewards(rewardsToBurn, poolId);
}
return (premium, allocationId);
}
function getActiveAllocationsWithoutCover(
uint productId,
uint allocationId,
uint start,
uint expiration
) internal returns (uint[] memory activeAllocations) {
uint packedCoverTrancheAllocation = coverTrancheAllocations[allocationId];
activeAllocations = getActiveAllocations(productId);
uint currentFirstActiveTrancheId = block.timestamp / TRANCHE_DURATION;
uint[] memory coverAllocations = new uint[](MAX_ACTIVE_TRANCHES);
// number of already expired tranches to skip
// currentFirstActiveTranche - previousFirstActiveTranche
uint offset = currentFirstActiveTrancheId - (start / TRANCHE_DURATION);
for (uint i = offset; i < MAX_ACTIVE_TRANCHES; i++) {
uint allocated = uint32(packedCoverTrancheAllocation >> (i * 32));
uint currentTrancheIdx = i - offset;
activeAllocations[currentTrancheIdx] -= allocated;
coverAllocations[currentTrancheIdx] = allocated;
}
// remove expiring cover amounts from buckets
updateExpiringCoverAmounts(
productId,
currentFirstActiveTrancheId,
Math.divCeil(expiration, BUCKET_DURATION), // targetBucketId
coverAllocations,
false // isAllocation
);
return activeAllocations;
}
function getActiveAllocations(
uint productId
) public view returns (uint[] memory trancheAllocations) {
uint _firstActiveTrancheId = block.timestamp / TRANCHE_DURATION;
uint currentBucket = block.timestamp / BUCKET_DURATION;
uint lastBucketId;
(trancheAllocations, lastBucketId) = getStoredAllocations(productId, _firstActiveTrancheId);
if (lastBucketId == 0) {
lastBucketId = currentBucket;
}
for (uint bucketId = lastBucketId + 1; bucketId <= currentBucket; bucketId++) {
uint[] memory expirations = getExpiringCoverAmounts(productId, bucketId, _firstActiveTrancheId);
for (uint i = 0; i < MAX_ACTIVE_TRANCHES; i++) {
trancheAllocations[i] -= expirations[i];
}
}
return trancheAllocations;
}
function getStoredAllocations(
uint productId,
uint firstTrancheId
) internal view returns (
uint[] memory storedAllocations,
uint16 lastBucketId
) {
storedAllocations = new uint[](MAX_ACTIVE_TRANCHES);
uint firstGroupId = firstTrancheId / COVER_TRANCHE_GROUP_SIZE;
uint lastGroupId = (firstTrancheId + MAX_ACTIVE_TRANCHES - 1) / COVER_TRANCHE_GROUP_SIZE;
// min 2 and max 3 groups
uint groupCount = lastGroupId - firstGroupId + 1;
TrancheAllocationGroup[] memory allocationGroups = new TrancheAllocationGroup[](groupCount);
for (uint i = 0; i < groupCount; i++) {
allocationGroups[i] = trancheAllocationGroups[productId][firstGroupId + i];
}
lastBucketId = allocationGroups[0].getLastBucketId();
// flatten groups
for (uint i = 0; i < MAX_ACTIVE_TRANCHES; i++) {
uint trancheId = firstTrancheId + i;
uint trancheGroupIndex = trancheId / COVER_TRANCHE_GROUP_SIZE - firstGroupId;
uint trancheIndexInGroup = trancheId % COVER_TRANCHE_GROUP_SIZE;
storedAllocations[i] = allocationGroups[trancheGroupIndex].getItemAt(trancheIndexInGroup);
}
}
function getExpiringCoverAmounts(
uint productId,
uint bucketId,
uint firstTrancheId
) internal view returns (uint[] memory expiringCoverAmounts) {
expiringCoverAmounts = new uint[](MAX_ACTIVE_TRANCHES);
uint firstGroupId = firstTrancheId / BUCKET_TRANCHE_GROUP_SIZE;
uint lastGroupId = (firstTrancheId + MAX_ACTIVE_TRANCHES - 1) / BUCKET_TRANCHE_GROUP_SIZE;
// min 1, max 2
uint groupCount = lastGroupId - firstGroupId + 1;
TrancheGroupBucket[] memory trancheGroupBuckets = new TrancheGroupBucket[](groupCount);
// min 1 and max 2 reads
for (uint i = 0; i < groupCount; i++) {
trancheGroupBuckets[i] = expiringCoverBuckets[productId][bucketId][firstGroupId + i];
}
// flatten bucket tranche groups
for (uint i = 0; i < MAX_ACTIVE_TRANCHES; i++) {
uint trancheId = firstTrancheId + i;
uint trancheGroupIndex = trancheId / BUCKET_TRANCHE_GROUP_SIZE - firstGroupId;
uint trancheIndexInGroup = trancheId % BUCKET_TRANCHE_GROUP_SIZE;
expiringCoverAmounts[i] = trancheGroupBuckets[trancheGroupIndex].getItemAt(trancheIndexInGroup);
}
return expiringCoverAmounts;
}
function getActiveTrancheCapacities(
uint productId,
uint globalCapacityRatio,
uint capacityReductionRatio
) public view returns (
uint[] memory trancheCapacities,
uint totalCapacity
) {
trancheCapacities = getTrancheCapacities(
productId,
block.timestamp / TRANCHE_DURATION, // first active tranche id
MAX_ACTIVE_TRANCHES,
globalCapacityRatio,
capacityReductionRatio
);
totalCapacity = Math.sum(trancheCapacities);
return (trancheCapacities, totalCapacity);
}
function getTrancheCapacities(
uint productId,
uint firstTrancheId,
uint trancheCount,
uint capacityRatio,
uint reductionRatio
) public view returns (uint[] memory trancheCapacities) {
// will revert if with unprocessed expirations
if (firstTrancheId < block.timestamp / TRANCHE_DURATION) {
revert RequestedTrancheIsExpired();
}
uint _activeStake = activeStake;
uint _stakeSharesSupply = stakeSharesSupply;
trancheCapacities = new uint[](trancheCount);
if (_stakeSharesSupply == 0) {
return trancheCapacities;
}
// TODO: can we get rid of the extra call to SP here?
uint multiplier =
capacityRatio
* (CAPACITY_REDUCTION_DENOMINATOR - reductionRatio)
* stakingProducts.getProductTargetWeight(poolId, productId);
uint denominator =
GLOBAL_CAPACITY_DENOMINATOR
* CAPACITY_REDUCTION_DENOMINATOR
* WEIGHT_DENOMINATOR;
for (uint i = 0; i < trancheCount; i++) {
uint trancheStake = (_activeStake * tranches[firstTrancheId + i].stakeShares / _stakeSharesSupply);
trancheCapacities[i] = trancheStake * multiplier / denominator / NXM_PER_ALLOCATION_UNIT;
}
return trancheCapacities;
}
function allocate(
uint amount,
AllocationRequest calldata request,
uint[] memory trancheAllocations
) internal returns (
uint coverAllocationAmount,
uint initialCapacityUsed,
uint totalCapacity,
uint allocationId
) {
if (request.allocationId == 0) {
allocationId = ++lastAllocationId;
} else {
allocationId = request.allocationId;
}
coverAllocationAmount = Math.divCeil(amount, NXM_PER_ALLOCATION_UNIT);
uint _firstActiveTrancheId = block.timestamp / TRANCHE_DURATION;
uint[] memory coverAllocations = new uint[](MAX_ACTIVE_TRANCHES);
{
uint firstTrancheIdToUse = (block.timestamp + request.period + request.gracePeriod) / TRANCHE_DURATION;
uint startIndex = firstTrancheIdToUse - _firstActiveTrancheId;
uint[] memory trancheCapacities = getTrancheCapacities(
request.productId,
_firstActiveTrancheId,
MAX_ACTIVE_TRANCHES, // count
request.globalCapacityRatio,
request.capacityReductionRatio
);
uint remainingAmount = coverAllocationAmount;
uint carryOver;
uint packedCoverAllocations;
for (uint i = 0; i < startIndex; i++) {
uint allocated = trancheAllocations[i];
uint capacity = trancheCapacities[i];
if (allocated > capacity) {
carryOver += allocated - capacity;
} else if (carryOver > 0) {
carryOver -= Math.min(carryOver, capacity - allocated);
}
}
initialCapacityUsed = carryOver;
for (uint i = startIndex; i < MAX_ACTIVE_TRANCHES; i++) {
initialCapacityUsed += trancheAllocations[i];
totalCapacity += trancheCapacities[i];
if (trancheAllocations[i] >= trancheCapacities[i]) {
// carry over overallocation
carryOver += trancheAllocations[i] - trancheCapacities[i];
continue;
}
if (remainingAmount == 0) {
// not breaking out of the for loop because we need the total capacity calculated above
continue;
}
uint allocatedAmount;
{
uint available = trancheCapacities[i] - trancheAllocations[i];
if (carryOver > available) {
// no capacity left in this tranche
carryOver -= available;
continue;
}
available -= carryOver;
carryOver = 0;
allocatedAmount = Math.min(available, remainingAmount);
}
coverAllocations[i] = allocatedAmount;
trancheAllocations[i] += allocatedAmount;
remainingAmount -= allocatedAmount;
packedCoverAllocations |= allocatedAmount << i * 32;
}
coverTrancheAllocations[allocationId] = packedCoverAllocations;
if (remainingAmount != 0) {
revert InsufficientCapacity();
}
}
updateExpiringCoverAmounts(
request.productId,
_firstActiveTrancheId,
Math.divCeil(block.timestamp + request.period, BUCKET_DURATION), // targetBucketId
coverAllocations,
true // isAllocation
);
updateStoredAllocations(
request.productId,
_firstActiveTrancheId,
trancheAllocations
);
return (coverAllocationAmount, initialCapacityUsed, totalCapacity, allocationId);
}
function updateStoredAllocations(
uint productId,
uint firstTrancheId,
uint[] memory allocations
) internal {
uint firstGroupId = firstTrancheId / COVER_TRANCHE_GROUP_SIZE;
uint lastGroupId = (firstTrancheId + MAX_ACTIVE_TRANCHES - 1) / COVER_TRANCHE_GROUP_SIZE;
uint groupCount = lastGroupId - firstGroupId + 1;
TrancheAllocationGroup[] memory allocationGroups = new TrancheAllocationGroup[](groupCount);
// min 2 and max 3 reads
for (uint i = 0; i < groupCount; i++) {
allocationGroups[i] = trancheAllocationGroups[productId][firstGroupId + i];
}
for (uint i = 0; i < MAX_ACTIVE_TRANCHES; i++) {
uint trancheId = firstTrancheId + i;
uint trancheGroupIndex = trancheId / COVER_TRANCHE_GROUP_SIZE - firstGroupId;
uint trancheIndexInGroup = trancheId % COVER_TRANCHE_GROUP_SIZE;
// setItemAt does not mutate so we have to reassign it
allocationGroups[trancheGroupIndex] = allocationGroups[trancheGroupIndex].setItemAt(
trancheIndexInGroup,
allocations[i].toUint48()
);
}
uint16 currentBucket = (block.timestamp / BUCKET_DURATION).toUint16();
for (uint i = 0; i < groupCount; i++) {
trancheAllocationGroups[productId][firstGroupId + i] = allocationGroups[i].setLastBucketId(currentBucket);
}
}
function updateExpiringCoverAmounts(
uint productId,
uint firstTrancheId,
uint targetBucketId,
uint[] memory coverTrancheAllocation,
bool isAllocation
) internal {
uint firstGroupId = firstTrancheId / BUCKET_TRANCHE_GROUP_SIZE;
uint lastGroupId = (firstTrancheId + MAX_ACTIVE_TRANCHES - 1) / BUCKET_TRANCHE_GROUP_SIZE;
uint groupCount = lastGroupId - firstGroupId + 1;
TrancheGroupBucket[] memory trancheGroupBuckets = new TrancheGroupBucket[](groupCount);
// min 1 and max 2 reads
for (uint i = 0; i < groupCount; i++) {
trancheGroupBuckets[i] = expiringCoverBuckets[productId][targetBucketId][firstGroupId + i];
}
for (uint i = 0; i < MAX_ACTIVE_TRANCHES; i++) {
uint trancheId = firstTrancheId + i;
uint trancheGroupId = trancheId / BUCKET_TRANCHE_GROUP_SIZE - firstGroupId;
uint trancheIndexInGroup = trancheId % BUCKET_TRANCHE_GROUP_SIZE;
uint32 expiringAmount = trancheGroupBuckets[trancheGroupId].getItemAt(trancheIndexInGroup);
uint32 trancheAllocation = coverTrancheAllocation[i].toUint32();
if (isAllocation) {
expiringAmount += trancheAllocation;
} else {
expiringAmount -= trancheAllocation;
}
// setItemAt does not mutate so we have to reassign it
trancheGroupBuckets[trancheGroupId] = trancheGroupBuckets[trancheGroupId].setItemAt(
trancheIndexInGroup,
expiringAmount
);
}
for (uint i = 0; i < groupCount; i++) {
expiringCoverBuckets[productId][targetBucketId][firstGroupId + i] = trancheGroupBuckets[i];
}
}
/// Extends the period of an existing deposit until a tranche that ends further into the future
///
/// @param tokenId The id of the NFT that proves the ownership of the deposit.
/// @param initialTrancheId The id of the tranche the deposit is already a part of.
/// @param newTrancheId The id of the new tranche determining the new deposit period.
/// @param topUpAmount An optional amount if the user wants to also increase the deposit
function extendDeposit(
uint tokenId,
uint initialTrancheId,
uint newTrancheId,
uint topUpAmount
) external whenNotPaused whenNotHalted {
// token id 0 is only used for pool manager fee tracking, no deposits allowed
if (tokenId == 0) {
revert InvalidTokenId();
}
// validate token id exists and belongs to this pool
// stakingPoolOf() reverts for non-existent tokens
if (stakingNFT.stakingPoolOf(tokenId) != poolId) {
revert InvalidStakingPoolForToken();
}
if (isPrivatePool && msg.sender != manager()) {
revert PrivatePool();
}
if (!stakingNFT.isApprovedOrOwner(msg.sender, tokenId)) {
revert NotTokenOwnerOrApproved();
}
if (topUpAmount > 0 && block.timestamp <= nxm.isLockedForMV(msg.sender)) {
revert NxmIsLockedForGovernanceVote();
}
uint _firstActiveTrancheId = block.timestamp / TRANCHE_DURATION;
{
if (initialTrancheId >= newTrancheId) {
revert NewTrancheEndsBeforeInitialTranche();
}
uint maxTrancheId = _firstActiveTrancheId + MAX_ACTIVE_TRANCHES - 1;
if (newTrancheId > maxTrancheId) {
revert RequestedTrancheIsNotYetActive();
}
if (newTrancheId < firstActiveTrancheId) {
revert RequestedTrancheIsExpired();
}
}
// if the initial tranche is expired, withdraw everything and make a new deposit
// this requires the user to have grante sufficient allowance
if (initialTrancheId < _firstActiveTrancheId) {
uint[] memory trancheIds = new uint[](1);
trancheIds[0] = initialTrancheId;
(uint withdrawnStake, /* uint rewardsToWithdraw */) = withdraw(
tokenId,
true, // withdraw the deposit
true, // withdraw the rewards
trancheIds
);
depositTo(withdrawnStake + topUpAmount, newTrancheId, tokenId, msg.sender);
return;
// done! skip the rest of the function.
}
// if we got here - the initial tranche is still active. move all the shares to the new tranche
// passing true because we mint reward shares
processExpirations(true);
Deposit memory initialDeposit = deposits[tokenId][initialTrancheId];
Deposit memory updatedDeposit = deposits[tokenId][newTrancheId];
uint _activeStake = activeStake;
uint _stakeSharesSupply = stakeSharesSupply;
uint newStakeShares;
// calculate the new stake shares if there's a deposit top up
if (topUpAmount > 0) {
newStakeShares = _stakeSharesSupply * topUpAmount / _activeStake;
activeStake = (_activeStake + topUpAmount).toUint96();
}
// calculate the new reward shares
uint newRewardsShares = calculateNewRewardShares(
initialDeposit.stakeShares,
newStakeShares,
initialTrancheId,
newTrancheId,
block.timestamp
);
{
Tranche memory initialTranche = tranches[initialTrancheId];
Tranche memory newTranche = tranches[newTrancheId];
// move the shares to the new tranche
initialTranche.stakeShares -= initialDeposit.stakeShares;
initialTranche.rewardsShares -= initialDeposit.rewardsShares;
newTranche.stakeShares += initialDeposit.stakeShares + newStakeShares.toUint128();
newTranche.rewardsShares += (initialDeposit.rewardsShares + newRewardsShares).toUint128();
// store the updated tranches
tranches[initialTrancheId] = initialTranche;
tranches[newTrancheId] = newTranche;
}
uint _accNxmPerRewardsShare = accNxmPerRewardsShare;
// if there already is a deposit on the new tranche, calculate its pending rewards
if (updatedDeposit.lastAccNxmPerRewardShare != 0) {
uint newEarningsPerShare = _accNxmPerRewardsShare.uncheckedSub(updatedDeposit.lastAccNxmPerRewardShare);
updatedDeposit.pendingRewards += (newEarningsPerShare * updatedDeposit.rewardsShares / ONE_NXM).toUint96();
}
// calculate the rewards for the deposit being extended and move them to the new deposit
{
uint newEarningsPerShare = _accNxmPerRewardsShare.uncheckedSub(initialDeposit.lastAccNxmPerRewardShare);
updatedDeposit.pendingRewards += (newEarningsPerShare * initialDeposit.rewardsShares / ONE_NXM).toUint96();
updatedDeposit.pendingRewards += initialDeposit.pendingRewards;
}
updatedDeposit.lastAccNxmPerRewardShare = _accNxmPerRewardsShare.toUint96();
updatedDeposit.stakeShares += (initialDeposit.stakeShares + newStakeShares).toUint128();
updatedDeposit.rewardsShares += (initialDeposit.rewardsShares + newRewardsShares).toUint128();
// everything is moved, delete the initial deposit
delete deposits[tokenId][initialTrancheId];
// store the new deposit.
deposits[tokenId][newTrancheId] = updatedDeposit;
// update global shares supply
stakeSharesSupply = (_stakeSharesSupply + newStakeShares).toUint128();
rewardsSharesSupply += newRewardsShares.toUint128();
// transfer nxm from the staker and update the pool deposit balance
tokenController.depositStakedNXM(msg.sender, topUpAmount, poolId);
emit DepositExtended(msg.sender, tokenId, initialTrancheId, newTrancheId, topUpAmount);
}
function burnStake(uint amount, BurnStakeParams calldata params) external onlyCoverContract {
// passing false because neither the amount of shares nor the reward per second are changed
processExpirations(false);
// sload
uint _activeStake = activeStake;
// If all stake is burned, leave 1 wei and close pool
if (amount >= _activeStake) {
amount = _activeStake - 1;
isHalted = true;
}
tokenController.burnStakedNXM(amount, poolId);
// sstore
activeStake = (_activeStake - amount).toUint96();
uint initialPackedCoverTrancheAllocation = coverTrancheAllocations[params.allocationId];
uint[] memory activeAllocations = getActiveAllocations(params.productId);
uint currentFirstActiveTrancheId = block.timestamp / TRANCHE_DURATION;
uint[] memory coverDeallocations = new uint[](MAX_ACTIVE_TRANCHES);
uint remainingDeallocationAmount = params.deallocationAmount / NXM_PER_ALLOCATION_UNIT;
uint newPackedCoverAllocations;
// number of already expired tranches to skip
// currentFirstActiveTranche - previousFirstActiveTranche
uint offset = currentFirstActiveTrancheId - (params.start / TRANCHE_DURATION);
// iterate the tranches backward to remove allocation from future tranches first
for (uint i = MAX_ACTIVE_TRANCHES - 1; i >= offset; i--) {
// i = tranche index when the allocation was made
// i - offset = index of the same tranche but in currently active tranches arrays
uint currentTrancheIdx = i - offset;
uint allocated = uint32(initialPackedCoverTrancheAllocation >> (i * 32));
uint deallocateAmount = Math.min(allocated, remainingDeallocationAmount);
activeAllocations[currentTrancheIdx] -= deallocateAmount;
coverDeallocations[currentTrancheIdx] = deallocateAmount;
newPackedCoverAllocations |= (allocated - deallocateAmount) << i * 32;
remainingDeallocationAmount -= deallocateAmount;
// avoids underflow in the for decrement loop
if (i == 0) {
break;
}
}
coverTrancheAllocations[params.allocationId] = newPackedCoverAllocations;
updateExpiringCoverAmounts(
params.productId,
currentFirstActiveTrancheId,
Math.divCeil(params.start + params.period, BUCKET_DURATION), // targetBucketId
coverDeallocations,
false // isAllocation
);
updateStoredAllocations(
params.productId,
currentFirstActiveTrancheId,
activeAllocations
);
emit StakeBurned(amount);
}
/* pool management */
function setPoolFee(uint newFee) external onlyManager {
if (newFee > maxPoolFee) {
revert PoolFeeExceedsMax();
}
uint oldFee = poolFee;
poolFee = uint8(newFee);
// passing true because the amount of rewards shares changes
processExpirations(true);
uint fromTrancheId = block.timestamp / TRANCHE_DURATION;
uint toTrancheId = fromTrancheId + MAX_ACTIVE_TRANCHES - 1;
uint _accNxmPerRewardsShare = accNxmPerRewardsShare;
for (uint trancheId = fromTrancheId; trancheId <= toTrancheId; trancheId++) {
// sload
Deposit memory feeDeposit = deposits[0][trancheId];
if (feeDeposit.rewardsShares == 0) {
continue;
}
// update pending reward and reward shares
uint newRewardPerRewardsShare = _accNxmPerRewardsShare.uncheckedSub(feeDeposit.lastAccNxmPerRewardShare);
feeDeposit.pendingRewards += (newRewardPerRewardsShare * feeDeposit.rewardsShares / ONE_NXM).toUint96();
feeDeposit.lastAccNxmPerRewardShare = _accNxmPerRewardsShare.toUint96();
// TODO: would using tranche.rewardsShares give a better precision?
feeDeposit.rewardsShares = (uint(feeDeposit.rewardsShares) * newFee / oldFee).toUint128();
// sstore
deposits[0][trancheId] = feeDeposit;
}
emit PoolFeeChanged(msg.sender, newFee);
}
function setPoolPrivacy(bool _isPrivatePool) external onlyManager {
isPrivatePool = _isPrivatePool;
emit PoolPrivacyChanged(msg.sender, _isPrivatePool);
}
function setPoolDescription(string memory ipfsDescriptionHash) external onlyManager {
emit PoolDescriptionSet(ipfsDescriptionHash);
}
/* getters */
function manager() public override view returns (address) {
return tokenController.getStakingPoolManager(poolId);
}
function getPoolId() external override view returns (uint) {
return poolId;
}
function getPoolFee() external override view returns (uint) {
return poolFee;
}
function getMaxPoolFee() external override view returns (uint) {
return maxPoolFee;
}
function getActiveStake() external override view returns (uint) {
return activeStake;
}
function getStakeSharesSupply() external override view returns (uint) {
return stakeSharesSupply;
}
function getRewardsSharesSupply() external override view returns (uint) {
return rewardsSharesSupply;
}
function getRewardPerSecond() external override view returns (uint) {
return rewardPerSecond;
}
function getAccNxmPerRewardsShare() external override view returns (uint) {
return accNxmPerRewardsShare;
}
function getLastAccNxmUpdate() external override view returns (uint) {
return lastAccNxmUpdate;
}
function getFirstActiveTrancheId() external override view returns (uint) {
return firstActiveTrancheId;
}
function getFirstActiveBucketId() external override view returns (uint) {
return firstActiveBucketId;
}
function getNextAllocationId() external override view returns (uint) {
return lastAllocationId + 1;
}
function getDeposit(uint tokenId, uint trancheId) external override view returns (
uint lastAccNxmPerRewardShare,
uint pendingRewards,
uint stakeShares,
uint rewardsShares
) {
Deposit memory deposit = deposits[tokenId][trancheId];
return (
deposit.lastAccNxmPerRewardShare,
deposit.pendingRewards,
deposit.stakeShares,
deposit.rewardsShares
);
}
function getTranche(uint trancheId) external override view returns (
uint stakeShares,
uint rewardsShares
) {
Tranche memory tranche = tranches[trancheId];
return (
tranche.stakeShares,
tranche.rewardsShares
);
}
function getExpiredTranche(uint trancheId) external override view returns (
uint accNxmPerRewardShareAtExpiry,
uint stakeAmountAtExpiry,
uint stakeSharesSupplyAtExpiry
) {
ExpiredTranche memory expiredTranche = expiredTranches[trancheId];
return (
expiredTranche.accNxmPerRewardShareAtExpiry,
expiredTranche.stakeAmountAtExpiry,
expiredTranche.stakeSharesSupplyAtExpiry
);
}
}
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.18;
// 5 x uint48 activeAllocation + 1 x uint16 lastBucketId
// 5 * 48 + 16 = 256
type TrancheAllocationGroup is uint;
// group ids: ________0_________|_________1_________|_________3__ ...
// tranche ids: 0 1 2 3 4 | 5 6 7 8 9 | 10 11 12 ...
// active tranches: \________________________________/
// 8 x (uint32 expiringAllocation)
type TrancheGroupBucket is uint;
library StakingTypesLib {
// TrancheAllocationGroup
function getLastBucketId(TrancheAllocationGroup items) internal pure returns (uint16) {
return uint16(TrancheAllocationGroup.unwrap(items));
}
function setLastBucketId(
TrancheAllocationGroup items,
uint16 lastBucketId
) internal pure returns (TrancheAllocationGroup) {
// applying the mask using binary AND to clear target item's bits
uint mask = ~(uint(type(uint16).max));
uint underlying = TrancheAllocationGroup.unwrap(items);
return TrancheAllocationGroup.wrap(underlying & mask | uint(lastBucketId));
}
function getItemAt(
TrancheAllocationGroup items,
uint index
) internal pure returns (uint48 allocation) {
uint underlying = TrancheAllocationGroup.unwrap(items);
return uint48(underlying >> (index * 48 + 16));
}
// heads up: does not mutate the TrancheAllocationGroup but returns a new one instead
function setItemAt(
TrancheAllocationGroup items,
uint index,
uint48 allocation
) internal pure returns (TrancheAllocationGroup) {
// applying the mask using binary AND to clear target item's bits
uint mask = ~(uint(type(uint64).max) << (index * 48 + 16));
uint item = uint(allocation) << (index * 48 + 16);
uint underlying = TrancheAllocationGroup.unwrap(items) & mask | item;
return TrancheAllocationGroup.wrap(underlying);
}
// TrancheGroupBucket
function getItemAt(
TrancheGroupBucket items,
uint index
) internal pure returns (uint32) {
uint underlying = TrancheGroupBucket.unwrap(items);
return uint32(underlying >> (index * 32));
}
// heads up: does not mutate the TrancheGroupBucket but returns a new one instead
function setItemAt(
TrancheGroupBucket items,
uint index,
uint32 value
) internal pure returns (TrancheGroupBucket) {
// applying the mask using binary AND to clear target item's bits
uint mask = ~(uint(type(uint32).max) << (index * 32));
uint itemUnderlying = uint(value) << (index * 32);
uint groupUnderlying = TrancheGroupBucket.unwrap(items) & mask | itemUnderlying;
return TrancheGroupBucket.wrap(groupUnderlying);
}
}