bitflow-market-maker-public/test/binShares.test.ts
Bought 6bf5f337bb Gate ticks on engine tip freshness and resolve bin shares on-chain
Two gaps the v2 engine docs surfaced.

Quote v2 serves from in-memory AMM state at the applied tip and has no
stale-DB fallback, so nothing was stopping us sizing an add against
reserves that trail the chain. Every tick now checks quotes/v2/status and
holds unless tip_lag is within BFF_MAX_TIP_LAG (default 0). A null lag
means the tip is unknown, which is not the same as caught up and is
treated as stale. v1 has no such surface and skips the gate.

Quote v2 also documents the bin ladder's `liquidity` as always null.
Today it is only null in the dust tail, well away from the curve, but
sizing must not depend on that: bin shares are the denominator for
minDlp, and defaulting a missing one to zero takes calcAddSlippage down
its empty-bin sqrt branch. That misprices minDlp in either direction --
it over-demands DLP on small adds, which aborts the transaction after
gas is spent, and under-demands on large ones, which quietly forfeits
the slippage protection the post-condition exists to provide. Missing
shares are now read from the pool's get-total-supply, which was verified
to match the inventory endpoint's binShares exactly on mainnet.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-09-15 20:20:44 -07:00

97 lines
3.5 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from 'vitest';
import { AddBinMath, PoolFees, calcAddSlippage } from '../src/liquidity';
// Quote v2 documents the bin ladder's `liquidity` as always null. Reading that
// as zero shares is not a rounding error: it switches calcAddSlippage onto the
// empty-bin branch, so these tests pin both the damage and the fallback.
const getBinTotalSupply = vi.fn();
vi.mock('../src/stacks', async () => ({
...(await vi.importActual<typeof import('../src/stacks')>('../src/stacks')),
getBinTotalSupply: (...args: unknown[]) => getBinTotalSupply(...args),
}));
const POOL = 'SM1FKXGNZJWSTWDWXQZJNF7B5TV5ZB235JTCXYXKD.dlmm-pool-sbtc-usdcx-v-1-bps-10';
const fees: PoolFees = {
xProtocol: 25,
xProvider: 25,
xVariable: 0,
yProtocol: 25,
yProvider: 25,
yVariable: 0,
};
beforeEach(() => {
getBinTotalSupply.mockReset();
});
describe('resolveBinShares', () => {
it('uses the ladder value when the BFF supplies one, with no chain reads', async () => {
const { resolveBinShares } = await import('../src/liquidity');
const shares = await resolveBinShares(POOL, [
{ bin_id: 664, liquidity: '695014326' },
{ bin_id: 665, liquidity: '540779927' },
] as never);
expect(shares.get(664)).toBe(695014326);
expect(shares.get(665)).toBe(540779927);
expect(getBinTotalSupply).not.toHaveBeenCalled();
});
it('reads total supply from the pool for bins the ladder nulls out', async () => {
const { resolveBinShares } = await import('../src/liquidity');
getBinTotalSupply.mockResolvedValue(BigInt('695014326'));
const shares = await resolveBinShares(POOL, [{ bin_id: 664, liquidity: null }] as never);
// The on-chain total must match what the inventory endpoint reports for the
// same bin -- verified against mainnet bin 664 on the sBTC/USDCx pool.
expect(shares.get(664)).toBe(695014326);
expect(getBinTotalSupply).toHaveBeenCalledWith(POOL, 664, expect.anything());
});
it('only pays for chain reads on the bins that are missing', async () => {
const { resolveBinShares } = await import('../src/liquidity');
getBinTotalSupply.mockResolvedValue(BigInt(1000));
await resolveBinShares(POOL, [
{ bin_id: 664, liquidity: '695014326' },
{ bin_id: 665, liquidity: null },
{ bin_id: 666, liquidity: undefined },
] as never);
expect(getBinTotalSupply).toHaveBeenCalledTimes(2);
});
});
describe('why the fallback exists', () => {
// Reserves/shares captured from mainnet bin 664 on the sBTC/USDCx pool.
const bin = (binShares: number, xAmount: number): AddBinMath => ({
isActiveBin: false,
binPriceScaled: 758326630,
reserveX: 2618844,
reserveY: 0,
binShares,
xAmount,
yAmount: 0,
});
const minDlp = (binShares: number, xAmount: number): bigint =>
calcAddSlippage(bin(binShares, xAmount), fees, 100).minDlp;
const SHARES = 695014326;
// Real DLP scales linearly with the add while the empty-bin branch scales with
// its square root, so reading a populated bin as empty misprices minDlp in
// *either* direction depending on size. Both outcomes are bad, which is why
// the shares have to be resolved rather than defaulted.
it('over-demands DLP on a small add, which aborts the transaction', () => {
expect(minDlp(0, 1000)).toBeGreaterThan(minDlp(SHARES, 1000) * BigInt(3));
});
it('under-demands DLP on a large add, which forfeits slippage protection', () => {
expect(minDlp(0, 100000) * BigInt(3)).toBeLessThan(minDlp(SHARES, 100000));
});
});