Skip to content

Commit

Permalink
Align decimal math functions
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciech-cichocki committed Feb 26, 2024
1 parent e0b60c9 commit ef40d8e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 29 deletions.
18 changes: 9 additions & 9 deletions contracts/math/clamm.ral
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,12 @@ Contract CLAMM() extends Log(){

let nominator = mul(deltaSqrtPrice, liquidity, liquidityScale)
if (roundingUp == true) {
return divValuesToTokenUp(
return divToTokenUp(
nominator,
mul(sqrtPriceA, sqrtPriceB, sqrtPriceScale)
)
} else {
return divValuesToToken(
return divToToken(
nominator,
mulUp(sqrtPriceA, sqrtPriceB, sqrtPriceScale)
)
Expand Down Expand Up @@ -173,7 +173,7 @@ Contract CLAMM() extends Log(){
let sqrtPriceScale = getSqrtPriceScale()
let tokenAmountScale = getTokenAmountScale()

let deltaSqrtPrice = fromDecimalToValue(liquidity, liquidityScale, sqrtPriceScale)
let deltaSqrtPrice = rescale(liquidity, liquidityScale, sqrtPriceScale)

let mut denominator = 0
if (addX) {
Expand All @@ -182,9 +182,9 @@ Contract CLAMM() extends Log(){
denominator = deltaSqrtPrice - mul(startingSqrtPrice, x, tokenAmountScale)
}

return divValuesToTokenUp(
return divToTokenUp(
mulUp(startingSqrtPrice, liquidity, liquidityScale),
fromDecimalToValue(denominator, sqrtPriceScale, tokenAmountScale)
rescale(denominator, sqrtPriceScale, tokenAmountScale)
)
}
pub fn getNextSqrtPriceYDown(
Expand All @@ -197,13 +197,13 @@ Contract CLAMM() extends Log(){
let sqrtPriceScale = getSqrtPriceScale()
let tokenAmountScale = getTokenAmountScale()

let numerator = fromDecimalToValue(y, tokenAmountScale, sqrtPriceScale)
let denominator = fromDecimalToValue(liquidity, liquidityScale, sqrtPriceScale)
let numerator = rescale(y, tokenAmountScale, sqrtPriceScale)
let denominator = rescale(liquidity, liquidityScale, sqrtPriceScale)

if (addY) {
return startingSqrtPrice + divValues(numerator, denominator)
return startingSqrtPrice + div(numerator, denominator)
} else {
return startingSqrtPrice - divValuesUp(numerator, denominator)
return startingSqrtPrice - divUp(numerator, denominator)
}

}
Expand Down
32 changes: 12 additions & 20 deletions contracts/math/decimal.ral
Original file line number Diff line number Diff line change
Expand Up @@ -57,22 +57,19 @@ Abstract Contract Decimal() {
return (l * r + almostOne(rScale)) / one(rScale)
}

// SqrtPrice related
pub fn divValuesToTokenUp(l: U256, r: U256) -> U256 {
pub fn divToTokenUp(l: U256, r: U256) -> U256 {
return (((l * one(getSqrtPriceScale()) + (r - 1)) / r) + almostOne(getSqrtPriceScale())) / one(getSqrtPriceScale())
}

// SqrtPrice related
pub fn divValuesToToken(l: U256, r: U256) -> U256 {
pub fn divToToken(l: U256, r: U256) -> U256 {
return ((l * one(getSqrtPriceScale())) / r) / one(getSqrtPriceScale())
}

// SqrtPrice related
pub fn divValuesUp(l: U256, r: U256) -> U256 {
pub fn divUp(l: U256, r: U256) -> U256 {
return l * one(getSqrtPriceScale()) + (r - 1) / r
}

pub fn divValues(l: U256, r: U256) -> U256 {
pub fn div(l: U256, r: U256) -> U256 {
return l * one(getSqrtPriceScale()) / r
}

Expand Down Expand Up @@ -103,10 +100,8 @@ Abstract Contract Decimal() {

let fixedPointScale = getFixedPointScale()
let sqrtPriceScale = getSqrtPriceScale()

let mut sqrtPrice = one(fixedPointScale)


let mut sqrtPrice = one(fixedPointScale)

if (tick & 0x1 != 0) {
sqrtPrice = (sqrtPrice * 1000049998750) / (10 ** fixedPointScale)
Expand Down Expand Up @@ -164,24 +159,23 @@ Abstract Contract Decimal() {
}

if (tickIndex >= 0i) {
return fromDecimalToValue(sqrtPrice, fixedPointScale, sqrtPriceScale)
return rescale(sqrtPrice, fixedPointScale, sqrtPriceScale)
} else {
let intermediate = one(sqrtPriceScale) / sqrtPrice
return fromDecimalToValue(intermediate, fixedPointScale, sqrtPriceScale)
return rescale(intermediate, fixedPointScale, sqrtPriceScale)
}
}

pub fn fromDecimalToValue(from: U256, fromScale: U256, expectedScale: U256) -> U256 {
pub fn rescale(fromValue: U256, fromScale: U256, expectedScale: U256) -> U256 {
if (expectedScale > fromScale) {
let multiplierScale = expectedScale - fromScale
return from * (10 ** multiplierScale)
return fromValue * (10 ** multiplierScale)
} else {
let denominatorScale = fromScale - expectedScale
return from / (10 ** denominatorScale)
return fromValue / (10 ** denominatorScale)
}
}

// FeeGrowth related
pub fn feeGrowthFromFee(liquidity: U256, fee: U256) -> U256 {
return fee * one(getFeeGrowthScale()) * one(getLiquidityScale()) / liquidity
}
Expand All @@ -203,7 +197,6 @@ Abstract Contract Decimal() {
}
}

// // FeeGrowth related
pub fn calculateFeeGrowthInside(
tickLowerIndex: I256,
tickLowerFeeGrowthOutsideX: U256,
Expand Down Expand Up @@ -245,8 +238,7 @@ Abstract Contract Decimal() {
return feeGrowthInsideX, feeGrowthInsideY
}

// FeeGrowth related
pub fn toFee(fee: U256, liquidity: U256) -> U256 {
return fee * liquidity / (10 ** (getFeeGrowthScale() + getLiquidityScale()))
pub fn toFee(feeGrowth: U256, liquidity: U256) -> U256 {
return feeGrowth * liquidity / (10 ** (getFeeGrowthScale() + getLiquidityScale()))
}
}

0 comments on commit ef40d8e

Please sign in to comment.