Skip to content

Commit

Permalink
feat: u256*u256 in u256 package
Browse files Browse the repository at this point in the history
chore: remove comments
  • Loading branch information
r3v4s committed Mar 7, 2024
1 parent cb8d9f5 commit 4dc5b34
Show file tree
Hide file tree
Showing 11 changed files with 123 additions and 243 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func TestPositionMintBarBazInRangeMore(t *testing.T) {
shouldEQ(t, tokenId, 2)
shouldEQ(t, liquidity, "447321")
shouldEQ(t, amount0, "50")
shouldEQ(t, amoutn1, "1000")
shouldEQ(t, amoutn1, "999")

std.TestSkipHeights(3)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package uint512
package uint256

const (
MAX_UINT256 = "115792089237316195423570985008687907853269984665640564039457584007913129639935"
Expand Down
98 changes: 98 additions & 0 deletions packages/big/uint256/uint256_overflow_calculation.gno
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
// REF: https://github.com/Uniswap/solidity-lib/blob/master/contracts/libraries/FullMath.sol
package uint256

func fullMul(
x Uint,
y Uint,
) (Uint, Uint) { // l, h
mm := new(*Uint).MulMod(x, y, UnsafeFromDecimal(MAX_UINT256))

l := new(Uint).Mul(x, y)
h := new(Uint).Sub(mm, l)

if mm.Lt(l) {
h = new(Uint).Sub(h, One())
}

return l, h
}

func fullDiv(
l Uint,
h Uint,
d Uint,
) Uint {
// uint256 pow2 = d & -d;
// d
_negD := new(Uint).Neg(d)
pow2 := new(Uint).And(d, _negD)
d = new(Uint).Div(d, pow2)
l = new(Uint).Div(l, pow2)

_negPow2 := new(Uint).Neg(pow2)

value1 := new(Uint).Div(_negPow2, pow2) // (-pow2) / pow2
value2 := new(Uint).UnsafeAdd(value1, One()) // (-pow2) / pow2 + 1)
value3 := new(Uint).Mul(h, value2) // h * ((-pow2) / pow2 + 1);
l = new(Uint).UnsafeAdd(l, value3)

r := One()
for i := 0; i < 8; i++ {
value1 := new(Uint).Mul(d, r) // d * r
value2 := new(Uint).Sub(NewUint(2), value1) // 2 - ( d * r )
r = new(Uint).Mul(r, value2) // r *= 2 - d * r;
}
res := new(Uint).Mul(l, r)
return res
}

func MulDiv(
x Uint,
y Uint,
d Uint,
) Uint {
l, h := fullMul(x, y)
mm := new(Uint).MulMod(x, y, d)

if mm.Gt(l) {
h = new(Uint).Sub(h, One())
}
l = new(Uint).Sub(l, mm)

if h.IsZero() {
return new(Uint).Div(l, d)
}

if !(h.Lt(d)) {
panic("FULLDIV_OVERFLOW")
}

return fullDiv(l, h, d)
}

func DivRoundingUp(
x Uint,
y Uint,
) Uint {
div := new(Uint).Div(x, y)

mod := new(Uint).Mod(x, y)
return new(Uint).Add(div, gt(mod, Zero()))
}

// HELPERs
func lt(x, y Uint) Uint {
if x.Lt(y) {
return One()
} else {
return Zero()
}
}

func gt(x, y Uint) Uint {
if x.Gt(y) {
return One()
} else {
return Zero()
}
}
1 change: 0 additions & 1 deletion packages/big/uint256/uint256_test.gno
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ func TestFuncs(t *testing.T) {
y := UnsafeFromDecimal("130406999485845074795897568971")

z := new(Uint).Add(x, y)
println("z:", z.ToString()) // 130486188034278082001322316149

if z.ToString() != "130486188034278082001322316149" {
t.Error("Expected 130486188034278082001322316149, got ", z.ToString())
Expand Down
121 changes: 0 additions & 121 deletions packages/big/uint512/uint512.gno

This file was deleted.

37 changes: 0 additions & 37 deletions packages/big/uint512/uint512_test.gno

This file was deleted.

16 changes: 0 additions & 16 deletions pool/_RPC_dry.gno
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,9 @@ func DrySwap(
liquidity: cache.liquidityStart,
}
}
// println("state.tick:", state.tick)
// println("IN state.sqrtPriceX96:", state.sqrtPriceX96.ToString())

// println("state.sqrtPriceX96:", state.sqrtPriceX96.ToString())
// continue swapping as long as we haven't used the entire input/output and haven't reached the price limit
for !(state.amountSpecifiedRemaining.IsZero()) && !(state.sqrtPriceX96.Eq(sqrtPriceLimitX96)) {
// println("state.amountSpecifiedRemaining:", state.amountSpecifiedRemaining.ToString())
var step StepComputations
step.sqrtPriceStartX96 = state.sqrtPriceX96

Expand Down Expand Up @@ -131,25 +127,13 @@ func DrySwap(
sqrtRatioTargetX96 = step.sqrtPriceNextX96
}

// println("state.sqrtPriceX96:", state.sqrtPriceX96.ToString())
// println("sqrtRatioTargetX96:", sqrtRatioTargetX96.ToString())
// println("state.liquidity:", state.liquidity.ToString())
// println("state.amountSpecifiedRemaining:", state.amountSpecifiedRemaining.ToString())
// println("pool.fee:", pool.fee)
state.sqrtPriceX96, step.amountIn, step.amountOut, step.feeAmount = swapMathComputeSwapStep(
state.sqrtPriceX96,
sqrtRatioTargetX96,
state.liquidity,
state.amountSpecifiedRemaining,
uint64(pool.fee),
)
if step.amountIn.ToString() == "271" {
// println("> state.sqrtPriceX96:", state.sqrtPriceX96.ToString())
// println("step.amountIn:", step.amountIn.ToString())
// println("step.amountOut:", step.amountOut.ToString())
// println("step.feeAmount:", step.feeAmount.ToString())
// panic("FIX")
}

amountInWithFee := i256.FromUint256(new(u256.Uint).Add(step.amountIn, step.feeAmount))
if exactInput {
Expand Down
2 changes: 1 addition & 1 deletion pool/_TEST_pool_dryswap_and_swap_test.gnoa
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestDrySwap_ZeroForOneTrue_AmountSpecified_Negative_16000(t *testing.T) {
consts.MIN_PRICE, // sqrtPriceLimitX96
)

shouldEQ(t, poolIn, "43512") // r3v4_xxx: ROUNDING ERROR
shouldEQ(t, poolIn, "43512")
shouldEQ(t, poolOut, "-16000")
}

Expand Down
Loading

0 comments on commit 4dc5b34

Please sign in to comment.