-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a14d9f8
commit f77bdbd
Showing
3 changed files
with
75 additions
and
30 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import BigNumber from "bignumber.js" // Import BigNumber.js for precise calculations | ||
|
||
// Configure BigNumber.js for high precision | ||
BigNumber.config({ EXPONENTIAL_AT: 999999, DECIMAL_PLACES: 40 }) | ||
|
||
// Function to calculate sqrtPriceX96 | ||
export function encodePriceSqrt(reserve1: string, reserve0: string): bigint { | ||
return BigInt( | ||
new BigNumber(reserve1) // Convert reserve1 to a BigNumber | ||
.div(reserve0) // Divide by reserve0 | ||
.sqrt() // Take the square root of the result | ||
.multipliedBy(new BigNumber(2).pow(96)) // Multiply by 2^96 | ||
.integerValue(BigNumber.ROUND_DOWN) // Round down to the nearest integer | ||
.toString() // Convert to string | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import BigNumber from "bignumber.js" | ||
|
||
export function getPrice(sqrtPriceX96: BigNumber) { | ||
// Calculate price of token0 in terms of token1 | ||
const buyOneOfToken0 = sqrtPriceX96.dividedBy(new BigNumber(2).pow(96)).pow(2) | ||
|
||
// Calculate price of token1 in terms of token0 | ||
const buyOneOfToken1 = new BigNumber(1).dividedBy(buyOneOfToken0) | ||
|
||
// Convert to smallest unit (wei) | ||
const buyOneOfToken0Wei = buyOneOfToken0.multipliedBy(new BigNumber(10).pow(18)).integerValue(BigNumber.ROUND_DOWN).toString(10) | ||
const buyOneOfToken1Wei = buyOneOfToken1.multipliedBy(new BigNumber(10).pow(18)).integerValue(BigNumber.ROUND_DOWN).toString(10) | ||
|
||
return { | ||
priceToken0InToken1: buyOneOfToken0, // Price of token0 in terms of token1 | ||
priceToken1InToken0: buyOneOfToken1, // Price of token1 in terms of token0 | ||
priceToken0InToken1Wei: buyOneOfToken0Wei, // Price of token0 in lowest decimal (wei) | ||
priceToken1InToken0Wei: buyOneOfToken1Wei, // Price of token1 in lowest decimal (wei) | ||
} | ||
} |