Skip to content

Commit

Permalink
premiumprice-eda
Browse files Browse the repository at this point in the history
  • Loading branch information
Keshavrajsinghal committed Jun 14, 2024
1 parent 7e7c26b commit c54efe3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 70 deletions.
16 changes: 7 additions & 9 deletions src/L2/ExponentialPremiumPriceOracle.sol
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// SPDX-License-Identifier: MIT
pragma solidity ~0.8.17;

import "./StablePriceOracle.sol";
Expand All @@ -6,45 +7,42 @@ import {EDAPrice} from "src/lib/EDAPrice.sol";
import "solady/utils/FixedPointMathLib.sol";
import {Test, console} from "forge-std/Test.sol";


contract ExponentialPremiumPriceOracle is StablePriceOracle {
uint256 public immutable startPremium;
uint256 public immutable endValue;
uint256 constant PRECISION = 1e18;

constructor(uint256[] memory rentPrices, uint256 startPremium_, uint256 totalDays) StablePriceOracle(rentPrices) {
startPremium = startPremium_;
endValue = startPremium >> totalDays;
}

uint256 constant PRECISION = 1e18;

/**
* @dev Returns the pricing premium in internal base units.
*/

function _premium(string memory, uint256 expires, uint256) internal view override returns (uint256) {
expires = expires + GRACE_PERIOD;
if (expires > block.timestamp) {
return 0;
}

uint256 elapsed = block.timestamp - expires;
uint256 premium = decayedPremium(elapsed);
if (premium > endValue) {
return premium - endValue;
}
return 0;
}

/**
* @dev Returns the premium price at current time elapsed
* @param elapsed time past since expiry
*/

function decayedPremium(uint256 elapsed) public view returns (uint256) {
/// @dev The half-life of the premium price decay
uint256 secondsInPeriod = 1 days;
uint256 perPeriodDecayPercentWad = FixedPointMathLib.WAD/2;
/// @dev 50% decay per period in wad format
uint256 perPeriodDecayPercentWad = FixedPointMathLib.WAD / 2;
uint256 premium = EDAPrice.currentPrice(startPremium, elapsed, secondsInPeriod, perPeriodDecayPercentWad);
return premium;
}

}

14 changes: 7 additions & 7 deletions src/lib/EDAPrice.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,36 @@ pragma solidity >=0.8.0;
import {Test, console} from "forge-std/Test.sol";
import "solady/utils/FixedPointMathLib.sol";


library EDAPrice {
/// @notice returns the current price of an exponential price decay auction defined by the passed params

/// @dev reverts if perPeriodDecayPercentWad >= 1e18
/// @dev reverts if uint256 secondsInPeriod = 0
/// @dev reverts if startPrice * multiplier overflows
/// @dev reverts if lnWad(percentWadRemainingPerPeriod) * ratio) overflows

/// @param startPrice the starting price of the auction
/// @param secondsElapsed the seconds elapsed since auction start
/// @param secondsInPeriod the seconds over which the price should decay perPeriodDecayPercentWad
/// @param perPeriodDecayPercentWad the percent the price should decay during secondsInPeriod, 100% = 1e18

/// @return price the current auction price

function currentPrice(
uint256 startPrice,
uint256 secondsElapsed,
uint256 secondsInPeriod,
uint256 perPeriodDecayPercentWad
) internal pure returns (uint256) {
// uint256 ratio = FixedPointMathLib.divWadDown(secondsElapsed, secondsInPeriod);
// uint256 percentWadRemainingPerPeriod = FixedPointMathLib.WAD - perPeriodDecayPercentWad;
uint256 ratio = FixedPointMathLib.divWad(secondsElapsed, secondsInPeriod);
uint256 ratio = FixedPointMathLib.divWad(secondsElapsed, secondsInPeriod);
uint256 percentWadRemainingPerPeriod = FixedPointMathLib.WAD - perPeriodDecayPercentWad;

// percentWadRemainingPerPeriod can be safely cast because < 1e18
// ratio can be safely cast because will not overflow unless ratio > int256.max,
// which would require secondsElapsed > int256.max, i.e. > 5.78e76 or 1.8e69 years

int256 multiplier = FixedPointMathLib.powWad(int256(percentWadRemainingPerPeriod), int256(ratio));
uint256 price = (startPrice * uint256(multiplier)) / FixedPointMathLib.WAD;
return price;
}


}
}
54 changes: 0 additions & 54 deletions src/visualization/Price.py

This file was deleted.

0 comments on commit c54efe3

Please sign in to comment.