Published
Edited
Insert cell
Insert cell
Insert cell
sqrtPriceX96 = ethers.BigNumber.from("63855142333622077081404887290401369")
Insert cell
currentPriceInUsdc = 1 / sqrtToPrice(sqrtPriceX96)
Insert cell
allowedPriceX96 = sqrtPriceX96.mul(995).div(1000)
Insert cell
allowedPriceInUsdc = 1 / sqrtToPrice(allowedPriceX96)
Insert cell
allowedPriceX96.toString()
Insert cell
Insert cell
md`
Suggested: ${tickToUsd(274000)} -> ${tickToUsd(270000)}

Huge range: ${usdToTick(1.7484)} -> ${usdToTick(1.1656000000000002)}

Currently: ${tickToUsd(272595)}
`
Insert cell
a = targetPrice * 0.8
Insert cell
b = targetPrice * 1.2
Insert cell
SCALE_FACTOR = 10 ** (18 - 6)
Insert cell
lowerTick = usdToTick(priceHigh)
Insert cell
upperTick = usdToTick(priceLow)
Insert cell
priceLow = average - 6 * sigma
Insert cell
priceHigh = average + 6 * sigma
Insert cell
targetPrice = 1.457
Insert cell
average = {
let sum = 0;
let average;

for (let i = 0; i < prices.length; i++) {
// Calculate percentage change in every dt step
sum = sum + prices[i];
}

average = sum / prices.length;

return average;
}
Insert cell
sigma = standardDeviation(prices)
Insert cell
DURATION = 14
Insert cell
Insert cell
floatReal = {
const startingPrice = coingeckoData["prices"][0][1];
return coingeckoData.prices
.map(([timestamp, price], index) => ({
price: price,
percentage: price / startingPrice,
granularChange:
price /
((index && coingeckoData["prices"][index - 1][1] - 1) || price) -
1,
timestamp: new Date(timestamp)
}))
.sort((a, b) => a.timestamp - b.timestamp);
}
Insert cell
prices = {
let bankPrices = [];

for (let i = 0; i < floatReal.length; i++) {
// Calculate percentage change in every dt step
bankPrices.push(floatReal[i].price);
}
return bankPrices;
}
Insert cell
function standardDeviation(arr, usePopulation = false) {
const mean = arr.reduce((acc, val) => acc + val, 0) / arr.length;
return Math.sqrt(
arr
.reduce((acc, val) => acc.concat((val - mean) ** 2), [])
.reduce((acc, val) => acc + val, 0) /
(arr.length - (usePopulation ? 0 : 1))
);
}
Insert cell
tickToUsd = (tick) => {
const float = 1.0001 ** tick / SCALE_FACTOR;
return 1 / float;
}
Insert cell
function usdToTick(usd) {
const float = 1 / usd;
const tick =
-Math.log(1 / (SCALE_FACTOR * float)) /
(-4 * Math.log(2) - 4 * Math.log(5) + Math.log(73) + Math.log(137));

return Math.ceil(tick / 60) * 60;
}
Insert cell
function sqrtToPrice(sqrtPriceX96) {
const sqrtPrice = ethers.BigNumber.from(sqrtPriceX96);
return (
sqrtPrice
.mul(sqrtPrice)
.mul(ethers.BigNumber.from(10).pow(token0Decimals + precision))
.div(ethers.BigNumber.from(10).pow(token1Decimals))
.div(ethers.BigNumber.from(2).pow(192))
.toNumber() /
10 ** precision
);
}
Insert cell
function priceToSqrt(usdPrice) {
const floatPrice = ethers.BigNumber.from(
Math.floor(usdPrice * 10 ** precision)
);
return;
sqrt(
floatPrice
.mul(ethers.BigNumber.from(2).pow(192))
.mul(ethers.BigNumber.from(10).pow(token1Decimals))
.div(ethers.BigNumber.from(10).pow(token0Decimals + precision))
);
}
Insert cell
ONE = ethers.BigNumber.from(1)
Insert cell
TWO = ethers.BigNumber.from(2)
Insert cell
function sqrt(value) {
const x = ethers.BigNumber.from(value);
const z = x.add(ONE).div(TWO);
const y = x;
while (z.sub(y).isNegative()) {
y = z;
z = x.div(z).add(z).div(TWO);
}
return y;
}
Insert cell
token0Decimals = 6
Insert cell
token1Decimals = 18
Insert cell
precision = 4
Insert cell
ethers = require("https://cdn.ethers.io/lib/ethers-5.0.umd.min.js")
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more