diff --git a/contracts/math/clamm.ral b/contracts/math/clamm.ral index 2f53701e..329295b9 100644 --- a/contracts/math/clamm.ral +++ b/contracts/math/clamm.ral @@ -136,12 +136,12 @@ Contract CLAMM(uints: Uints) extends Log() { let mut result = U512 { lower: 0, higher: 0 } if (roundingUp) { - result = uints.bigMulUp(deltaSqrtPrice, liquidity, one(LiquidityScale)) - result = uints.bigAdd512(result, almostOne(SqrtPriceScale)) + result = uints.bigMulDiv256(deltaSqrtPrice, liquidity, one(LiquidityScale)) + result = uints.bigAdd512(result, uints.toU512(almostOne(SqrtPriceScale))) result = uints.bigDiv(result, one(SqrtPriceScale), 1) return uints.toU256(result) } else { - result = uints.bigMulUp(deltaSqrtPrice, liquidity, one(LiquidityScale)) + result = uints.bigMulDiv256(deltaSqrtPrice, liquidity, one(LiquidityScale)) result = uints.bigDiv(result, one(SqrtPriceScale), 1) return uints.toU256(result) } diff --git a/contracts/math/uints.ral b/contracts/math/uints.ral index 992aad9c..f2c415bf 100644 --- a/contracts/math/uints.ral +++ b/contracts/math/uints.ral @@ -1,3 +1,8 @@ +struct Result { + mut value: U512, + mut error: Bool +} + struct U512 { mut higher: U256, mut lower: U256 @@ -23,7 +28,7 @@ Contract Uints () { return v.lower } - pub fn bigAdd(a: U256, b: U256) -> U512 { + pub fn bigAdd256(a: U256, b: U256) -> U512 { if (MaxU256 - a < b) { return U512 { higher: 1, @@ -79,13 +84,60 @@ Contract Uints () { return div(a, b, bDenominator, true) } - pub fn bigAdd512(a: U512, b: U256) -> U512 { - let (aLowerB, overflow) = overflowingAdd(a.lower, b) - let aHigherOverflow = wrappingAdd(a.higher, overflow) + pub fn bigAdd512(a: U512, b: U512) -> U512 { + let (aLowerB, overflow) = overflowingAdd(a.lower, b.lower) + let aHigherOverflow = wrappingAdd(wrappingAdd(a.higher, b.higher), overflow) return U512 { lower: aLowerB, higher: aHigherOverflow } } - pub fn mul(a: U256, b: U256) -> U512 { + pub fn bigDivWrapper(a: U512, b: U256, bDenominator: U256, up: Bool) -> U512 { + assert!(b > 0, ArithmeticErrors.NotPositiveDivisor) + + if (b == 1) { + return a + } + + // Calculate new higher + let newHigher = a.higher * bDenominator + let higher = newHigher / b + let higherRemainder = newHigher % b + // calculate higher remainder + let maxDiv = MaxU256 / b + let deltaHigherRemainder = higherRemainder * maxDiv + // Calculate lower + let newLower = a.lower * bDenominator + let lower = newLower / b + let mut deltaLower = deltaHigherRemainder + lower + + if (up) { + let lowerRemainder = newLower % b + + let higherDecimal = deltaHigherRemainder % b + if (higherDecimal + lowerRemainder != 0) { + deltaLower = deltaLower + 1 + } + } + + return U512 { + higher: higher, + lower: deltaLower + } + } + + pub fn bigDiv512(a: U512, b: U256, bDenominator: U256) -> U512 { + return bigDivWrapper(a, b, bDenominator, false) + } + + pub fn bigDivUp512(a: U512, b: U256, bDenominator: U256) -> U512 { + return bigDivWrapper(a, b, bDenominator, true) + } + + pub fn bigMul512(a: U512, b: U256) -> U512 { + let result = bigMul256(a.lower, b) + return bigAdd512(result, U512 { higher: a.higher * b, lower: 0 }) + } + + pub fn bigMul256(a: U256, b: U256) -> U512 { let aLower = low128(a) let aHigher = high128(a) let bLower = low128(b) @@ -106,18 +158,18 @@ Contract Uints () { return U512 { higher: higher, lower: lower } } - pub fn bigMul(a: U256, b: U256, bDenominator: U256) -> U512 { - let mut result = mul(a, b) + pub fn bigMulDiv256(a: U256, b: U256, bDenominator: U256) -> U512 { + let mut result = bigMul256(a, b) - result = bigDiv(result, bDenominator, 1) + result = bigDiv512(result, bDenominator, 1) return result } - pub fn bigMulUp(a: U256, b: U256, bDenominator: U256) -> U512 { - let mut result = mul(a, b) + pub fn bigMulDivUp256(a: U256, b: U256, bDenominator: U256) -> U512 { + let mut result = bigMul256(a, b) - result = bigAdd512(result, bDenominator - 1) + result = bigAdd512(result, toU512(bDenominator - 1)) result = bigDiv(result, bDenominator, 1) return result diff --git a/test/clamm.test.ts b/test/clamm.test.ts index 0591aed4..f7e55e03 100644 --- a/test/clamm.test.ts +++ b/test/clamm.test.ts @@ -473,4 +473,244 @@ describe('math tests', () => { } } }) + + describe('calculate fee growth inside', () => { + let clamm: CLAMMInstance + const globalFeeGrowthX = 15_0000000000000000000000000000n + const globalFeeGrowthY = 15_0000000000000000000000000000n + + const tickLowerIndex = -2n + const tickLowerFeeGrowthOutsideX = 0n + const tickLowerFeeGrowthOutsideY = 0n + + const tickUpperIndex = 2n + const tickUpperFeeGrowthOutsideX = 0n + const tickUpperFeeGrowthOutsideY = 0n + + beforeEach(async () => { + clamm = (await deployCLAMM(sender)).contractInstance + }) + + test('sqrt price between ticks', async () => { + // <────────────── ──────────────> + // fee_outside_t0| fee_growth_inside |fee_outside_t1 + //<───────────── t0 ────── C ────── t1 ───────────────────> + // fee_growth_inside = fee_growth_global - t0.fee_outside - t1.fee_outside + + const tickCurrent = 0n + + // current tick inside range + // lower current upper + // | | | + // -2 0 2 + + const result = await clamm.methods.calculateFeeGrowthInside({ + args: { + tickLowerIndex, + tickLowerFeeGrowthOutsideX, + tickLowerFeeGrowthOutsideY, + tickUpperIndex, + tickUpperFeeGrowthOutsideX, + tickUpperFeeGrowthOutsideY, + tickCurrent, + globalFeeGrowthX, + globalFeeGrowthY + } + }) + + expect(result.returns[0]).toBe(15_0000000000000000000000000000n) + expect(result.returns[1]).toBe(15_0000000000000000000000000000n) + }) + + test('sqrt price below ticks', async () => { + // ───────fee_outside_t0──────────> + // |fee_growth_inside| fee_outside_t1 + // ─────── c ─────── t0 ──────────────> t1 ───────────> + // fee_growth_inside = t0.fee_outside - t1.fee_outside + + const tickCurrent = -4n + + // current tick below range + // current lower upper + // | | | + // -4 2 2 + + const result = await clamm.methods.calculateFeeGrowthInside({ + args: { + tickLowerIndex, + tickLowerFeeGrowthOutsideX, + tickLowerFeeGrowthOutsideY, + tickUpperIndex, + tickUpperFeeGrowthOutsideX, + tickUpperFeeGrowthOutsideY, + tickCurrent, + globalFeeGrowthX, + globalFeeGrowthY + } + }) + + expect(result.returns[0]).toBe(0n) + expect(result.returns[1]).toBe(0n) + }) + + test('sqrt price above ticks', async () => { + // <──────────fee_outside_t0────────── + // fee_outside_t1 | fee_growth_inside| + // ────────────── t1 ──────────────── t0 ─────── c ───────────> + // fee_growth_inside = t0.fee_outside - t1.fee_outside + + const tickCurrent = 3n + + // current tick upper range + // lower upper current + // | | | + // -2 2 4 + + const result = await clamm.methods.calculateFeeGrowthInside({ + args: { + tickLowerIndex, + tickLowerFeeGrowthOutsideX, + tickLowerFeeGrowthOutsideY, + tickUpperIndex, + tickUpperFeeGrowthOutsideX, + tickUpperFeeGrowthOutsideY, + tickCurrent, + globalFeeGrowthX, + globalFeeGrowthY + } + }) + + expect(result.returns[0]).toBe(0n) + expect(result.returns[1]).toBe(0n) + }) + + test('sqrt price above ticks, liquidity outside upper tick', async () => { + const tickCurrent = 3n + + const tickUpperFeeGrowthOutsideX = 1n + const tickUpperFeeGrowthOutsideY = 2n + + const globalFeeGrowthX = 5_0000000000000000000000000000n + const globalFeeGrowthY = 5_0000000000000000000000000000n + + // current tick upper range + // lower upper current + // | | | + // -2 2 3 + + const result = await clamm.methods.calculateFeeGrowthInside({ + args: { + tickLowerIndex, + tickLowerFeeGrowthOutsideX, + tickLowerFeeGrowthOutsideY, + tickUpperIndex, + tickUpperFeeGrowthOutsideX, + tickUpperFeeGrowthOutsideY, + tickCurrent, + globalFeeGrowthX, + globalFeeGrowthY + } + }) + + expect(result.returns[0]).toBe(1n) + expect(result.returns[1]).toBe(2n) + }) + + test('sqrt price in between ticks, liquidity outside upper tick', async () => { + const tickCurrent = 0n + + const tickUpperFeeGrowthOutsideX = 2_0000000000000000000000000000n + const tickUpperFeeGrowthOutsideY = 3_0000000000000000000000000000n + + // current tick inside range + // lower current upper + // | | | + // -2 0 2 + + const result = await clamm.methods.calculateFeeGrowthInside({ + args: { + tickLowerIndex, + tickLowerFeeGrowthOutsideX, + tickLowerFeeGrowthOutsideY, + tickUpperIndex, + tickUpperFeeGrowthOutsideX, + tickUpperFeeGrowthOutsideY, + tickCurrent, + globalFeeGrowthX, + globalFeeGrowthY + } + }) + + expect(result.returns[0]).toBe(13_0000000000000000000000000000n) + expect(result.returns[1]).toBe(12_0000000000000000000000000000n) + }) + + test('sqrt price in between ticks, liquidity outside lower tick', async () => { + const tickCurrent = 0n + + const tickLowerFeeGrowthOutsideX = 2_0000000000000000000000000000n + const tickLowerFeeGrowthOutsideY = 3_0000000000000000000000000000n + + // current tick inside range + // lower current upper + // | | | + // -2 0 2 + + const result = await clamm.methods.calculateFeeGrowthInside({ + args: { + tickLowerIndex, + tickLowerFeeGrowthOutsideX, + tickLowerFeeGrowthOutsideY, + tickUpperIndex, + tickUpperFeeGrowthOutsideX, + tickUpperFeeGrowthOutsideY, + tickCurrent, + globalFeeGrowthX, + globalFeeGrowthY + } + }) + + expect(result.returns[0]).toBe(13_0000000000000000000000000000n) + expect(result.returns[1]).toBe(12_0000000000000000000000000000n) + }) + }) + + describe('calculate fee growth inside - domain', () => { + let clamm: CLAMMInstance + + const tickCurrent = 0n + const globalFeeGrowthX = 20_0000000000000000000000000000n + const globalFeeGrowthY = 20_0000000000000000000000000000n + + const tickLowerIndex = -20n + const tickLowerFeeGrowthOutsideX = 20_0000000000000000000000000000n + const tickLowerFeeGrowthOutsideY = 20_0000000000000000000000000000n + + const tickUpperIndex = -10n + const tickUpperFeeGrowthOutsideX = 15_0000000000000000000000000000n + const tickUpperFeeGrowthOutsideY = 15_0000000000000000000000000000n + + beforeEach(async () => { + clamm = (await deployCLAMM(sender)).contractInstance + }) + + test('max fee growth', async () => { + const result = await clamm.methods.calculateFeeGrowthInside({ + args: { + tickLowerIndex, + tickLowerFeeGrowthOutsideX, + tickLowerFeeGrowthOutsideY, + tickUpperIndex, + tickUpperFeeGrowthOutsideX, + tickUpperFeeGrowthOutsideY, + tickCurrent, + globalFeeGrowthX, + globalFeeGrowthY + } + }) + + expect(result.returns[0]).toBe(2n ** 256n - 1n - 5_0000000000000000000000000000n + 1n) + expect(result.returns[1]).toBe(2n ** 256n - 1n - 5_0000000000000000000000000000n + 1n) + }) + }) }) diff --git a/test/uints.test.ts b/test/uints.test.ts index efc7f8b4..927d8a26 100644 --- a/test/uints.test.ts +++ b/test/uints.test.ts @@ -21,29 +21,29 @@ describe('uints tests', () => { expect(castBack).toStrictEqual(v) }) - test('big add', async () => { + test('big add 256', async () => { const uints = await deployUints(sender) { const a = 115792089237316195423570985008687907853269984665640564039457584007913129639935n const b = 999n - const result = (await uints.contractInstance.methods.bigAdd({ args: { a, b } })).returns + const result = (await uints.contractInstance.methods.bigAdd256({ args: { a, b } })).returns expect(result).toStrictEqual({ higher: 1n, lower: b - 1n }) } { const a = 777n const b = 115792089237316195423570985008687907853269984665640564039457584007913129639935n - const result = (await uints.contractInstance.methods.bigAdd({ args: { a, b } })).returns + const result = (await uints.contractInstance.methods.bigAdd256({ args: { a, b } })).returns expect(result).toStrictEqual({ higher: 1n, lower: a - 1n }) } { const a = 115792089237316195423570985008687907853269984665640564039457584007913129639935n const b = 115792089237316195423570985008687907853269984665640564039457584007913129639935n - const result = (await uints.contractInstance.methods.bigAdd({ args: { a, b } })).returns + const result = (await uints.contractInstance.methods.bigAdd256({ args: { a, b } })).returns expect(result).toStrictEqual({ higher: 1n, lower: a - 1n }) } }) - test('big div', async () => { + test('big div 512 / big div up 512', async () => { const uints = await deployUints(sender) { const a = { @@ -53,7 +53,7 @@ describe('uints tests', () => { const b = 50n const bDenominator = 10n { - const result = (await uints.contractInstance.methods.bigDiv({ args: { a, b, bDenominator } })).returns + const result = (await uints.contractInstance.methods.bigDiv512({ args: { a, b, bDenominator } })).returns // expected: 486326774796728020778998137036489212983733935595690368965721000000000000000000 // received: 486326774796728020778998137036489212983733935595690368965721852833235144487731 expect(result).toStrictEqual({ @@ -62,7 +62,7 @@ describe('uints tests', () => { }) } { - const result = (await uints.contractInstance.methods.bigDivUp({ args: { a, b, bDenominator } })).returns + const result = (await uints.contractInstance.methods.bigDivUp512({ args: { a, b, bDenominator } })).returns expect(result).toStrictEqual({ higher: 4n, lower: 23158417847463239084714197001737581570653996933128112807891516801582625927988n @@ -77,7 +77,7 @@ describe('uints tests', () => { const b = 2n const bDenominator = 1n { - const result = (await uints.contractInstance.methods.bigDiv({ args: { a, b, bDenominator } })).returns + const result = (await uints.contractInstance.methods.bigDiv512({ args: { a, b, bDenominator } })).returns // expected: 2907354897182427562197295231552018137414565442749272241125960796722557152453591693304764202855054262243050086425064711734138406514458624n // received: 2907354897182427562197295231552018137414565442749272241125960796722557152453591693304764202855054262243050086425064711734138406514458624n expect(result).toStrictEqual({ @@ -86,7 +86,7 @@ describe('uints tests', () => { }) } { - const result = (await uints.contractInstance.methods.bigDivUp({ args: { a, b, bDenominator } })).returns + const result = (await uints.contractInstance.methods.bigDivUp512({ args: { a, b, bDenominator } })).returns expect(result).toStrictEqual({ higher: 25108406941546723055343157692830665664409421777856138051584n, lower: 0n @@ -101,14 +101,14 @@ describe('uints tests', () => { const b = 2n const bDenominator = 1n { - const result = (await uints.contractInstance.methods.bigDiv({ args: { a, b, bDenominator } })).returns + const result = (await uints.contractInstance.methods.bigDiv512({ args: { a, b, bDenominator } })).returns expect(result).toStrictEqual({ higher: 0n, lower: 0n }) } { - const result = (await uints.contractInstance.methods.bigDivUp({ args: { a, b, bDenominator } })).returns + const result = (await uints.contractInstance.methods.bigDivUp512({ args: { a, b, bDenominator } })).returns expect(result).toStrictEqual({ higher: 0n, lower: 1n @@ -123,14 +123,14 @@ describe('uints tests', () => { const b = 1n const bDenominator = 1000n { - const result = (await uints.contractInstance.methods.bigDiv({ args: { a, b, bDenominator } })).returns + const result = (await uints.contractInstance.methods.bigDiv512({ args: { a, b, bDenominator } })).returns expect(result).toStrictEqual({ higher: 0n, lower: 10n }) } { - const result = (await uints.contractInstance.methods.bigDivUp({ args: { a, b, bDenominator } })).returns + const result = (await uints.contractInstance.methods.bigDivUp512({ args: { a, b, bDenominator } })).returns expect(result).toStrictEqual({ higher: 0n, lower: 10n @@ -146,14 +146,14 @@ describe('uints tests', () => { const b = 3n * 10n ** 3n const bDenominator = 1000n { - const result = (await uints.contractInstance.methods.bigDiv({ args: { a, b, bDenominator } })).returns + const result = (await uints.contractInstance.methods.bigDiv512({ args: { a, b, bDenominator } })).returns expect(result).toStrictEqual({ higher: 0n, lower: 20n }) } { - const result = (await uints.contractInstance.methods.bigDivUp({ args: { a, b, bDenominator } })).returns + const result = (await uints.contractInstance.methods.bigDivUp512({ args: { a, b, bDenominator } })).returns expect(result).toStrictEqual({ higher: 0n, lower: 20n @@ -166,24 +166,79 @@ describe('uints tests', () => { const uints = await deployUints(sender) { const a = { higher: 0n, lower: 10n } - const b = 20n + const b = { higher: 0n, lower: 20n } const result = (await uints.contractInstance.methods.bigAdd512({ args: { a, b } })).returns expect(result).toStrictEqual({ higher: 0n, lower: 30n }) } { const a = { higher: 0n, lower: 115792089237316195423570985008687907853269984665640564039457584007913129639935n } - const b = 20n + const b = { higher: 0n, lower: 20n } const result = (await uints.contractInstance.methods.bigAdd512({ args: { a, b } })).returns expect(result).toStrictEqual({ higher: 1n, lower: 19n }) } }) - test('big mul', async () => { + test('big mul 512', async () => { + const uints = await deployUints(sender) + { + const a = { higher: 0n, lower: 123n } + const b = 2n + const result = (await uints.contractInstance.methods.bigMul512({ args: { a, b } })).returns + expect(result).toStrictEqual({ higher: 0n, lower: 246n }) + // expected: 246 + // real: 246 + } + { + const a = { higher: 0n, lower: 340282366920938463463374607431768211457n } + const b = 340282366920938463463374607431768211457n + const result = (await uints.contractInstance.methods.bigMul512({ args: { a, b } })).returns + expect(result).toStrictEqual({ higher: 1n, lower: 680564733841876926926749214863536422913n }) + // expected: 115792089237316195423570985008687907853950549399482440966384333222776666062849 + // real: 115792089237316195423570985008687907853950549399482440966384333222776666062849 + } + { + const a = { higher: 0n, lower: 115792089237316195423570985008687907853269984665640564039457584007913129639935n } + const b = 115792089237316195423570985008687907853269984665640564039457584007913129639935n + const result = (await uints.contractInstance.methods.bigMul512({ args: { a, b } })).returns + expect(result).toStrictEqual({ + higher: 115792089237316195423570985008687907853269984665640564039457584007913129639934n, + lower: 1n + }) + // expected: 13407807929942597099574024998205846127479365820592393377723561443721764030073315392623399665776056285720014482370779510884422601683867654778417822746804225 + // real: 13407807929942597099574024998205846127479365820592393377723561443721764030073315392623399665776056285720014482370779510884422601683867654778417822746804225 + } + { + const a = { higher: 0n, lower: 500n } + const b = 0n + const result = (await uints.contractInstance.methods.bigMul512({ args: { a, b } })).returns + expect(result).toStrictEqual({ higher: 0n, lower: 0n }) + } + { + const a = { higher: 1n, lower: 115792089237316195423570985008687907853269984665640564039457584007913129639935n } + const b = 100n + const result = (await uints.contractInstance.methods.bigMul512({ args: { a, b } })).returns + expect(result).toStrictEqual({ + higher: 199n, + lower: 115792089237316195423570985008687907853269984665640564039457584007913129639836n + }) + } + { + const a = { higher: 340282366920938463463374607431768211455n, lower: 340282366920938463463374607431768211455n } + const b = 340282366920938463463374607431768211455n + const result = (await uints.contractInstance.methods.bigMul512({ args: { a, b } })).returns + expect(result).toStrictEqual({ + higher: 115792089237316195423570985008687907852589419931798687112530834793049593217025n, + lower: 115792089237316195423570985008687907852589419931798687112530834793049593217025n + }) + } + }) + + test('big mul div 256', async () => { const uints = await deployUints(sender) { const a = 123n const b = 2n - const result = (await uints.contractInstance.methods.bigMul({ args: { a, b, bDenominator: 1n } })).returns + const result = (await uints.contractInstance.methods.bigMulDiv256({ args: { a, b, bDenominator: 1n } })).returns expect(result).toStrictEqual({ higher: 0n, lower: 246n }) // expected: 246 // real: 246 @@ -191,7 +246,7 @@ describe('uints tests', () => { { const a = 340282366920938463463374607431768211457n const b = 340282366920938463463374607431768211457n - const result = (await uints.contractInstance.methods.bigMul({ args: { a, b, bDenominator: 1n } })).returns + const result = (await uints.contractInstance.methods.bigMulDiv256({ args: { a, b, bDenominator: 1n } })).returns expect(result).toStrictEqual({ higher: 1n, lower: 680564733841876926926749214863536422913n }) // expected: 115792089237316195423570985008687907853950549399482440966384333222776666062849 // real: 115792089237316195423570985008687907853950549399482440966384333222776666062849 @@ -199,7 +254,7 @@ describe('uints tests', () => { { const a = 115792089237316195423570985008687907853269984665640564039457584007913129639935n const b = 115792089237316195423570985008687907853269984665640564039457584007913129639935n - const result = (await uints.contractInstance.methods.bigMul({ args: { a, b, bDenominator: 1n } })).returns + const result = (await uints.contractInstance.methods.bigMulDiv256({ args: { a, b, bDenominator: 1n } })).returns expect(result).toStrictEqual({ higher: 115792089237316195423570985008687907853269984665640564039457584007913129639934n, lower: 1n @@ -210,41 +265,42 @@ describe('uints tests', () => { { const a = 500n const b = 0n - const result = (await uints.contractInstance.methods.bigMul({ args: { a, b, bDenominator: 1n } })).returns + const result = (await uints.contractInstance.methods.bigMulDiv256({ args: { a, b, bDenominator: 1n } })).returns expect(result).toStrictEqual({ higher: 0n, lower: 0n }) } { const a = 100n const b = 100n - const result = (await uints.contractInstance.methods.bigMul({ args: { a, b, bDenominator: 100n } })).returns + const result = (await uints.contractInstance.methods.bigMulDiv256({ args: { a, b, bDenominator: 100n } })).returns expect(result).toStrictEqual({ higher: 0n, lower: 100n }) } { const a = 30n const b = 1n - const result = (await uints.contractInstance.methods.bigMul({ args: { a, b, bDenominator: 10n } })).returns + const result = (await uints.contractInstance.methods.bigMulDiv256({ args: { a, b, bDenominator: 10n } })).returns expect(result).toStrictEqual({ higher: 0n, lower: 3n }) } { const a = 500n const b = 4000n - const result = (await uints.contractInstance.methods.bigMul({ args: { a, b, bDenominator: 1000n } })).returns + const result = (await uints.contractInstance.methods.bigMulDiv256({ args: { a, b, bDenominator: 1000n } })) + .returns expect(result).toStrictEqual({ higher: 0n, lower: 2000n }) } { const a = 10n const b = 37n - const result = (await uints.contractInstance.methods.bigMul({ args: { a, b, bDenominator: 100n } })).returns + const result = (await uints.contractInstance.methods.bigMulDiv256({ args: { a, b, bDenominator: 100n } })).returns expect(result).toStrictEqual({ higher: 0n, lower: 3n }) } }) - test('big mul up', async () => { + test('big mul div up 256', async () => { const uints = await deployUints(sender) { const a = 123n const b = 2n - const result = (await uints.contractInstance.methods.bigMulUp({ args: { a, b, bDenominator: 1n } })).returns + const result = (await uints.contractInstance.methods.bigMulDivUp256({ args: { a, b, bDenominator: 1n } })).returns expect(result).toStrictEqual({ higher: 0n, lower: 246n }) // expected: 246 // real: 246 @@ -252,7 +308,7 @@ describe('uints tests', () => { { const a = 340282366920938463463374607431768211457n const b = 340282366920938463463374607431768211457n - const result = (await uints.contractInstance.methods.bigMulUp({ args: { a, b, bDenominator: 1n } })).returns + const result = (await uints.contractInstance.methods.bigMulDivUp256({ args: { a, b, bDenominator: 1n } })).returns expect(result).toStrictEqual({ higher: 1n, lower: 680564733841876926926749214863536422913n }) // expected: 115792089237316195423570985008687907853950549399482440966384333222776666062849 // real: 115792089237316195423570985008687907853950549399482440966384333222776666062849 @@ -260,7 +316,7 @@ describe('uints tests', () => { { const a = 115792089237316195423570985008687907853269984665640564039457584007913129639935n const b = 115792089237316195423570985008687907853269984665640564039457584007913129639935n - const result = (await uints.contractInstance.methods.bigMulUp({ args: { a, b, bDenominator: 1n } })).returns + const result = (await uints.contractInstance.methods.bigMulDivUp256({ args: { a, b, bDenominator: 1n } })).returns expect(result).toStrictEqual({ higher: 115792089237316195423570985008687907853269984665640564039457584007913129639934n, lower: 1n @@ -271,31 +327,35 @@ describe('uints tests', () => { { const a = 500n const b = 0n - const result = (await uints.contractInstance.methods.bigMulUp({ args: { a, b, bDenominator: 1n } })).returns + const result = (await uints.contractInstance.methods.bigMulDivUp256({ args: { a, b, bDenominator: 1n } })).returns expect(result).toStrictEqual({ higher: 0n, lower: 0n }) } { const a = 100n const b = 100n - const result = (await uints.contractInstance.methods.bigMulUp({ args: { a, b, bDenominator: 100n } })).returns + const result = (await uints.contractInstance.methods.bigMulDivUp256({ args: { a, b, bDenominator: 100n } })) + .returns expect(result).toStrictEqual({ higher: 0n, lower: 100n }) } { const a = 30n const b = 1n - const result = (await uints.contractInstance.methods.bigMulUp({ args: { a, b, bDenominator: 10n } })).returns + const result = (await uints.contractInstance.methods.bigMulDivUp256({ args: { a, b, bDenominator: 10n } })) + .returns expect(result).toStrictEqual({ higher: 0n, lower: 3n }) } { const a = 500n const b = 4000n - const result = (await uints.contractInstance.methods.bigMulUp({ args: { a, b, bDenominator: 1000n } })).returns + const result = (await uints.contractInstance.methods.bigMulDivUp256({ args: { a, b, bDenominator: 1000n } })) + .returns expect(result).toStrictEqual({ higher: 0n, lower: 2000n }) } { const a = 10n const b = 37n - const result = (await uints.contractInstance.methods.bigMulUp({ args: { a, b, bDenominator: 100n } })).returns + const result = (await uints.contractInstance.methods.bigMulDivUp256({ args: { a, b, bDenominator: 100n } })) + .returns expect(result).toStrictEqual({ higher: 0n, lower: 4n }) } })