Skip to content

Commit

Permalink
refactor: remove and inline calculatePremiumPerYear
Browse files Browse the repository at this point in the history
  • Loading branch information
duxxud committed Dec 2, 2024
1 parent 43aeb12 commit 444f58e
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 156 deletions.
12 changes: 0 additions & 12 deletions contracts/interfaces/IStakingProducts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,13 @@ interface IStakingProducts {
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);

/* ========== STAKING POOL CREATION ========== */

function stakingPool(uint poolId) external view returns (IStakingPool);
Expand Down
6 changes: 1 addition & 5 deletions contracts/mocks/generic/StakingProductsGeneric.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ contract StakingProductsGeneric is IStakingProducts {
revert("Unsupported");
}

function calculatePremium(StakedProduct memory, uint, uint, uint, uint, uint, uint, uint, uint, uint) public virtual pure returns (uint, StakedProduct memory) {
revert("Unsupported");
}

function calculatePremiumPerYear(uint, uint, uint, uint, uint, uint, uint) public virtual pure returns (uint) {
function calculatePremium(StakedProduct memory, uint, uint, uint, uint, uint, uint, uint) public virtual pure returns (uint, StakedProduct memory) {
revert("Unsupported");
}

Expand Down
36 changes: 5 additions & 31 deletions contracts/mocks/modules/StakingPool/SKMockStakingProducts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -319,12 +319,12 @@ contract SKMockStakingProducts is StakingProductsGeneric, MasterAwareV2, Multica
uint productId,
uint period,
uint coverAmount,
uint initialCapacityUsed,
uint /*initialCapacityUsed*/, // TODO: deprecated
uint totalCapacity,
uint globalMinPrice,
bool useFixedPrice,
uint nxmPerAllocationUnit,
uint allocationUnitsPerNXM
uint /*allocationUnitsPerNXM*/ // TODO: deprecated
) public override returns (uint premium) {

StakedProduct memory product = _products[poolId][productId];
Expand All @@ -338,12 +338,10 @@ contract SKMockStakingProducts is StakingProductsGeneric, MasterAwareV2, Multica
product,
period,
coverAmount,
initialCapacityUsed,
totalCapacity,
targetPrice,
block.timestamp,
nxmPerAllocationUnit,
allocationUnitsPerNXM,
TARGET_PRICE_DENOMINATOR
);

Expand Down Expand Up @@ -374,12 +372,10 @@ contract SKMockStakingProducts is StakingProductsGeneric, MasterAwareV2, Multica
StakedProduct memory product,
uint period,
uint coverAmount,
uint initialCapacityUsed,
uint totalCapacity,
uint targetPrice,
uint currentBlockTimestamp,
uint nxmPerAllocationUnit,
uint allocationUnitsPerNxm,
uint targetPriceDenominator
) public override pure returns (uint premium, StakedProduct memory) {

Expand All @@ -401,36 +397,14 @@ contract SKMockStakingProducts is StakingProductsGeneric, MasterAwareV2, Multica
product.bumpedPrice = (basePrice + priceBump).toUint96();
product.bumpedPriceUpdateTime = uint32(currentBlockTimestamp);

// use calculated base price and apply surge pricing if applicable
uint premiumPerYear = calculatePremiumPerYear(
basePrice,
coverAmount,
initialCapacityUsed,
totalCapacity,
nxmPerAllocationUnit,
allocationUnitsPerNxm,
targetPriceDenominator
);
// cover amount has 2 decimals (100 = 1 unit)
// scale coverAmount to 18 decimals and apply price percentage
uint premiumPerYear = coverAmount * nxmPerAllocationUnit * basePrice / targetPriceDenominator;

// calculate the premium for the requested period
return (premiumPerYear * period / 365 days, product);
}

function calculatePremiumPerYear(
uint basePrice,
uint coverAmount,
uint /*initialCapacityUsed*/, //TODO: depricated
uint /*totalCapacity*/, // TODO: depricated
uint nxmPerAllocationUnit,
uint /*allocationUnitsPerNxm*/, // TODO: depricated
uint targetPriceDenominator
) public override pure returns (uint) {
// cover amount has 2 decimals (100 = 1 unit)
// scale coverAmount to 18 decimals and apply price percentage
uint premium = coverAmount * nxmPerAllocationUnit * basePrice / targetPriceDenominator;
return premium;
}

/* dependencies */

function changeDependentContractAddress() external {
Expand Down
36 changes: 5 additions & 31 deletions contracts/modules/staking/StakingProducts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -369,12 +369,12 @@ contract StakingProducts is IStakingProducts, MasterAwareV2, Multicall {
uint productId,
uint period,
uint coverAmount,
uint initialCapacityUsed,
uint /*initialCapacityUsed*/, // TODO: deprecated
uint totalCapacity,
uint globalMinPrice,
bool useFixedPrice,
uint nxmPerAllocationUnit,
uint allocationUnitsPerNXM
uint /*allocationUnitsPerNXM*/ // TODO: deprecated
) public returns (uint premium) {

if (msg.sender != StakingPoolLibrary.getAddress(stakingPoolFactory, poolId)) {
Expand All @@ -392,12 +392,10 @@ contract StakingProducts is IStakingProducts, MasterAwareV2, Multicall {
product,
period,
coverAmount,
initialCapacityUsed,
totalCapacity,
targetPrice,
block.timestamp,
nxmPerAllocationUnit,
allocationUnitsPerNXM,
TARGET_PRICE_DENOMINATOR
);

Expand Down Expand Up @@ -446,12 +444,10 @@ contract StakingProducts is IStakingProducts, MasterAwareV2, Multicall {
StakedProduct memory product,
uint period,
uint coverAmount,
uint initialCapacityUsed,
uint totalCapacity,
uint targetPrice,
uint currentBlockTimestamp,
uint nxmPerAllocationUnit,
uint allocationUnitsPerNxm,
uint targetPriceDenominator
) public pure returns (uint premium, StakedProduct memory) {

Expand All @@ -470,36 +466,14 @@ contract StakingProducts is IStakingProducts, MasterAwareV2, Multicall {
product.bumpedPrice = (basePrice + priceBump).toUint96();
product.bumpedPriceUpdateTime = uint32(currentBlockTimestamp);

// use calculated base price and apply surge pricing if applicable
uint premiumPerYear = calculatePremiumPerYear(
basePrice,
coverAmount,
initialCapacityUsed,
totalCapacity,
nxmPerAllocationUnit,
allocationUnitsPerNxm,
targetPriceDenominator
);
// cover amount has 2 decimals (100 = 1 unit)
// scale coverAmount to 18 decimals and apply price percentage
uint premiumPerYear = coverAmount * nxmPerAllocationUnit * basePrice / targetPriceDenominator;

// calculate the premium for the requested period
return (premiumPerYear * period / 365 days, product);
}

function calculatePremiumPerYear(
uint basePrice,
uint coverAmount,
uint /*initialCapacityUsed*/, // TODO: depricated
uint /*totalCapacity*/, // TODO: depricated
uint nxmPerAllocationUnit,
uint /*allocationUnitsPerNxm*/, // TODO: depricated
uint targetPriceDenominator
) public pure returns (uint) {
// cover amount has 2 decimals (100 = 1 unit)
// scale coverAmount to 18 decimals and apply price percentage
uint premium = coverAmount * nxmPerAllocationUnit * basePrice / targetPriceDenominator;
return premium;
}

/* ========== STAKING POOL CREATION ========== */

function stakingPool(uint poolId) public view returns (IStakingPool) {
Expand Down
Loading

0 comments on commit 444f58e

Please sign in to comment.