Last active
April 25, 2023 17:56
-
-
Save ryley-o/23cf44a7f3ff80079ad8f1f32d90ef6c to your computer and use it in GitHub Desktop.
half-life calc for DA Exp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { BigNumber, utils } from "ethers"; | |
// SUMMARY | |
// This script calculates the half-life in seconds for a given | |
// startPrice, endPrice, startTime, and endTime. | |
// The calculation accounts for the Art Blocks pseudo-exponential pricing curve | |
// used in DAExp minters. | |
// INPUTS | |
const startPrice = utils.parseUnits("10", "ether"); | |
const endPrice = utils.parseUnits("0.8", "ether"); | |
const startTime = BigNumber.from("1681530920"); // 2023-04-14 20:55:20 | |
const endTime = startTime.add(3600); // 1 hour later | |
// calc intermediate terms | |
// use EXTRA_DECIMALS to gain precision for decimal values (BigNumber doesn't support decimals) | |
const EXTRA_DECIMALS = 10 ** 5; | |
const priceRatio = | |
startPrice.mul(EXTRA_DECIMALS).div(endPrice).toNumber() / EXTRA_DECIMALS; | |
const completedHalfLives = Math.floor(Math.log(priceRatio) / Math.log(2)); | |
const _denominator = BigNumber.from(2).pow(completedHalfLives); | |
const y1 = startPrice.div(_denominator); | |
const y2 = y1.div(2); | |
const zNumerator = endPrice.sub(y1); | |
const zDenominator = y2.sub(y1); | |
const z = | |
zNumerator.mul(EXTRA_DECIMALS).div(zDenominator).toNumber() / EXTRA_DECIMALS; | |
// half-life is a fallout | |
const halfLifeSeconds = Math.round( | |
endTime.sub(startTime).toNumber() / | |
((completedHalfLives + 1) * z + completedHalfLives * (1 - z)) | |
); | |
console.log("halfLifeSeconds:", halfLifeSeconds.toString()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment