From e54187edfe8fa68b370c5c273161d4dda4bd321b Mon Sep 17 00:00:00 2001 From: Deemexlyon <113914039+Deemexlyon@users.noreply.github.com> Date: Fri, 6 Nov 2026 12:04:26 +0000 Subject: [PATCH 1/2] Validate liquidity input bounds --- src/__tests__/liquidity.test.ts | 71 ++++++++++++++++++++++++++++++++ src/core/liquidity.ts | 72 +++++++++++++++++++++++++-------- 2 files changed, 127 insertions(+), 16 deletions(-) diff --git a/src/__tests__/liquidity.test.ts b/src/__tests__/liquidity.test.ts index beac6fd..1cf962d 100644 --- a/src/__tests__/liquidity.test.ts +++ b/src/__tests__/liquidity.test.ts @@ -111,4 +111,75 @@ describe('StellarToolkit - Liquidity Pool Operations', () => { const proportionalShares = toolkit.calculateLpAmount(200, 50, 2000, 500, 1000); expect(proportionalShares).toBe('100.0000000'); }); + + it.each([NaN, Infinity, 0, -1])('rejects invalid amountA %p', (amountA) => { + expect(() => toolkit.calculateLpAmount(amountA, 10)).toThrow('amountA'); + }); + + it.each([NaN, Infinity, 0, -1])('rejects invalid amountB %p', (amountB) => { + expect(() => toolkit.calculateLpAmount(10, amountB)).toThrow('amountB'); + }); + + it.each([ + { field: 'reserveA', reserveA: 0, reserveB: 10, totalShares: 100 }, + { field: 'reserveA', reserveA: NaN, reserveB: 10, totalShares: 100 }, + { field: 'reserveB', reserveA: 10, reserveB: Infinity, totalShares: 100 }, + { field: 'totalShares', reserveA: 10, reserveB: 10, totalShares: 0 }, + { field: 'totalShares', reserveA: 10, reserveB: 10, totalShares: NaN }, + ])('rejects invalid $field', ({ field, reserveA, reserveB, totalShares }) => { + expect(() => toolkit.calculateLpAmount(1, 1, reserveA, reserveB, totalShares)).toThrow( + field, + ); + }); + + it('rejects incomplete optional reserve inputs', () => { + expect(() => toolkit.calculateLpAmount(1, 1, 10)).toThrow('reserveA, reserveB, and totalShares'); + }); + + it.each([ + { field: 'maxAmountA', maxAmountA: 'NaN', maxAmountB: '1' }, + { field: 'maxAmountA', maxAmountA: '0', maxAmountB: '1' }, + { field: 'maxAmountB', maxAmountA: '1', maxAmountB: 'Infinity' }, + ])('rejects invalid $field without submitting', async ({ field, maxAmountA, maxAmountB }) => { + const user = Keypair.random(); + const server = (toolkit as unknown as { server: { submitTransaction: jest.Mock } }).server; + + await expect( + toolkit.depositLiquidity({ + accountSecret: user.secret(), + assetA: 'native', + assetB: usdc, + maxAmountA, + maxAmountB, + minPrice: '3.5', + maxPrice: '4.5', + }), + ).rejects.toThrow(field); + expect(server.submitTransaction).not.toHaveBeenCalled(); + }); + + it.each([ + { field: 'spotPrice', spotPrice: NaN, minPrice: '3.5', maxPrice: '4.5' }, + { field: 'spotPrice', spotPrice: Infinity, minPrice: '3.5', maxPrice: '4.5' }, + { field: 'minPrice', spotPrice: 4, minPrice: 'NaN', maxPrice: '4.5' }, + { field: 'maxPrice', spotPrice: 4, minPrice: '3.5', maxPrice: 'Infinity' }, + { field: 'minPrice', spotPrice: 4, minPrice: '5', maxPrice: '4.5' }, + ])('rejects invalid $field without submitting', async ({ field, spotPrice, minPrice, maxPrice }) => { + const user = Keypair.random(); + const server = (toolkit as unknown as { server: { submitTransaction: jest.Mock } }).server; + + await expect( + toolkit.depositLiquidity({ + accountSecret: user.secret(), + assetA: 'native', + assetB: usdc, + maxAmountA: '200', + maxAmountB: '50', + minPrice, + maxPrice, + spotPrice, + }), + ).rejects.toThrow(field); + expect(server.submitTransaction).not.toHaveBeenCalled(); + }); }); diff --git a/src/core/liquidity.ts b/src/core/liquidity.ts index 863707e..a55c3e4 100644 --- a/src/core/liquidity.ts +++ b/src/core/liquidity.ts @@ -48,32 +48,63 @@ export class LiquidityPoolManager { ): string { const a = Number(amountA); const b = Number(amountB); - if (!Number.isFinite(a) || !Number.isFinite(b) || a <= 0 || b <= 0) { - throw new Error('Deposit amounts must be positive numbers'); + if (!Number.isFinite(a) || a <= 0) { + throw new Error('amountA must be a finite positive number'); + } + if (!Number.isFinite(b) || b <= 0) { + throw new Error('amountB must be a finite positive number'); + } + + const hasReserveInputs = [reserveA, reserveB, totalShares].some( + (value) => value !== undefined, + ); + const hasAllReserveInputs = [reserveA, reserveB, totalShares].every( + (value) => value !== undefined, + ); + if (hasReserveInputs && !hasAllReserveInputs) { + throw new Error('reserveA, reserveB, and totalShares must be provided together'); } - if ( - reserveA !== undefined && - reserveB !== undefined && - totalShares !== undefined && - Number(totalShares) > 0 - ) { + let minted: number; + if (hasAllReserveInputs) { const rA = Number(reserveA); const rB = Number(reserveB); const shares = Number(totalShares); - const minted = Math.min((a * shares) / rA, (b * shares) / rB); - return minted.toFixed(7); + if (!Number.isFinite(rA) || rA <= 0) { + throw new Error('reserveA must be a finite positive number'); + } + if (!Number.isFinite(rB) || rB <= 0) { + throw new Error('reserveB must be a finite positive number'); + } + if (!Number.isFinite(shares) || shares <= 0) { + throw new Error('totalShares must be a finite positive number'); + } + minted = Math.min((a * shares) / rA, (b * shares) / rB); + } else { + minted = Math.sqrt(a) * Math.sqrt(b); } - return Math.sqrt(a * b).toFixed(7); + if (!Number.isFinite(minted) || minted <= 0) { + throw new Error('LP share estimate must be a finite positive number'); + } + return minted.toFixed(7); } /** * Validate that the current pool spot price falls within [minPrice, maxPrice] */ validatePriceSlippage(spotPrice: number, minPrice: number, maxPrice: number): void { - if (minPrice <= 0 || maxPrice <= 0 || minPrice > maxPrice) { - throw new Error('Invalid price bounds'); + if (!Number.isFinite(minPrice) || minPrice <= 0) { + throw new Error('minPrice must be a finite positive number'); + } + if (!Number.isFinite(maxPrice) || maxPrice <= 0) { + throw new Error('maxPrice must be a finite positive number'); + } + if (minPrice > maxPrice) { + throw new Error('minPrice must be less than or equal to maxPrice'); + } + if (!Number.isFinite(spotPrice) || spotPrice <= 0) { + throw new Error('spotPrice must be a finite positive number'); } if (spotPrice < minPrice || spotPrice > maxPrice) { throw new Error( @@ -89,9 +120,18 @@ export class LiquidityPoolManager { const { accountSecret, assetA, assetB, maxAmountA, maxAmountB, minPrice, maxPrice, spotPrice } = options; - const minP = parseFloat(minPrice); - const maxP = parseFloat(maxPrice); - const impliedPrice = spotPrice ?? parseFloat(maxAmountA) / parseFloat(maxAmountB); + const amountA = Number(maxAmountA); + const amountB = Number(maxAmountB); + if (!Number.isFinite(amountA) || amountA <= 0) { + throw new Error('maxAmountA must be a finite positive number'); + } + if (!Number.isFinite(amountB) || amountB <= 0) { + throw new Error('maxAmountB must be a finite positive number'); + } + + const minP = Number(minPrice); + const maxP = Number(maxPrice); + const impliedPrice = spotPrice ?? amountA / amountB; this.validatePriceSlippage(impliedPrice, minP, maxP); const keypair = Keypair.fromSecret(accountSecret); From 16f281828e8297b54a0eca1d1d84d5b619a20f38 Mon Sep 17 00:00:00 2001 From: Deemexlyon <113914039+Deemexlyon@users.noreply.github.com> Date: Fri, 6 Nov 2026 12:09:59 +0000 Subject: [PATCH 2/2] Format liquidity validation changes --- src/__tests__/liquidity.test.ts | 47 ++++++++++++++++++--------------- src/core/liquidity.ts | 4 +-- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/src/__tests__/liquidity.test.ts b/src/__tests__/liquidity.test.ts index 1cf962d..6a3d820 100644 --- a/src/__tests__/liquidity.test.ts +++ b/src/__tests__/liquidity.test.ts @@ -127,13 +127,13 @@ describe('StellarToolkit - Liquidity Pool Operations', () => { { field: 'totalShares', reserveA: 10, reserveB: 10, totalShares: 0 }, { field: 'totalShares', reserveA: 10, reserveB: 10, totalShares: NaN }, ])('rejects invalid $field', ({ field, reserveA, reserveB, totalShares }) => { - expect(() => toolkit.calculateLpAmount(1, 1, reserveA, reserveB, totalShares)).toThrow( - field, - ); + expect(() => toolkit.calculateLpAmount(1, 1, reserveA, reserveB, totalShares)).toThrow(field); }); it('rejects incomplete optional reserve inputs', () => { - expect(() => toolkit.calculateLpAmount(1, 1, 10)).toThrow('reserveA, reserveB, and totalShares'); + expect(() => toolkit.calculateLpAmount(1, 1, 10)).toThrow( + 'reserveA, reserveB, and totalShares', + ); }); it.each([ @@ -164,22 +164,25 @@ describe('StellarToolkit - Liquidity Pool Operations', () => { { field: 'minPrice', spotPrice: 4, minPrice: 'NaN', maxPrice: '4.5' }, { field: 'maxPrice', spotPrice: 4, minPrice: '3.5', maxPrice: 'Infinity' }, { field: 'minPrice', spotPrice: 4, minPrice: '5', maxPrice: '4.5' }, - ])('rejects invalid $field without submitting', async ({ field, spotPrice, minPrice, maxPrice }) => { - const user = Keypair.random(); - const server = (toolkit as unknown as { server: { submitTransaction: jest.Mock } }).server; - - await expect( - toolkit.depositLiquidity({ - accountSecret: user.secret(), - assetA: 'native', - assetB: usdc, - maxAmountA: '200', - maxAmountB: '50', - minPrice, - maxPrice, - spotPrice, - }), - ).rejects.toThrow(field); - expect(server.submitTransaction).not.toHaveBeenCalled(); - }); + ])( + 'rejects invalid $field without submitting', + async ({ field, spotPrice, minPrice, maxPrice }) => { + const user = Keypair.random(); + const server = (toolkit as unknown as { server: { submitTransaction: jest.Mock } }).server; + + await expect( + toolkit.depositLiquidity({ + accountSecret: user.secret(), + assetA: 'native', + assetB: usdc, + maxAmountA: '200', + maxAmountB: '50', + minPrice, + maxPrice, + spotPrice, + }), + ).rejects.toThrow(field); + expect(server.submitTransaction).not.toHaveBeenCalled(); + }, + ); }); diff --git a/src/core/liquidity.ts b/src/core/liquidity.ts index a55c3e4..90fb365 100644 --- a/src/core/liquidity.ts +++ b/src/core/liquidity.ts @@ -55,9 +55,7 @@ export class LiquidityPoolManager { throw new Error('amountB must be a finite positive number'); } - const hasReserveInputs = [reserveA, reserveB, totalShares].some( - (value) => value !== undefined, - ); + const hasReserveInputs = [reserveA, reserveB, totalShares].some((value) => value !== undefined); const hasAllReserveInputs = [reserveA, reserveB, totalShares].every( (value) => value !== undefined, );