Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

PumpSwap: refuse exhausted pools on every quoting path

nirholas07e4ea5112eb
diff --git a/api/agents/solana-trade.js b/api/agents/solana-trade.js
index 8c69a1f62..e5e582f17 100644
--- a/api/agents/solana-trade.js
+++ b/api/agents/solana-trade.js
@@ -206,6 +206,15 @@ async function loadAmm(network, mintPk) {
if (resolvedQuote !== WSOL_MINT) {
throw typed(409, 'unsupported_quote', 'this coin trades against a non-SOL asset — trade it from its coin page instead');
}
+ // `Pool.virtual_quote_reserves` is a SIGNED i128, so effective depth
+ // (vault + virtual) can land at or below zero: a pool that cannot absorb a
+ // trade. Refuse here rather than quote it, because `derivePriceImpact`
+ // reports a non-positive reserve as 0% impact, i.e. as the safest possible
+ // trade, which would sail straight past the max-price-impact guard.
+ const effective = BigInt(amm.effectiveQuoteReserve?.toString?.() ?? 0);
+ if (effective <= 0n) {
+ throw typed(409, 'pool_depth_empty', 'this pool has no tradable SOL depth right now, try again later');
+ }
return amm;
}
diff --git a/api/pump-fun-mcp.js b/api/pump-fun-mcp.js
index 775a0b6a4..36b08f5c6 100644
--- a/api/pump-fun-mcp.js
+++ b/api/pump-fun-mcp.js
@@ -470,6 +470,14 @@ async function handleQuoteSwap({
feeConfig,
} = state;
const amountBn = new BN(String(amountIn));
+ // `Pool.virtual_quote_reserves` is a SIGNED i128, so effective depth
+ // (vault + virtual) can be zero or negative: a pool that cannot absorb a
+ // trade. Refuse rather than quote it, because the impact math below clamps
+ // with Math.max(0, …) and would report such a pool as 0 bps, the most
+ // attractive quote we can return.
+ if (BigInt(effectiveQuoteReserve.toString()) <= 0n) {
+ throw rpcError(-32004, 'pool has no tradable quote depth');
+ }
// pump-swap-sdk takes slippage as a PERCENT (1 = 1%): `1 ± slippage / 100`.
const slip = (slippageBps ?? 100) / 100;
// `virtualQuoteReserves` is a separate arg the SDK adds to `quoteReserve`
diff --git a/docs/pumpfun-program/UPSTREAM-pumpswap-virtual-quote-reserves.md b/docs/pumpfun-program/UPSTREAM-pumpswap-virtual-quote-reserves.md
index 82698a33c..f1eaa4e60 100644
--- a/docs/pumpfun-program/UPSTREAM-pumpswap-virtual-quote-reserves.md
+++ b/docs/pumpfun-program/UPSTREAM-pumpswap-virtual-quote-reserves.md
@@ -151,17 +151,37 @@ Surfaces updated to price on effective reserves:
reserves, so a pool holding real quote-side depth virtually is not failed as
`pool_reserves_empty`.
- `workers/agent-sniper/amm-exit.js` — position exits and their price-impact
- circuit breaker. Because the virtual figure is signed, a quote here refuses a
- pool whose effective depth is non-positive (`amm_quote_depth_empty`) instead of
- pricing it. The refusal is load-bearing: the impact math reports a non-positive
- reserve as 0% impact, i.e. as the safest possible trade, so without it the
- breaker would wave through exactly the pool it exists to stop.
+ circuit breaker.
- `workers/agent-mm/market.js`, `workers/agent-orders/market.js` — the market
- maker's price and the order book's derived market cap.
+ maker's price and the order book's derived market cap. These return no price at
+ all for a non-positive effective reserve, rather than a misleading one.
-Regression coverage is in [`tests/agent-sniper-amm-exit.test.js`](../../tests/agent-sniper-amm-exit.test.js):
-one test asserts the virtual figure reaches the SDK un-summed, another asserts
-price impact is measured against effective reserves.
+### Refusing an exhausted pool
+
+Because the virtual figure is signed, effective depth can be zero or negative:
+a pool that cannot absorb a trade. Every path that quotes must refuse such a pool
+outright rather than price it, because all of our impact math clamps the result
+to a floor of zero and would therefore report an exhausted pool as **0% impact,
+the safest-looking trade available**. That number then sails past the very guards
+meant to stop it (the sniper's circuit breaker, the agent `max_price_impact_pct`
+limit).
+
+| Surface | Refusal |
+|---|---|
+| `workers/agent-sniper/amm-exit.js` | throws `amm_quote_depth_empty` |
+| `api/agents/solana-trade.js` (`loadAmm`) | HTTP 409 `pool_depth_empty` |
+| `api/pump-fun-mcp.js` (`quoteSwap`) | JSON-RPC `-32004` |
+| `src/pump/pump-swap-quote.js` | throws `no tradable quote depth` |
+
+A `0` returned by any impact helper means "unknown", never "safe".
+
+Regression coverage lives in
+[`tests/agent-sniper-amm-exit.test.js`](../../tests/agent-sniper-amm-exit.test.js)
+and [`tests/pump-swap-quote.test.js`](../../tests/pump-swap-quote.test.js): the
+virtual figure must reach the SDK un-summed and signed, impact must be measured
+against effective reserves, a negative that exhausts depth must be refused before
+any quote is attempted, and a negative that merely reduces depth must still price
+normally against the reduced reserve.
## Versions
diff --git a/src/pump/pump-swap-quote.js b/src/pump/pump-swap-quote.js
index fdb9ef97d..71dba0fe6 100644
--- a/src/pump/pump-swap-quote.js
+++ b/src/pump/pump-swap-quote.js
@@ -84,11 +84,19 @@ export async function quoteSwap({ inputMint, outputMint, amountIn, slippageBps =
// pump-swap-sdk takes slippage as a PERCENT (1 = 1%): `1 ± slippage / 100`.
const slippage = slippageBps / 100;
// PumpSwap prices against effective quote reserves (raw vault + the pool's
- // virtual_quote_reserves, non-zero on launchpad coins since 2026-07-20). The
- // SDK adds `virtualQuoteReserves` to `quoteReserve` internally, so pass the
- // raw reserve here; the spot-price baseline below uses the summed value.
+ // virtual_quote_reserves, non-zero on boost pools since 2026-07-20). The SDK
+ // adds `virtualQuoteReserves` to `quoteReserve` internally, so pass the raw
+ // reserve here; the spot-price baseline below uses the summed value.
+ //
+ // The virtual figure is a SIGNED i128, so effective depth can be zero or
+ // negative: a pool that cannot absorb a trade. Refuse rather than quote it,
+ // because the impact math below clamps with Math.max(0, …) and would report
+ // such a pool as 0 bps, the most attractive quote we can return.
const virtualQuoteReserves = new BN((pool.virtualQuoteReserves ?? 0).toString());
const effectiveQuoteReserve = poolQuoteAmount.add(virtualQuoteReserves);
+ if (effectiveQuoteReserve.lten(0)) {
+ throw new Error(`Pool ${poolKey.toBase58()} has no tradable quote depth`);
+ }
const shared = {
slippage,
baseReserve: poolBaseAmount,
diff --git a/tests/pump-swap-quote.test.js b/tests/pump-swap-quote.test.js
index a4a93a8c8..b725a73dd 100644
--- a/tests/pump-swap-quote.test.js
+++ b/tests/pump-swap-quote.test.js
@@ -142,6 +142,52 @@ describe('quoteSwap', () => {
expect(result.priceImpactBps).toBe(200);
});
+ // `Pool.virtual_quote_reserves` is a signed i128. A negative large enough to
+ // wipe out effective depth means the pool cannot absorb a trade, and the
+ // impact math clamps with Math.max(0, …) — so an unrefused quote would come
+ // back as 0 bps, the most attractive number the endpoint can return.
+ it('refuses a pool whose effective quote depth is wiped out by a negative virtual', async () => {
+ mockSwapSolanaState.mockResolvedValueOnce(
+ fakeState({
+ poolQuoteAmount: new BN(1_000_000),
+ pool: {
+ ...fakeState().pool,
+ virtualQuoteReserves: new BN(-1_000_000),
+ },
+ }),
+ );
+
+ await expect(
+ quoteSwap({ inputMint: TOKEN, outputMint: WSOL, amountIn: 10_000 }),
+ ).rejects.toThrow(/no tradable quote depth/);
+ expect(mockSellBaseInput).not.toHaveBeenCalled();
+ });
+
+ // A negative virtual that only reduces depth is still tradable: it must price
+ // against the reduced effective reserve, while the SDK still receives the raw
+ // vault balance and the signed virtual as separate arguments.
+ it('prices against reduced depth when a negative virtual does not exhaust the pool', async () => {
+ mockSwapSolanaState.mockResolvedValueOnce(
+ fakeState({
+ poolBaseAmount: new BN(1_000_000),
+ poolQuoteAmount: new BN(2_000_000),
+ pool: {
+ ...fakeState().pool,
+ virtualQuoteReserves: new BN(-1_000_000),
+ },
+ }),
+ );
+ mockSellBaseInput.mockReturnValueOnce({ uiQuote: new BN(9_800) });
+
+ const result = await quoteSwap({ inputMint: TOKEN, outputMint: WSOL, amountIn: 10_000 });
+
+ // effective quote = 2e6 - 1e6 = 1e6, so the impact matches the baseline case.
+ expect(result.priceImpactBps).toBe(200);
+ const call = mockSellBaseInput.mock.calls[0][0];
+ expect(call.quoteReserve.toString()).toBe('2000000');
+ expect(call.virtualQuoteReserves.toString()).toBe('-1000000');
+ });
+
it('uses default slippageBps of 100 when not provided', async () => {
mockSwapSolanaState.mockResolvedValueOnce(fakeState());
mockBuyQuoteInput.mockReturnValueOnce({ base: new BN(5_000) });