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

Refactor uints #50

Merged
merged 6 commits into from
Apr 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
5 changes: 4 additions & 1 deletion contracts/collections/pools.ral
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Contract Pools(
poolTemplateContractId: ByteVec,
clamm: CLAMM,
uints: Uints,
mut invariantId: Address,
mut positionsId: Address,
mut swapUtilsId: Address,
Expand Down Expand Up @@ -45,7 +46,9 @@ Contract Pools(
lastTimestamp: currentTimestamp,
feeReceiver: feeReceiver
},
clamm)
clamm,
uints
)
let _ = copyCreateSubContract!{originalCaller -> ALPH: 1 alph}(
poolKey,
poolTemplateContractId,
Expand Down
6 changes: 4 additions & 2 deletions contracts/collections/positions.ral
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ Contract Positions(
positionsCounterContractId: ByteVec,
mut invariantId: Address,
mut areAdminsSet: Bool,
clammContract: CLAMM
clammContract: CLAMM,
uints: Uints
) {
enum PositionsError {
NotAdmin = 500
Expand Down Expand Up @@ -58,7 +59,8 @@ Contract Positions(
owner: originalCaller
},
true,
clammContract
clammContract,
uints
)
let _ = copyCreateSubContract!{originalCaller -> ALPH: 1 alph}(
toByteVec!(length + 1),
Expand Down
98 changes: 70 additions & 28 deletions contracts/math/clamm.ral
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ Contract CLAMM(uints: Uints) extends Log() {
let amountAfterFee = mul(amount, (one(PercentageScale) - fee), PercentageScale)

if (xToY) {
amountIn = getDeltaX(targetSqrtPrice, currentSqrtPrice, liquidity, true)
amountIn = uints.unwrapU256(getDeltaX(targetSqrtPrice, currentSqrtPrice, liquidity, true))
} else {
amountIn = getDeltaY(currentSqrtPrice, targetSqrtPrice, liquidity, true)
amountIn = uints.unwrapU256(getDeltaY(currentSqrtPrice, targetSqrtPrice, liquidity, true))
}

if (amountAfterFee >= amountIn) {
Expand All @@ -43,9 +43,9 @@ Contract CLAMM(uints: Uints) extends Log() {
}
} else {
if (xToY) {
amountOut = getDeltaY(targetSqrtPrice, currentSqrtPrice, liquidity, false)
amountOut = uints.unwrapU256(getDeltaY(targetSqrtPrice, currentSqrtPrice, liquidity, false))
} else {
amountOut = getDeltaX(currentSqrtPrice, targetSqrtPrice, liquidity, false)
amountOut = uints.unwrapU256(getDeltaX(currentSqrtPrice, targetSqrtPrice, liquidity, false))
}

if (amount >= amountOut) {
Expand All @@ -59,17 +59,17 @@ Contract CLAMM(uints: Uints) extends Log() {

if (xToY) {
if (notMax || !byAmountIn) {
amountIn = getDeltaX(nextSqrtPrice, currentSqrtPrice, liquidity, true)
amountIn = uints.unwrapU256(getDeltaX(nextSqrtPrice, currentSqrtPrice, liquidity, true))
}
if (notMax || byAmountIn) {
amountOut = getDeltaY(nextSqrtPrice, currentSqrtPrice, liquidity, false)
amountOut = uints.unwrapU256(getDeltaY(nextSqrtPrice, currentSqrtPrice, liquidity, false))
}
} else {
if (notMax|| !byAmountIn) {
amountIn = getDeltaY(currentSqrtPrice, nextSqrtPrice, liquidity, true)
amountIn = uints.unwrapU256(getDeltaY(currentSqrtPrice, nextSqrtPrice, liquidity, true))
}
if (notMax || byAmountIn) {
amountOut = getDeltaX(currentSqrtPrice, nextSqrtPrice, liquidity, false)
amountOut = uints.unwrapU256(getDeltaX(currentSqrtPrice, nextSqrtPrice, liquidity, false))
}
}

Expand Down Expand Up @@ -98,15 +98,20 @@ Contract CLAMM(uints: Uints) extends Log() {
sqrtPriceB: U256,
liquidity: U256,
roundingUp: Bool
) -> U256 {
) -> ResultU256 {
let mut deltaSqrtPrice = 0u
if (sqrtPriceA > sqrtPriceB) {
deltaSqrtPrice = sqrtPriceA - sqrtPriceB
} else {
deltaSqrtPrice = sqrtPriceB - sqrtPriceA
}

let nominator = uints.bigMulDiv256(deltaSqrtPrice, liquidity, one(LiquidityScale))
let wrappedResult = uints.bigMulDiv256(deltaSqrtPrice, liquidity, one(LiquidityScale))
if (wrappedResult.error != 0) {
return ResultU256 { value: u256Max!(), error: wrappedResult.error }
}
let nominator = uints.unwrapU512(wrappedResult)

if (roundingUp) {
let denominator = mul(sqrtPriceA, sqrtPriceB, SqrtPriceScale)
return uints.bigDivToTokenUp(nominator, denominator, one(SqrtPriceScale))
Expand All @@ -121,24 +126,51 @@ Contract CLAMM(uints: Uints) extends Log() {
sqrtPriceB: U256,
liquidity: U256,
roundingUp: Bool
) -> U256 {
) -> ResultU256 {
let mut deltaSqrtPrice = 0u
if (sqrtPriceA > sqrtPriceB) {
deltaSqrtPrice = sqrtPriceA - sqrtPriceB
} else {
deltaSqrtPrice = sqrtPriceB - sqrtPriceA
}

let mut wrappedResult = ResultU512 { value: U512 { higher: 0, lower: 0 }, error: 0}
let u256Max = u256Max!()
let mut result = U512 { lower: 0, higher: 0 }

if (roundingUp) {
result = uints.bigMulDiv256(deltaSqrtPrice, liquidity, one(LiquidityScale))
result = uints.bigAdd512(result, uints.toU512(almostOne(SqrtPriceScale)))
result = uints.bigDiv512(result, one(SqrtPriceScale), 1)
wrappedResult = uints.bigMulDiv256(deltaSqrtPrice, liquidity, one(LiquidityScale))
if (wrappedResult.error != 0) {
return ResultU256 { value: u256Max, error: wrappedResult.error }
}
result = uints.unwrapU512(wrappedResult)

wrappedResult = uints.bigAdd512(result, uints.toU512(almostOne(SqrtPriceScale)))
if (wrappedResult.error != 0) {
return ResultU256 { value: u256Max, error: wrappedResult.error }
}
result = uints.unwrapU512(wrappedResult)

wrappedResult = uints.bigDiv(result, one(SqrtPriceScale), 1)
if (wrappedResult.error != 0) {
return ResultU256 { value: u256Max, error: wrappedResult.error }
}
result = uints.unwrapU512(wrappedResult)

return uints.toU256(result)
} else {
result = uints.bigMulDiv256(deltaSqrtPrice, liquidity, one(LiquidityScale))
result = uints.bigDiv512(result, one(SqrtPriceScale), 1)
wrappedResult = uints.bigMulDiv256(deltaSqrtPrice, liquidity, one(LiquidityScale))
if (wrappedResult.error != 0) {
return ResultU256 { value: u256Max, error: wrappedResult.error }
}
result = uints.unwrapU512(wrappedResult)

wrappedResult = uints.bigDiv(result, one(SqrtPriceScale), 1)
if (wrappedResult.error != 0) {
return ResultU256 { value: u256Max, error: wrappedResult.error }
}
result = uints.unwrapU512(wrappedResult)

return uints.toU256(result)
}
}
Expand Down Expand Up @@ -226,33 +258,33 @@ Contract CLAMM(uints: Uints) extends Log() {
let mut updateLiquidity = false

if (currentTickIndex < lowerTick) {
amountX = getDeltaX(
amountX = uints.unwrapU256(getDeltaX(
sqrtPriceFromTick(lowerTick),
sqrtPriceFromTick(upperTick),
liquidityDelta,
liquiditySign
)
))
} else if (currentTickIndex < upperTick) {
amountX = getDeltaX(
amountX = uints.unwrapU256(getDeltaX(
currentSqrtPrice,
sqrtPriceFromTick(upperTick),
liquidityDelta,
liquiditySign
)
amountY = getDeltaY(
))
amountY = uints.unwrapU256(getDeltaY(
sqrtPriceFromTick(lowerTick),
currentSqrtPrice,
liquidityDelta,
liquiditySign
)
))
updateLiquidity = true
} else {
amountY = getDeltaY(
amountY = uints.unwrapU256(getDeltaY(
sqrtPriceFromTick(lowerTick),
sqrtPriceFromTick(upperTick),
liquidityDelta,
liquiditySign
)
))
}

return amountX, amountY, updateLiquidity
Expand Down Expand Up @@ -331,13 +363,23 @@ Contract CLAMM(uints: Uints) extends Log() {
return true
}

pub fn toFee(feeGrowth: U256, liquidity: U256) -> U256 {
let fee = uints.bigMulDiv256(feeGrowth, liquidity, one(FeeGrowthScale + LiquidityScale))
pub fn toFee(feeGrowth: U256, liquidity: U256) -> ResultU256 {
let wrappedResult = uints.bigMulDiv256(feeGrowth, liquidity, one(FeeGrowthScale + LiquidityScale))
if (wrappedResult.error != 0) {
return ResultU256 { value: u256Max!(), error: wrappedResult.error }
}
let fee = uints.unwrapU512(wrappedResult)

return uints.toU256(fee)
}

pub fn feeGrowthFromFee(liquidity: U256, fee: U256) -> U256 {
let feeGrowth = uints.bigMulDiv256(fee, one(LiquidityScale + FeeGrowthScale), liquidity)
pub fn feeGrowthFromFee(liquidity: U256, fee: U256) -> ResultU256 {
let wrappedResult = uints.bigMulDiv256(fee, one(LiquidityScale + FeeGrowthScale), liquidity)
if (wrappedResult.error != 0) {
return ResultU256 { value: u256Max!(), error: wrappedResult.error }
}
let feeGrowth = uints.unwrapU512(wrappedResult)

return uints.toU256(feeGrowth)
}
}
Loading
Loading