Skip to content

Commit

Permalink
Merge pull request #51 from invariant-labs/refactor-get-next-sqrt-pri…
Browse files Browse the repository at this point in the history
…ce-y-down

Refactor getNextSqrtPriceYDown
  • Loading branch information
Sniezka1927 authored Apr 13, 2024
2 parents adf4158 + edfa1c1 commit f0e8f30
Show file tree
Hide file tree
Showing 7 changed files with 1,659 additions and 1,386 deletions.
10 changes: 5 additions & 5 deletions contracts/math/clamm.ral
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,13 @@ Contract CLAMM(uints: Uints) extends Log(uints){
y: U256,
addY: Bool
) -> U256 {
let numerator = rescale(y, TokenAmountScale, SqrtPriceScale)
let denominator = rescale(liquidity, LiquidityScale, SqrtPriceScale)

let numerator = uints.bigRescale(y, TokenAmountScale, SqrtPriceScale)
let denominator = uints.bigRescale(liquidity, LiquidityScale, SqrtPriceScale)
if (addY) {
return startingSqrtPrice + div(numerator, denominator)
return startingSqrtPrice + uints.toU256(uints.bigDiv512(numerator, denominator, one(SqrtPriceScale)))
} else {
return startingSqrtPrice - divUp(numerator, denominator)
return startingSqrtPrice - uints.toU256(uints.bigDivUp512(numerator, denominator, one(SqrtPriceScale)))
}

}
Expand Down
118 changes: 51 additions & 67 deletions contracts/math/uints.ral
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,55 @@ Contract Uints () {
return bigDivWrapper(result, b, 1)
}

pub fn bigDiv512(dividend: U512, divisor: U512, divisorDenominator: U256) -> U512 {
let mut q = U512 { higher: 0, lower: 0 }
let mut r = U512 { higher: 0, lower: 0 }

assert!(!isZero(divisor), ArithmeticError.DivNotPositiveDivisor)
assert!(divisorDenominator != 0, ArithmeticError.DivNotPositiveDenominator)

let extDividend = bigMul(dividend, divisorDenominator)
let mut uHigh = extDividend.higher
let mut uLow = extDividend.lower

let v = divisor

let mut j = 512
while (j > 0) {
j = j - 1
r = bigShl(r, 1)

if (((uHigh >> 255) & 1) != 0) {
r = bitOrLower(r, 1)
}

uHigh = uHigh << 1

if ((uLow >> 255) != 0) {
uHigh = uHigh | 1
}
uLow = uLow << 1

if (isGreaterEqual(r,v)) {
r = bigSub512(r, v)
if (j >= 256) {
q.higher = q.higher | (1 << (j - 256))
} else {
q.lower = q.lower | (1 << j)
}
}
}

return q
}

pub fn bigDivUp512(dividend: U512, divisor: U512, divisorDenominator: U256) -> U512 {
let mut result = bigMul(dividend, divisorDenominator)
result = bigAdd512(result, divisor)
result.lower = result.lower - 1
return bigDiv512(result, divisor, 1)
}

pub fn bigMul256(a: U256, b: U256) -> U512 {
let aLower = low128(a)
let aHigher = high128(a)
Expand Down Expand Up @@ -208,9 +257,8 @@ Contract Uints () {
pub fn overflowingAdd(a: U256, b: U256) -> (U256, U256) {
if (MaxU256 - a >= b) {
return a + b, 0
}

if (a > b) {
}
if (a > b){
return a - (MaxU256 - b) - 1, 1
} else {
return b - (MaxU256 - a) - 1, 1
Expand Down Expand Up @@ -293,69 +341,5 @@ Contract Uints () {
pub fn isZero(a: U512) -> Bool {
return a.higher == 0 && a.lower == 0
}

pub fn bigDiv512(dividend: U512, divisor: U512, divisorDenominator: U256) -> U512 {
let mut q = U512 { higher: 0, lower: 0 }
let mut r = U512 { higher: 0, lower: 0 }

assert!(!isZero(divisor), ArithmeticError.DivNotPositiveDivisor)
assert!(divisorDenominator > 0, ArithmeticError.DivNotPositiveDenominator)

if (dividend.higher == 0) {
let mulResult = bigMul256(dividend.lower, divisorDenominator)

if (mulResult.higher == 0) {
return U512 { higher: 0, lower: mulResult.lower / divisor.lower }
}
let divResult = bigDiv512(mulResult, divisor, 1)
return divResult
}

let mut uHigh = dividend.higher * divisorDenominator
let mut uLow = dividend.lower * divisorDenominator
let v = divisor

for (let mut j = 511; j > 0; j = j - 1) {
r.lower = r.lower << 1 | (r.higher >> 255)
r.higher = r.higher << 1

if (((uHigh >> 255) & 1) != 0) {
r.lower = r.lower | 1
}

uHigh = uHigh << 1

if ((uLow >> 255) != 0) {
uHigh = uHigh | 1
}
uLow = uLow << 1

if (r.higher > v.higher || (r.higher == v.higher && r.lower >= v.lower)) {
let mut borrow = 1

if (r.lower >= v.lower) {
borrow = 0
}

r.lower = r.lower - (v.lower + borrow)
r.higher = r.higher - (v.higher + borrow)

if (j >= 256) {
q.higher = q.higher | (1 << (j - 256))
} else {
q.lower = q.lower | (1 << j)
}
}
}

return q
}

pub fn bigDivUp512(dividend: U512, divisor: U512, divisorDenominator: U256) -> U512 {
let mut result = bigMul(dividend, divisorDenominator)
result = bigAdd512(result, divisor)
result.lower = result.lower - 1
return bigDiv512(result, divisor, 1)
}
}

Loading

0 comments on commit f0e8f30

Please sign in to comment.