11const { ethers } = require ( 'ethers' ) ;
22const { parseEther, ZeroAddress } = ethers ;
3+ const { BigIntMath } = require ( './helpers' ) ;
4+ const { PoolAsset } = require ( './constants' ) ;
35
46const COVER_PRICE_DENOMINATOR = 10000n ;
57
@@ -33,15 +35,15 @@ function calculateInternalPrice(currentState, observations, capital, supply, cur
3335
3436 const averagePriceB = ( currentObservation . priceCumulativeBelow - firstObservation . priceCumulativeBelow ) / elapsed ;
3537
36- const priceA = averagePriceA > spotPriceA ? spotPriceA : averagePriceA ;
37- const priceB = averagePriceB > spotPriceB ? averagePriceB : spotPriceB ;
38+ const priceA = BigIntMath . min ( averagePriceA , spotPriceA ) ;
39+ const priceB = BigIntMath . max ( averagePriceB , spotPriceB ) ;
40+ const bookValue = ( parseEther ( '1' ) * capital ) / supply ;
3841
39- const internalPrice = ( ( priceA + priceB - parseEther ( '1' ) ) * capital ) / supply ;
42+ const internalPrice = priceA + priceB - bookValue ;
4043 const maxPrice = ( parseEther ( '1' ) * 3n * capital ) / supply ; // 300% BV
4144 const minPrice = ( parseEther ( '1' ) * 35n * capital ) / ( supply * 100n ) ; // 35% BV
4245
43- const maxInternalPrice = internalPrice > maxPrice ? internalPrice : maxPrice ;
44- return maxInternalPrice > minPrice ? minPrice : maxInternalPrice ;
46+ return BigIntMath . max ( BigIntMath . min ( internalPrice , maxPrice ) , minPrice ) ;
4547}
4648
4749// TODO: eject from lib - we'll clearly not use it outside of tests
@@ -71,6 +73,7 @@ async function getInternalPrice(ramm, pool, tokenController, timestamp) {
7173 timestamp : observation . timestamp ,
7274 } ;
7375 }
76+
7477 const state = {
7578 nxmA : previousState . nxmA ,
7679 nxmB : previousState . nxmB ,
@@ -81,22 +84,23 @@ async function getInternalPrice(ramm, pool, tokenController, timestamp) {
8184 } ;
8285
8386 const [ currentState ] = await ramm . _getReserves ( state , context , timestamp ) ;
84-
8587 const observations = await ramm . _updateTwap ( state , previousObservations , context , timestamp ) ;
88+
8689 return calculateInternalPrice ( currentState , observations , capital , supply , timestamp , { GRANULARITY , PERIOD_SIZE } ) ;
8790}
8891
8992// ======================= COVER ==============================================
9093
91- function calculatePremium ( amount , rate , period , price , allocationUnit ) {
94+ function calculatePremium ( amount , rate , period , price , allocationUnit , paymentAsset ) {
9295 const nxmAmount = ( amount * parseEther ( '1' ) ) / rate ;
9396
9497 const coverNXMAmount =
9598 nxmAmount % allocationUnit === 0n ? nxmAmount : ( nxmAmount / allocationUnit + 1n ) * allocationUnit ;
9699
97- const premiumInNxm = ( ( ( coverNXMAmount * price ) / COVER_PRICE_DENOMINATOR ) * period ) / ( 365n * 24n * 60n * 60n ) ;
100+ const annualizedPremiumNxm = ( coverNXMAmount * price ) / COVER_PRICE_DENOMINATOR ;
101+ const premiumInNxm = ( annualizedPremiumNxm * BigInt ( period ) ) / ( 365n * 24n * 60n * 60n ) ;
98102
99- const premiumInAsset = ( premiumInNxm * rate ) / parseEther ( '1' ) ;
103+ const premiumInAsset = paymentAsset === PoolAsset . NXM ? premiumInNxm : ( premiumInNxm * rate ) / parseEther ( '1' ) ;
100104
101105 return { premiumInNxm, premiumInAsset, coverNXMAmount } ;
102106}
0 commit comments