Skip to content

Instantly share code, notes, and snippets.

@danoctavian
Created February 16, 2021 08:33
Show Gist options
  • Save danoctavian/c0449eb5ab2f19d089396a1dac2250b5 to your computer and use it in GitHub Desktop.
Save danoctavian/c0449eb5ab2f19d089396a1dac2250b5 to your computer and use it in GitHub Desktop.
function calculateNxmForEth (Vt0, ethIn, mcrEth, stepSize) {
let previousV;
let currentV = Vt0;
let previousPrice;
let totalTokens = Decimal(0);
while (ethIn.gt('0')) {
const mcrPercentage = currentV.div(mcrEth);
const currentPrice = getSpotPrice(mcrPercentage, mcrEth);
if (previousPrice && previousV) {
const averagePrice = currentPrice.add(previousPrice).div(2);
const deltaTokens = currentV.sub(previousV).div(averagePrice);
totalTokens = totalTokens.add(deltaTokens);
}
previousV = currentV;
previousPrice = currentPrice;
currentV = currentV.add(stepSize);
ethIn = ethIn.sub(stepSize);
}
return totalTokens;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment