services
Get a key

library / Use cases / Exchanges, custodians and on-ramps / UC0109

Quoted vs delivered amount audit

What delivered means for a swap, how to set it against the quoted amount and the slippage floor, where a gap comes from, what the lookups cost on Carbium, and a read-only Node script that prints the verdict.

By Carbium · Exchanges, custodians and on-ramps · UC0109 · updated 2026-11-23

01

What delivered means

A customer swaps inside your app. The quote said 1,000.00 USDC out and the wallet shows 996.40. Support opens a ticket, and the first job is to agree on which number is the delivered one. It is the change in the owner's token balance across the transaction, read from the meta record the validator writes. preTokenBalances and postTokenBalances list every token account the transaction touched, and each entry carries owner, mint and uiTokenAmount.amount, a string of base units. Anything else, such as an app screen or a notification, is a claim about what should have happened.

Compare in integers. USDC has 6 decimals, so 1,000.00 is 1000000000 base units. Parse the amount string as a BigInt and ignore the float uiAmount, which rounds and can turn a clean swap into a ticket or hide a real shortfall.

02

Quote, minimum and delivered

A quote is a prediction made at one moment. Carbium answers it with the quote method as JSON-RPC on the same RPC endpoint as your reads. The `swap` method then builds an unsigned transaction that you sign and send through any RPC, so the quote and the landing are two events with a signing step between them. When you send, store the quoted output amount and the slippage you asked for next to the signature. Without that pair an audit has nothing to compare against.

NumberWhere it comes fromWhat it tells you
Quoted outStored from the quote responseWhat the route predicted
Minimum outQuoted out times (10000 minus slippage bps), divided by 10000The floor the transaction was built to enforce
DeliveredPost balance minus pre balance for the owner and the output mintWhat landed

Three verdicts follow. Delivered at or above the quote means the customer got what was promised, and the complaint is about something else, usually a different token or a different wallet. Delivered between the minimum and the quote means slippage worked as designed: the pool moved against the user inside the tolerance they accepted. Delivered below the minimum on a transaction that succeeded points at your records. A swap built with a floor fails when it cannot meet it, so a landed swap under its floor usually means a fee leg in the same transaction took part of the output, or the stored amount is in the wrong decimals, the wrong mint or a looser slippage than you recorded. Swap slippage settings covers choosing the tolerance.

03

Where a gap comes from

Most tickets resolve into one of these causes, and the audit output points at which.

  • Price movement. Pools change every slot, about 200 ms each. A user who takes ten seconds to approve a quote is signing against a number about 50 slots old. Record the quote's age at signing.
  • The router fee. A Carbium quote carries a router fee, platformFee, of 5 bps (0.05%), charged on chain in the transaction. On a 1,000 USDC swap that is 0.50 USDC, already a visible part of a 3.60 gap. Read the quote response to see how it presents the fee, and record which side of it your stored amount is on.
  • A transfer fee on the output mint. A Token-2022 mint that charges a transfer fee delivers less than was sent, and the balance change is what remains after it.
  • Several token accounts. One wallet can hold more than one token account for a mint, and the swap may pay into a different one than the account your screen reads. Sum by owner and mint.
  • Other legs in the same transaction. If your app sends a service fee in the output mint from the same wallet, the owner's net is the swap output minus that fee. The net hides the path, so read the inner instructions when the split matters.
  • Native SOL output. SOL comes back through a wrapped SOL account that the transaction may close, and the user's gain then shows only in the lamport balances, preBalances and postBalances. The script below audits token mints only.
04

The audit in one file

The script takes the output mint. With no signature it lists the newest finalized signatures for that mint, fetches each with getTransaction in jsonParsed encoding and picks the first successful one where the fee payer's wallet gained the mint and lost another. When none of the first few qualifies it audits the first successful transaction it fetched and says so. Mainnet carries v1 transactions, and with maxSupportedTransactionVersion absent or 0 Carbium answers -32015 for one, uncharged, so the call passes 1. The key goes in the `x-api-key` header.

main.ts
// UC0109 · Quoted vs delivered amount audit// https://carbium.sh/library/usecase/quoted-vs-delivered-amount-audit// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MIT// Read-only: how much of an output mint one swap delivered to its fee payer, checked against a stored quote.import { address, createDefaultRpcTransport, createSolanaRpcFromTransport, signature } from "@solana/kit";const key = process.env.CARBIUM_KEY;if (!key) { console.error("Set CARBIUM_KEY to your Carbium API key."); process.exit(1); }const url = process.env.CARBIUM_RPC_URL ?? "https://services.carbium.sh";const [mint, sigIn, quotedIn, bpsIn = "50"] = process.argv.slice(2);if (!mint) { console.error("Usage: node main.ts <outMint> [signature|-] [quotedBaseUnits slippageBps]"); process.exit(1); }const rpc = createSolanaRpcFromTransport(createDefaultRpcTransport({ url, headers: { "x-api-key": key } }));type Bal = { mint: string; owner?: string; uiTokenAmount: { amount: string } };type Tx = { slot: bigint; transaction: { message: { accountKeys: { pubkey: string }[] } }; meta: { err: unknown; preTokenBalances?: Bal[] | null; postTokenBalances?: Bal[] | null } | null };// Mainnet carries v1 transactions: a maxSupportedTransactionVersion of 0 or none gets -32015 for one.const getTx = async (s: string) =>  (await rpc.getTransaction(signature(s), { encoding: "jsonParsed", maxSupportedTransactionVersion: 1, commitment: "finalized" }).send()) as unknown as Tx | null;// Net change per mint for one owner, summed over every token account; a missing side counts as 0.function deltas(tx: Tx, who: string): Map<string, bigint> {  const d = new Map<string, bigint>();  const add = (list: Bal[] | null | undefined, sign: bigint) => {    for (const b of list ?? []) if (b.owner === who) d.set(b.mint, (d.get(b.mint) ?? 0n) + sign * BigInt(b.uiTokenAmount.amount));  };  add(tx.meta?.preTokenBalances, -1n);  add(tx.meta?.postTokenBalances, 1n);  return d;}const payerOf = (tx: Tx) => tx.transaction.message.accountKeys[0]!.pubkey;const isSwap = (tx: Tx) => {  const d = deltas(tx, payerOf(tx));  return (d.get(mint!) ?? 0n) > 0n && [...d].some(([m, v]) => m !== mint && v < 0n);};let sig = sigIn && sigIn !== "-" ? sigIn : undefined;let tx: Tx | null = sig ? await getTx(sig) : null;if (!sig) {  const page = await rpc.getSignaturesForAddress(address(mint), { limit: 20, commitment: "finalized" }).send()    .catch((e): never => { console.error("getSignaturesForAddress failed:", String(e)); process.exit(1); });  let first: { sig: string; tx: Tx } | undefined;  for (const s of page.filter((p) => p.err === null).slice(0, 6)) {    try {      const t = await getTx(s.signature);      if (!t?.meta) continue;      first ??= { sig: s.signature, tx: t };      if (isSwap(t)) { sig = s.signature; tx = t; break; }    } catch (e) { console.error("skipped", s.signature, String(e)); }  }  if (!sig && first) { sig = first.sig; tx = first.tx; console.log("no swap among the newest transactions; auditing the first one fetched"); }}if (!sig || !tx?.meta) { console.error("No transaction found at finalized for", mint); process.exit(1); }const payer = payerOf(tx);const d = deltas(tx, payer);console.log(`tx ${sig}\nslot ${tx.slot}, payer ${payer}, ${tx.meta.err ? "FAILED" : "succeeded"}`);for (const [m, v] of d) if (v !== 0n) console.log(`${m} ${v > 0n ? "+" : ""}${v}`);const delivered = d.get(mint) ?? 0n;console.log(`delivered ${delivered} base units of ${mint}`);if (quotedIn) {  const quoted = BigInt(quotedIn);  const minimum = (quoted * (10000n - BigInt(bpsIn))) / 10000n;  const verdict = tx.meta.err ? "FAILED" : delivered >= quoted ? "ABOVE QUOTE" : delivered >= minimum ? "WITHIN SLIPPAGE" : "BELOW MINIMUM";  console.log(`${verdict}: delivered ${delivered}, quoted ${quoted}, minimum ${minimum} at ${bpsIn} bps, gap ${quoted - delivered}`);}

run: CARBIUM_KEY=$CARBIUM_KEY node main.ts <outMint> [signature|-] [quotedBaseUnits slippageBps]

It prints the payer's balance change for every mint in the transaction, then the delivered amount for the output mint. Give it your stored quote and slippage and it adds a verdict: ABOVE QUOTE, WITHIN SLIPPAGE, BELOW MINIMUM or FAILED when the transaction errored. Pass a dash for the signature to let it pick one. Every read sets finalized, so the audit only judges swaps that can no longer roll back. The script signs and sends nothing. It reports any call that fails, and a failed transaction lookup is skipped for the next one.

05

Auditing every swap

One ticket needs one lookup. A routine audit of every swap needs a queue. Keep the signature, the quoted amount and the slippage in your own table when the swap is sent, and run the check on each row once it is final. Listing signatures for the customer's output token account with getSignaturesForAddress finds swaps you did not record, which is useful when a customer used another front end.

MethodCells per callFree req/sDev req/sBusiness req/sPro req/s
getSignaturesForAddress500.241.2100500
getTransaction500.241.2525

An audit costs one getTransaction at 50 cells, so the plan's req/s for that method sets how fast a backlog clears. Signature pages come from an address index that keeps up to 30 days, and the transaction behind an older signature can come back null, so audit within the window and keep your own record after it. For swaps that are the second leg of a fiat purchase, On-ramp fulfilment tracker shows how to tie the swap to the order it settles.

06

Where it ends

  • The audit explains a gap after the fact. It cannot move funds or fix a quote, and the script is read-only.
  • It audits the fee payer's wallet. When you sponsor fees, the payer is your account and the customer's owner address must replace it in the script.
  • A single transaction is read. A swap split across several transactions needs your own ledger to total the legs.
  • The verdict is only as good as the stored quote and slippage. Without them the script reports what landed and nothing more.
  • Native SOL output needs the lamport change and the fee handled by hand.
  • A method Carbium does not serve answers -32601: check the method name and do not retry.
07

When it goes wrong

what goes wrongwhat to do
The delivered amount is compared as a float and a 1,000.00 swap fails or passes by rounding.Use uiTokenAmount.amount as a BigInt in base units and store the quote in base units too.
The delivered amount is read from one token account and the swap paid into another account of the same owner.Sum by owner and mint across every token account in the transaction.
getTransaction fails with -32015 on a swap that used a v1 transaction.Pass maxSupportedTransactionVersion: 1 on every getTransaction call. With the value absent or 0, Carbium answers -32015 for a v1 transaction.
The audit reads at confirmed because a call left commitment out.Set commitment: "finalized" on getSignaturesForAddress and getTransaction. Kit fills in confirmed when the field is missing.
An old swap returns null from getTransaction or an empty signature page.The address index keeps up to 30 days, and its pages can list a signature the node no longer holds. Audit inside the window and keep your own record of swaps.
The gap is blamed on slippage when the router fee and a service fee explain it.Check whether your stored quote is before or after the 5 bps router fee, and add back any fee your app sends from the same wallet in the output mint before judging against the minimum.
08

Questions

Is a delivered amount below the quote a bug?

Not by itself. A quote predicts, and slippage allows the pool to move inside the tolerance the user set. It is a problem when delivered falls under the minimum on a succeeded transaction, which points at your stored numbers.

What should I store when I request a quote?

The output mint, the quoted output in base units, the slippage in bps, the time of the quote, and the signature once you send. The audit needs the first four to judge and the last to find the transaction.

Why does an explorer show a different amount than my script?

Explorers often show one token account and a rounded figure. The script sums every account the owner holds for the mint and keeps integers, so it matches the ledger.

What does one audit cost on Carbium?

One getTransaction at 50 cells. Listing signatures first costs another 50 cells.