-
Notifications
You must be signed in to change notification settings - Fork 62
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
docs: existing contract docs improvements #1324
docs: existing contract docs improvements #1324
Conversation
20fe35a
to
e8bbc15
Compare
e8bbc15
to
0b736f7
Compare
StakingPool Contract Developer DocumentationTable of ContentsOverviewThe Each StakingPool contract represents its own distinct pool that manages the staked NXM tokens and the allocations of those staked NXM to cover products. This allows for precise management of stakes and cover allocations specific to that pool When a user stakes NXM, the contract mints an NFT, which serves as a proof of stake ownership. This contract handles:
Key ConceptsTranches
struct Tranche {
uint128 stakeShares;
uint128 rewardsShares;
}
Formula for current tranche ID:uint currentTrancheId = block.timestamp / TRANCHE_DURATION; Buckets
Formula for current bucket ID:uint currentBucketId = block.timestamp / BUCKET_DURATION; Allocations
FunctionsMutative Functions
|
Parameter | Description |
---|---|
amount |
The amount to deposit. |
trancheId |
The ID of the tranche to deposit into. |
requestTokenId |
The ID of the request token (0 for a new deposit, or use an existing token ID to add to a previous deposit) |
destination |
The address to send the Staking NFT token to. |
- Creates stake & reward shares.
- Emits
StakeDeposited
.
withdraw
Allows users to withdraw their stake and/or rewards from specific tranches. Withdrawing the stakes can be done only on expired tranches, while rewards can be withdrawn at any time.
function withdraw(uint tokenId, bool withdrawStake, bool withdrawRewards, uint[] memory trancheIds) external;
Parameter | Description |
---|---|
tokenId |
The ID of the staking NFT token. |
withdrawStake |
Whether to withdraw the stake. |
withdrawRewards |
Whether to withdraw the rewards. |
trancheIds |
The IDs of the tranches to withdraw from. |
- Stake can only be withdrawn from expired tranches.
- Rewards can be withdrawn at any time.
- Emits
Withdraw
.
extendDeposit
Extends the duration of a deposit by moving it from an tranche to a future tranche.
function extendDeposit(uint tokenId, uint initialTrancheId, uint targetTrancheId, uint topUpAmount) external;
Parameter | Description |
---|---|
tokenId |
The ID of the staking NFT token. |
initialTrancheId |
The ID of the initial tranche. |
targetTrancheId |
The ID of the target tranche. |
topUpAmount |
The amount to top up the deposit by. |
- Tranche must be active to extend.
- Emits
DepositExtended
.
View Functions
getDeposit
Get deposit details for a given NFT and tranche.
function getDeposit(uint tokenId, uint trancheId) external view returns (
uint lastAccNxmPerRewardShare,
uint pendingRewards,
uint stakeShares,
uint rewardsShares
);
Parameter | Description |
---|---|
tokenId |
The ID of the product. |
trancheId |
The ID of the tranche. |
getTranche
Get details of a specific tranche.
function getTranche(uint trancheId) external view returns (
uint stakeShares,
uint rewardsShares
);
Parameter | Description |
---|---|
trancheId |
The ID of the tranche. |
getExpiredTranche
Get data of an expired tranche.
function getExpiredTranche(uint trancheId) external view returns (
uint accNxmPerRewardShareAtExpiry,
uint stakeAmountAtExpiry,
uint stakeSharesSupplyAtExpiry
);
Parameter | Description |
---|---|
trancheId |
The ID of the tranche. |
getActiveAllocations
Returns an array with the allocated amounts in the currently active tranches for a product.
function getActiveAllocations(uint productId) external view returns (uint[] memory trancheAllocations);
Parameter | Description |
---|---|
productId |
The ID of the product. |
getActiveTrancheCapacities
Returns an array of the active tranche capacities and total capacity for a product.
function getActiveTrancheCapacities(
uint productId,
uint globalCapacityRatio,
uint capacityReductionRatio
) public view returns (
uint[] memory trancheCapacities,
uint totalCapacity
);
Parameter | Description |
---|---|
productId |
The ID of the product. |
globalCapacityRatio |
Global Capacity Ratio |
capacityReductionRatio |
Capacity Reduction Ratio. |
getTrancheCapacities
Returns an array with the total capacities for the currently active tranches for a product.
function getTrancheCapacities(
uint productId,
uint firstTrancheId,
uint trancheCount,
uint capacityRatio,
uint reductionRatio
) external view returns (uint[] memory trancheCapacities);
Parameter | Description |
---|---|
productId |
The ID of the product. |
globalCapacityRatio |
Global Capacity Ratio |
capacityReductionRatio |
Capacity Reduction Ratio. |
Miscellaneous View Functions
getPoolId()
– Returns the pool ID.getPoolFee()
– Returns the current pool fee.getMaxPoolFee()
– Returns the max pool fee.getActiveStake()
– Returns the active stake.getStakeSharesSupply()
– Returns total stake shares.getRewardsSharesSupply()
– Returns total reward shares.getRewardPerSecond()
– Returns reward emission rate.getAccNxmPerRewardsShare()
– Returns accumulated NXM per reward share.
Events
-
StakeDeposited(address indexed user, uint256 amount, uint256 trancheId, uint256 tokenId)
- Emitted when a user deposits stake.
-
Withdraw(address indexed user, uint indexed tokenId, uint tranche, uint amountStakeWithdrawn, uint amountRewardsWithdrawn)
- Emitted when a user withdraws stake or rewards.
-
PoolFeeChanged(address indexed manager, uint newFee)
- Emitted when the pool fee is updated.
-
PoolPrivacyChanged(address indexed manager, bool isPrivate)
- Emitted when the pool’s privacy setting is changed.
FAQ
How is cover capacity allocated from tranches?
Only tranches that remain active for the full duration of the cover plus a grace period are eligible for allocation. This ensures that covers are backed by active stakes for their entire lifespan, maintaining the security of the coverage.
What happens when I deposit NXM?
You receive an NFT representing your stake, which can be used across multiple tranches.
Can I withdraw my stake at any time?
No, you can only withdraw stake from expired tranches. Rewards can be withdrawn at any time.
How are rewards distributed?
Rewards are proportional to stake size in a tranche.
Can I move my stake to a different tranche?
Yes, use extendDeposit()
to move stake from one tranche to another.
What happens if my allocation is used for cover?
Once capacity is allocated for cover, your stake remains locked until the cover expires.
Contact and Support
If you have questions or need assistance integrating with the StakingPool
contract or other parts of the protocol, please reach out through the official support channels or developer forums.
- Developer Forums: Join our community forums to discuss with other developers and seek help.
- Official Support Channels: Contact us via our official support email or join our Discord server.
- Documentation Resources: Access additional documentation, tutorials, and FAQs on our official website.
- GitHub Repository: Report issues or contribute to the codebase through our GitHub repository.
Disclaimer: This documentation provides a high-level overview of the StakingPool
contract's functionality. It is intended for developers integrating with the protocol and may omit internal details not relevant to external interactions. Always refer to the latest contract code and official resources
StakingProducts Contract Developer DocumentationTable of ContentsOverviewThe
Key ConceptsProduct WeightsEach product within a staking pool has two weight metrics:
The contract attempts to reach the target weight but may assign a lower effective weight if:
The effective weight never exceeds the target weight. Pricing MechanismThe pricing system adjusts dynamically based on usage and capacity, ensuring fair pricing and market-driven price discovery:
FunctionsMutative Functions
|
Parameter | Description |
---|---|
poolId |
ID of the staking pool. |
params |
Array of StakedProductParam structs with product settings. |
Purpose:
- Creates, updates, or removes products from the staking pool.
- Adjusts weights and pricing parameters.
- Recalculates effective weights if necessary.
recalculateEffectiveWeights
Dynamically adjusts effective weights for specified products based on current capacity and utilization.
function recalculateEffectiveWeights(uint poolId, uint[] calldata productIds) external;
Parameter | Description |
---|---|
poolId |
Staking pool ID. |
productIds |
List of product IDs to update. |
Purpose:
- Ensures fair capacity allocation by dynamically adjusting product weights.
- Recommended after significant changes in usage or pricing.
recalculateEffectiveWeightsForAllProducts
Recalculates effective weights for all products within a staking pool.
function recalculateEffectiveWeightsForAllProducts(uint poolId) external;
Parameter | Description |
---|---|
poolId |
The ID of the staking pool. |
Purpose:
- Ensures fair capacity allocation by dynamically adjusting all product weights.
- Recommended after significant changes in usage or pricing.
createStakingPool
Creates a new staking pool.
function createStakingPool(
bool isPrivatePool,
uint initialPoolFee,
uint maxPoolFee,
ProductInitializationParams[] memory productInitParams,
string calldata ipfsHash
) external returns (uint poolId, address stakingPoolAddress);
Parameter | Description |
---|---|
isPrivatePool |
true for a private pool, false for public. |
initialPoolFee |
Initial staking pool fee. |
maxPoolFee |
Maximum allowed pool fee. |
productInitParams |
Initial product parameters. |
ipfsHash |
IPFS hash for metadata. |
Purpose:
- Creates a new staking pool with customizable settings.
- Assigns pool manager role to the caller.
setPoolMetadata
Updates the metadata for a staking pool.
function setPoolMetadata(uint poolId, string calldata ipfsHash) external;
Parameter | Description |
---|---|
poolId |
ID of the staking pool. |
ipfsHash |
New IPFS hash for metadata. |
View Functions
getProduct
Retrieves product details.
function getProduct(uint poolId, uint productId) external view returns (
uint lastEffectiveWeight,
uint targetWeight,
uint targetPrice,
uint bumpedPrice,
uint bumpedPriceUpdateTime
);
getPoolManager
Returns the pool manager address.
function getPoolManager(uint poolId) public view returns (address);
Parameter | Description |
---|---|
poolId |
The ID of the staking pool. |
getPoolMetadata
Retrieves metadata IPFS hash.
function getPoolMetadata(uint poolId) external view returns (string memory ipfsHash);
Parameter | Description |
---|---|
poolId |
The ID of the staking pool. |
Pricing Functions
getPremium
Calculates the premium for a cover product.
function getPremium(
uint poolId,
uint productId,
uint period,
uint coverAmount,
uint totalCapacity,
uint productMinPrice,
bool useFixedPrice,
uint nxmPerAllocationUnit
) public returns (uint premium);
Parameter | Description |
---|---|
poolId |
The ID of the staking pool. |
productId |
The ID of the specific cover product. |
period |
The cover duration in seconds. |
coverAmount |
The coverage amount requested (in the protocol's units). |
initialCapacityUsed |
The capacity already used before this cover. |
totalCapacity |
The total capacity available in the pool. |
globalMinPrice |
The global minimum price ratio for the cover product. |
useFixedPrice |
Boolean indicating if a fixed price should be used. |
nxmPerAllocationUnit |
The amount of NXM per allocation unit. |
allocationUnitsPerNXM |
The number of allocation units per NXM token. |
Description: Typically called internally by the staking pool during cover purchase. Updates the product's bumped price and timestamp.
Includes:
- Base pricing and price bumps (as capacity is used).
- Uses product-specific
minPrice
instead of the global minimum price. - Cover period and amount affect final premium.
Events
ProductUpdated(uint indexed productId, uint targetWeight, uint targetPrice)
- Emitted when a product is updated.
FAQ
How are product weights determined?
Each product has two weight metrics:
- Target Weight (
targetWeight
) – Set by the pool manager, representing the ideal weight for the product in the pool. - Effective Weight (
lastEffectiveWeight
) – The actual weight dynamically calculated based on staked capacity, utilization, and platform-wide constraints.
The contract tries to meet the target weight but may assign a lower effective weight if the pool lacks sufficient stake or if constraints (like global capacity limits) affect the allocation.
Can effective weight be higher than the target weight?
No, the effective weight is capped at the target weight. If the available stake is low, the effective weight may be lower than the target weight, but it will never exceed the target.
Can I create a private or public staking pool?
Yes:
- Private pools (
isPrivatePool = true
) restrict who can interact. - Public pools (
isPrivatePool = false
) allow open participation.
How often should effective weights be recalculated?
Effective weights must be manually recalculated whenever staking pool conditions change. This can be done by calling:
recalculateEffectiveWeights(poolId, productIds[])
– Recalculates specific products in a pool.recalculateEffectiveWeightsForAllProducts(poolId)
– Updates all products in a pool.
Recalculations should be triggered:
- Significant cover purchases that affect capacity.
- Stake changes (new stakes, stake withdrawals, or stake extensions).
- Periodically (e.g., daily or weekly) to reflect evolving conditions.
How is the premium calculated?
Premiums are calculated dynamically based on:
- Base Price – The product's target price set by the pool manager.
- Price Bumps – As more capacity is used, the price increases slightly (+0.05% per 1% capacity used).
- Pricing Decay – Over time, the price gradually decreases (0.5% per day), allowing for price discovery.
- Minimum Price (
minPrice
) – Ensures the price never falls below a predefined level (overrides the global minimum if set). - Cover Details – The amount of cover purchased and coverage duration also impact the final premium.
This dynamic model ensures pricing reflects demand and supply, preventing underpricing while keeping costs fair for buyers.
How does StakingProducts integrate with StakingPoolFactory to create a new staking pool?
The StakingProducts contract uses the StakingPoolFactory to deploy and initialize new staking pools.
How is a new staking pool created?
-
The createStakingPool() function is called on StakingProducts.
-
This interacts with StakingPoolFactory, which deploys a new StakingPool contract.
-
The caller becomes the staking pool manager, who can then:
- Set pool parameters (e.g., fees, private/public status).
- Choose which cover products to allow in the pool.
- Set initial target weights for products in the pool.
-
The staking pool is linked to StakingProducts, enabling capacity allocation for covers.
How can I update pool metadata?
Call setPoolMetadata(poolId, ipfsHash)
, where ipfsHash
contains updated metadata.
Contact and Support
If you have questions or need assistance integrating with the StakingProducts
contract, reach out through:
- Developer Forums: Discuss with other developers.
- Official Support Channels: Contact us via email or Discord.
- Documentation Resources: Access tutorials and FAQs.
- GitHub Repository: Report issues or contribute to the codebase.
Disclaimer: This documentation provides an overview of the StakingProducts
contract. It does not cover internal logic—always refer to the latest contract code and official resources.
Description
Testing
Explain how you tested your changes to ensure they work as expected.
Checklist