Skip to content
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
74 changes: 74 additions & 0 deletions src/__tests__/liquidity.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,78 @@ 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();
},
);
});
70 changes: 54 additions & 16 deletions src/core/liquidity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,61 @@ 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(
Expand All @@ -89,9 +118,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);
Expand Down
Loading