Premiums
mapping(PositionId => uint256) public PremiumDeposit;// Get premiums quoted this block
// This will be automatically paid when a trader is interacting with the protocol
uint256 premiumQuoted = computePremiums(PositionId);
// can force close if deposit is less than the instantaneous premiums owed
bool canForceClose = PremiumDeposit[PositionId] < premiumQuoted; Computing Premiums
function computePremiums( PositionId positionId) public view returns(uint256 premium){
(uint lastPremiumPaymentTime, BorrowedTick[] memory borrowedTicks)
= getPositionInfo(PositionId);
// get the time-weighted averaged tick between current time and last payment time
// The 'distance' between the twat and the borrowed ticks will determine the
// multiplier
int24 twat = getTimeWeightedAverageTick(block.timestamp - lastPremiumPaymentTime);
for(uint i; i<borrowedTicks.length; i++){
// the closer the twat to the borrowed tick, the higher the multiplier
uint multiplier = getMultiplier(twat, borrowedTicks[i].tick);
// get the interest accumulator at the given tick
uint interestGrowthInTick = getInterestGrowth(borrowedTicks[i].tick);
// since interestGrowth is an accumulator, need to get actually owed interest
uint interest = (interestGrowthInTick/borrowedTicks[i].lastInterestGrowth);
// scale interest by multiplier
interest = interest * multiplier;
// add to total premiums for all borrowed ticks
premium += interest * toTokenAmounts(borrowedTicks[i].liquidity)
}
return premium;
}
function getInterestGrowth(int24 tick) public view returns(uint256){
UtilizationGrowth memory growth = UtilizationGrowths[tick];
// interest rate per second is proportional to utilization rate,
// where the utilization rate is recorded whenever a user provides,withdraws,borrows, or repays
// the tick
uint percentageIncreaseFromLastGrowth = getInterestRate(growth.lastURate)
** (block.timestamp- growth.lastUpdateTime);
// the higher the last recorded utilization rate of the tick, the faster
// the rate of growth
uint totalAccumulatedGrowthForTick = growth.lastGrowth * percentageIncreaseFromLastGrowth;
return totalAccumulatedGrowthTick;
}
Interest Rate

Last updated