Skip to content

Commit

Permalink
Added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Sniezka1927 committed Apr 9, 2024
1 parent d74f490 commit c26259d
Show file tree
Hide file tree
Showing 2 changed files with 261 additions and 1 deletion.
11 changes: 10 additions & 1 deletion contracts/math/uints.ral
Original file line number Diff line number Diff line change
Expand Up @@ -178,5 +178,14 @@ Contract Uints () {
result = bigDiv512(result, sqrtPriceDenominator, 1)
return toU256(result)
}
}

pub fn bigRescale(fromValue: U256, fromScale: U256, expectedScale: U256) -> U512 {
if (expectedScale > fromScale) {
let multiplierScale = expectedScale - fromScale
return bigMul256(fromValue, 10 ** multiplierScale)
} else {
let denominatorScale = fromScale - expectedScale
return bigDiv512(toU512(fromValue), 10 ** denominatorScale, 1)
}
}
}
251 changes: 251 additions & 0 deletions test/clamm.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,257 @@ describe('math tests', () => {
expect(sqrtPrice).toEqual(998501199320000000000000n)
}
})
test('get next sqrt price y down - base samples', async () => {
const clamm = await deployCLAMM(sender)
const sqrtPriceDenominator = 10n ** 24n
const liquidityDenominator = 10n ** 5n
{
const startingSqrtPrice = 1n * sqrtPriceDenominator
const liquidity = 1n * liquidityDenominator
const y = 1n
const params = { args: { startingSqrtPrice, liquidity, y, addY: true } }
const nextSqrtPrice = (await clamm.contractInstance.methods.getNextSqrtPriceYDown(params)).returns
expect(nextSqrtPrice).toEqual(2n * sqrtPriceDenominator)
}
{
const startingSqrtPrice = 1n * sqrtPriceDenominator
const liquidity = 2n * liquidityDenominator
const y = 3n
const params = { args: { startingSqrtPrice, liquidity, y, addY: true } }
const nextSqrtPrice = (await clamm.contractInstance.methods.getNextSqrtPriceYDown(params)).returns
expect(nextSqrtPrice).toEqual(25n * 10n ** 23n)
}
{
const startingSqrtPrice = 2n * sqrtPriceDenominator
const liquidity = 3n * liquidityDenominator
const y = 5n
const params = { args: { startingSqrtPrice, liquidity, y, addY: true } }
const nextSqrtPrice = (await clamm.contractInstance.methods.getNextSqrtPriceYDown(params)).returns
expect(nextSqrtPrice).toEqual((11n * sqrtPriceDenominator) / 3n)
}
{
const startingSqrtPrice = 24234n * sqrtPriceDenominator
const liquidity = 3000n * liquidityDenominator
const y = 5000n
const params = { args: { startingSqrtPrice, liquidity, y, addY: true } }
const nextSqrtPrice = (await clamm.contractInstance.methods.getNextSqrtPriceYDown(params)).returns
expect(nextSqrtPrice).toEqual((72707n * sqrtPriceDenominator) / 3n)
}
{
const startingSqrtPrice = 1n * sqrtPriceDenominator
const liquidity = 3n * liquidityDenominator
const y = 5n
const params = { args: { startingSqrtPrice, liquidity, y, addY: false } }
const nextSqrtPrice = (await clamm.contractInstance.methods.getNextSqrtPriceYDown(params)).returns
expect(nextSqrtPrice).toEqual(5n * 10n ** 23n)
}
{
const startingSqrtPrice = 100000n * sqrtPriceDenominator
const liquidity = 500000000n * liquidityDenominator
const y = 4000n
const params = { args: { startingSqrtPrice, liquidity, y, addY: false } }
const nextSqrtPrice = (await clamm.contractInstance.methods.getNextSqrtPriceYDown(params)).returns
expect(nextSqrtPrice).toEqual(99999999992000000000000000000n)
}
{
const startingSqrtPrice = 3n * sqrtPriceDenominator
const liquidity = 222n * liquidityDenominator
const y = 37n
const params = { args: { startingSqrtPrice, liquidity, y, addY: false } }
const nextSqrtPrice = (await clamm.contractInstance.methods.getNextSqrtPriceYDown(params)).returns
expect(nextSqrtPrice).toEqual(2833333333333333333333333n)
}
})
test('get next sqrt price y down - domain', async () => {
const clamm = await deployCLAMM(sender)
const minY = 1n
const maxY = (1n << 256n) - 1n
const maxSqrtPrice = 65535383934512647000000000000n
const minSqrtPrice = 15258932000000000000n
const almostMinSqrtPrice = minSqrtPrice + 1n
const almostMaxSqrtPrice = maxSqrtPrice - 1n
const minSqrtPriceOutsideDomain = 1n
const minLiquidity = 1n
const maxLiquidity = (1n << 256n) - 1n
const minOverflowTokenY = 340282366920940n
const oneLiquidity = 1n * 10n ** 5n

// Min value inside domain
{
// increases minSqrtPrice
{
const params = {
args: {
startingSqrtPrice: minSqrtPrice,
liquidity: maxLiquidity,
y: minY + (1n << 128n) * (1n << 32n),
addY: true
}
}
const nextSqrtPrice = (await clamm.contractInstance.methods.getNextSqrtPriceYDown(params)).returns
expect(nextSqrtPrice).toEqual(15258932000000000001n)
}
// decreases almostMinSqrtPrice
{
const params = {
args: {
startingSqrtPrice: almostMinSqrtPrice,
liquidity: maxLiquidity,
y: minY,
addY: false
}
}
const nextSqrtPrice = (await clamm.contractInstance.methods.getNextSqrtPriceYDown(params)).returns
expect(nextSqrtPrice).toEqual(15258932000000000000n)
}
}
// Max value inside domain
{
// decreases maxSqrtPrice
{
const params = {
args: {
startingSqrtPrice: maxSqrtPrice,
liquidity: maxLiquidity,
y: minY + (1n << 128n) * (1n << 32n),
addY: false
}
}
const nextSqrtPrice = (await clamm.contractInstance.methods.getNextSqrtPriceYDown(params)).returns
expect(nextSqrtPrice).toEqual(65535383934512646999999999998n)
}
// increases almostMaxSqrtPrice
{
const params = {
args: {
startingSqrtPrice: almostMaxSqrtPrice,
liquidity: maxLiquidity,
y: minY + 600000000n,
addY: true
}
}
const nextSqrtPrice = (await clamm.contractInstance.methods.getNextSqrtPriceYDown(params)).returns
expect(nextSqrtPrice).toEqual(65535383934512646999999999999n)
}
}
// Extension TokenAmount to SqrtPrice decimal overflow
{
{
const params = {
args: {
startingSqrtPrice: maxSqrtPrice,
liquidity: minLiquidity,
y: maxY,
addY: true
}
}
await expectError(clamm.contractInstance.methods.getNextSqrtPriceYDown(params))
}
{
const params = {
args: {
startingSqrtPrice: minSqrtPriceOutsideDomain,
liquidity: minLiquidity,
y: maxY,
addY: false
}
}
await expectError(clamm.contractInstance.methods.getNextSqrtPriceYDown(params))
}
}
// Overflow in SqrtPrice Diffrence
{
{
const params = {
args: {
startingSqrtPrice: maxSqrtPrice,
liquidity: oneLiquidity,
y: minOverflowTokenY + 2n,
addY: true
}
}
await expectError(clamm.contractInstance.methods.getNextSqrtPriceYDown(params))
}
{
const params = {
args: {
startingSqrtPrice: minSqrtPriceOutsideDomain,
liquidity: oneLiquidity,
y: minOverflowTokenY - 2n,
addY: false
}
}
await expectError(clamm.contractInstance.methods.getNextSqrtPriceYDown(params))
}
}
// Quotient overflow
{
const minYOverflowDecimalExtension = 1n << 225n
const irrelevantSqrtPrice = 1n
const irrelevantLiquidity = oneLiquidity
{
const params = {
args: {
startingSqrtPrice: irrelevantSqrtPrice,
liquidity: irrelevantLiquidity,
y: minYOverflowDecimalExtension,
addY: true
}
}
await expectError(clamm.contractInstance.methods.getNextSqrtPriceYDown(params))
}
{
const params = {
args: {
startingSqrtPrice: irrelevantSqrtPrice,
liquidity: irrelevantLiquidity,
y: minYOverflowDecimalExtension,
addY: false
}
}
await expectError(clamm.contractInstance.methods.getNextSqrtPriceYDown(params))
}
}
// Y max
{
const params = {
args: {
startingSqrtPrice: minSqrtPrice,
liquidity: maxLiquidity,
y: maxY,
addY: true
}
}
const nextSqrtPrice = (await clamm.contractInstance.methods.getNextSqrtPriceYDown(params)).returns
expect(nextSqrtPrice).toEqual(100000000015258932000000000000n)
}
// L = 0
{
const params = {
args: {
startingSqrtPrice: minSqrtPrice,
liquidity: 0n,
y: minY,
addY: true
}
}
await expectError(clamm.contractInstance.methods.getNextSqrtPriceYDown(params))
}
// TokenAmount is zero
{
const params = {
args: {
startingSqrtPrice: minSqrtPrice,
liquidity: maxLiquidity,
y: 0n,
addY: true
}
}
const nextSqrtPrice = (await clamm.contractInstance.methods.getNextSqrtPriceYDown(params)).returns
expect(nextSqrtPrice).toEqual(minSqrtPrice)
}
})

describe('get delta x', () => {
let clamm: DeployContractResult<CLAMMInstance>

Expand Down

0 comments on commit c26259d

Please sign in to comment.