Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expand SqrtPrice domain #143

Merged
merged 4 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions contracts/constants.ral
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const GLOBAL_MAX_TICK = 221818i
const GLOBAL_MIN_TICK = -221818i
const GLOBAL_MAX_TICK = 547_613i
const GLOBAL_MIN_TICK = -GLOBAL_MAX_TICK

const MAX_SWAP_STEPS = 6
const MAX_SWAP_STEPS = 38

const MAX_U256 = 115792089237316195423570985008687907853269984665640564039457584007913129639935

Expand Down
2 changes: 1 addition & 1 deletion contracts/invariant.ral
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ Contract Invariant(
Liquidity{v: 0},
calculateSqrtPrice(index),
feeGrowthOutsideX,
feeGrowthOutsideY,
feeGrowthOutsideY,
0
)
}
Expand Down
78 changes: 42 additions & 36 deletions contracts/math/decimal.ral
Original file line number Diff line number Diff line change
Expand Up @@ -39,59 +39,65 @@ Abstract Contract Decimal() {

assert!(tickIndexAbs <= toU256!(GLOBAL_MAX_TICK), DecimalError.TickOverBounds)

if (tickIndexAbs & 0x1 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1000049998750) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x00001 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1000049998750062496094023) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x2 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1000100000000) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x00002 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1000100000000000000000000) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x4 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1000200010000) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x00004 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1000200010000000000000000) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x8 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1000400060004) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x00008 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1000400060004000100000000) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x10 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1000800280056) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x00010 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1000800280056007000560028) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x20 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1001601200560) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x00020 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1001601200560182043688009) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x40 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1003204964963) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x000040 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1003204964963598014666528) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x80 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1006420201726) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x00080 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1006420201727613920156533) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x100 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1012881622442) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x00100 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1012881622445451097078095) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x200 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1025929181080) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x00200 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1025929181087729343658708) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x400 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1052530684591) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x00400 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1052530684607338948386589) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x800 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1107820842005) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x00800 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1107820842039993613899215) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x1000 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1227267017980) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x01000 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1227267018058200482050503) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x2000 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1506184333421) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x02000 != 0) {
sqrtPrice.v = (sqrtPrice.v * 1506184333613467388107955) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x4000 != 0) {
sqrtPrice.v = (sqrtPrice.v * 2268591246242) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x04000 != 0) {
sqrtPrice.v = (sqrtPrice.v * 2268591246822644826925609) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x8000 != 0) {
sqrtPrice.v = (sqrtPrice.v * 5146506242525) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x08000 != 0) {
sqrtPrice.v = (sqrtPrice.v * 5146506245160322222537991) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x00010000 != 0) {
sqrtPrice.v = (sqrtPrice.v * 26486526504348) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x10000 != 0) {
sqrtPrice.v = (sqrtPrice.v * 26486526531474198664033811) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x00020000 != 0) {
sqrtPrice.v = (sqrtPrice.v * 701536086265529) / FIXED_POINT_DENOMINATOR
if (tickIndexAbs & 0x20000 != 0) {
sqrtPrice.v = (sqrtPrice.v * 701536087702486644953017488) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x40000 != 0) {
sqrtPrice.v = (sqrtPrice.v * 492152882348911033633683861778) / FIXED_POINT_DENOMINATOR
}
if (tickIndexAbs & 0x80000 != 0) {
sqrtPrice.v = (sqrtPrice.v * 242214459604341065650571799093539783) / FIXED_POINT_DENOMINATOR
}

if (tickIndex >= 0i) {
Expand Down
56 changes: 30 additions & 26 deletions contracts/math/log.ral
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
Abstract Contract Log() extends Decimal() {
const LOG2_SCALE = 32
const LOG2_SCALE = 64
const LOG2_DOUBLE_SCALE = LOG2_SCALE *2
const LOG2_SQRT10001 = 309801
const LOG2_NEGATIVE_MAX_LOSE = 300000
const LOG2_MIN_BINARY_POSITION = 15
const LOG2_SQRT10001 = 1330584781654116
const LOG2_NEGATIVE_MAX_LOSE = 1330580000000000 * 7 / 9
const LOG2_MIN_BINARY_POSITION = 46
const LOG2_ONE = 1 << LOG2_SCALE
const LOG2_HALF = LOG2_ONE >> 1
const LOG2_TWO = LOG2_ONE << 1
const LOG2_DOUBLE_ONE = 1 << LOG2_DOUBLE_SCALE
const LOG2_ACCURACY = 1 << (31 - LOG2_MIN_BINARY_POSITION)
const LOG2_ACCURACY = 1 << (63 - LOG2_MIN_BINARY_POSITION)

fn sqrtPriceToX32(val: SqrtPrice) -> U256 {
fn sqrtPriceToX64(val: SqrtPrice) -> U256 {
return val.v * LOG2_ONE / SQRT_PRICE_DENOMINATOR
}

fn log2FloorX32( mut sqrtPriceX32: U256) -> U256 {
fn log2FloorX64( mut sqrtPriceX64: U256) -> U256 {
let mut msb = 0

if (sqrtPriceX32 >= 1 << 32) {
sqrtPriceX32 = sqrtPriceX32 >> 32
if (sqrtPriceX64 >= 1 << 64) {
sqrtPriceX64 = sqrtPriceX64 >> 64
msb = msb | 64
}
if (sqrtPriceX64 >= 1 << 32) {
sqrtPriceX64 = sqrtPriceX64 >> 32
msb = msb | 32
}
if (sqrtPriceX32 >= 1 << 16) {
sqrtPriceX32 = sqrtPriceX32 >> 16
if (sqrtPriceX64 >= 1 << 16) {
sqrtPriceX64 = sqrtPriceX64 >> 16
msb = msb | 16
}
if (sqrtPriceX32 >= 1 << 8) {
sqrtPriceX32 = sqrtPriceX32 >> 8
if (sqrtPriceX64 >= 1 << 8) {
sqrtPriceX64 = sqrtPriceX64 >> 8
msb = msb | 8
}
if (sqrtPriceX32 >= 1 << 4) {
sqrtPriceX32 = sqrtPriceX32 >> 4
if (sqrtPriceX64 >= 1 << 4) {
sqrtPriceX64 = sqrtPriceX64 >> 4
msb = msb | 4
}
if (sqrtPriceX32 >= 1 << 2) {
sqrtPriceX32 = sqrtPriceX32 >> 2
if (sqrtPriceX64 >= 1 << 2) {
sqrtPriceX64 = sqrtPriceX64 >> 2
msb = msb | 2
}
if (sqrtPriceX32 >= 1 << 1) {
if (sqrtPriceX64 >= 1 << 1) {
msb = msb | 1
}

Expand All @@ -57,16 +61,16 @@ Abstract Contract Log() extends Decimal() {
return accurateTick - substrahend
}
}

fn log2IterativeApproximationX32(mut sqrtPriceX32: U256) -> (Bool, U256) {
fn log2IterativeApproximationX64(mut sqrtPriceX64: U256) -> (Bool, U256) {
let mut sign = true
if (sqrtPriceX32 < LOG2_ONE) {
if (sqrtPriceX64 < LOG2_ONE) {
sign = false
sqrtPriceX32 = (LOG2_DOUBLE_ONE / (sqrtPriceX32 + 1))
sqrtPriceX64 = (LOG2_DOUBLE_ONE / (sqrtPriceX64 + 1))
}
let log2Floor = log2FloorX32( sqrtPriceX32 >> LOG2_SCALE)
let log2Floor = log2FloorX64( sqrtPriceX64 >> LOG2_SCALE)
let mut result = log2Floor << LOG2_SCALE
let mut y = sqrtPriceX32 >> log2Floor
let mut y = sqrtPriceX64 >> log2Floor

if (y == LOG2_ONE) {
return sign, result
Expand All @@ -85,9 +89,9 @@ Abstract Contract Log() extends Decimal() {

pub fn getTickAtSqrtPrice(sqrtPrice: SqrtPrice, tickSpacing: U256) -> I256 {
assert!(sqrtPrice.v <= MAX_SQRT_PRICE && sqrtPrice.v >= MIN_SQRT_PRICE, LogError.SqrtPriceOutOfRange)
let sqrtPriceX32 = sqrtPriceToX32(sqrtPrice)
let sqrtPriceX64 = sqrtPriceToX64(sqrtPrice)

let (log2Sign, log2SqrtPrice) = log2IterativeApproximationX32(sqrtPriceX32)
let (log2Sign, log2SqrtPrice) = log2IterativeApproximationX64(sqrtPriceX64)

let mut absFloorTick = 0
let mut nearerTick = 0i
Expand Down
6 changes: 3 additions & 3 deletions contracts/math/types.ral
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ struct SqrtPrice{
}
const SQRT_PRICE_SCALE = 24
const SQRT_PRICE_DENOMINATOR = 10**SQRT_PRICE_SCALE
const MAX_SQRT_PRICE = 65535383934512647000000000000
const MIN_SQRT_PRICE = 15258932000000000000
const MAX_SQRT_PRICE = 777446803530032965262892619622664451
const MIN_SQRT_PRICE = 1286261639329

struct Liquidity{
mut v: U256
Expand Down Expand Up @@ -36,6 +36,6 @@ const TOKEN_AMOUNT_DENOMINATOR = 10**TOKEN_AMOUNT_SCALE
struct FixedPoint{
mut v: U256
}
const FIXED_POINT_SCALE = 12
const FIXED_POINT_SCALE = 24
const FIXED_POINT_DENOMINATOR = 10**FIXED_POINT_SCALE

3 changes: 2 additions & 1 deletion src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export enum VMError {
ArithmeticError = 'ArithmeticError',
OutOfGas = 'OutOfGas',
NotEnoughBalance = 'Not enough approved balance for address',
MaxStoredAssets = 'max token number is 8'
MaxStoredAssets = 'max token number is 8',
VMExecutionError = 'VM execution error: Assertion Failed in Contract'
}

export const INVARIANT_ADDRESS = {
Expand Down
Loading
Loading