library / How to use Solana / HS0073
Quote mint validation, vendor-neutral
One TypeScript file checks mint strings in three steps before a quote request: the string decodes to 32 bytes, the account exists, and a token program owns it as a mint. It then quotes the valid pair and prints each input with its verdict.
What WrongSize means
A Solana address is 32 bytes. Written as text it is a base58 string, usually 43 or 44 characters long, and a mint address looks like any other address. When a quote service receives inputMint, it decodes that string back into bytes. If the result is not exactly 32 bytes, the parser stops with a size error, and the WrongSize you saw is that error.
The error says something about the string and nothing about the token. It does not tell you whether the mint exists, has liquidity or has a route. A string that fails the size check never reaches any lookup, so the fix sits on your side: decode the string yourself first and send a request only for strings that pass.
Base58 has no length marker and no checksum. The decoder cannot tell a lost character from a different address, so it reports what it can see, a byte count that is not 32. Cut a paste short by more than a few characters and the decoded length drops under 32. A string that lost a single character can still decode to 32 bytes as a different address, and only a later check catches that one.
How a mint string goes bad
Most bad mints come from the path between the place a developer copied the address and the place the request is built.
| Cause | Example | What the decoder sees |
|---|---|---|
| A cut paste | The first 40 characters of a 44-character mint | Fewer than 32 bytes, so a size error |
| Padding or whitespace | A trailing space, tab or newline from a config file or a form | A character base58 does not have |
| A symbol in place of an address | SOL or USDC typed into a mint field | Too short, or an invalid character (the O in SOL) |
| Characters base58 lacks | A zero, a capital O, a capital I or a lowercase l | An invalid character |
| Extra characters | A trailing comma, or two addresses pasted together | More than 32 bytes, or an invalid character |
The base58 alphabet leaves out 0, O, I and l because they look alike. A mint retyped from a screenshot is the usual way one of them gets in. Depending on the decoder, the same bad string produces a size error or an invalid-character error, so a caller that only matches the text WrongSize will miss the second kind.
The three checks
Run these in order. Step 1 makes no request, and steps 2 and 3 share one account read.
- Decode. isAddress from @solana/kit returns true only when the string is base58 and decodes to exactly 32 bytes. It runs on every input and costs nothing on the network.
- Exists. getMultipleAccounts answers null for an address with no account. A mistyped string that still decodes to 32 bytes ends here.
- Is a mint. The account owner must be a token program, either the classic one starting Tokenkeg or Token-2022 starting Tokenz, and the data must have a mint's layout. A classic mint is 82 bytes. A Token-2022 mint with extensions is longer, padded to 165 bytes, with the value 1 at byte 165 marking it a mint.
The layout test matters because a token account is owned by the same two programs. A holder's token account is 165 bytes in the classic program, and an owner check alone would wave it through as a mint. The mint address sits in the first 32 bytes of that account's data, which is where to look when a token account address was pasted by mistake.
getMultipleAccounts takes the whole list of survivors, so ten mints cost one request. The code leaves the commitment out, and @solana/kit 8.6.0 fills in confirmed for a call that has none.
The code
Install @solana/kit at 8.6.0, export CARBIUM_KEY and run the file with Node 24, which strips the types on load. It checks seven built-in samples, five bad and two good, plus any strings you pass as arguments. The bad ones are the symbol SOL, a mint with a trailing space, a mint cut to 40 characters, a mint with a zero in place of its first letter and the System Program address, which is a valid 32-byte string and not a mint.
// HS0073 · Quote mint validation, vendor-neutral// https://carbium.sh/library/howsolana/quote-mint-validation-vendor-neutral// Copyright (c) Carbium Corporation. MIT License (LICENSE). Keep this notice.// SPDX-License-Identifier: MITimport { createDefaultRpcTransport, createSolanaRpcFromTransport, isAddress } 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 rpc = createSolanaRpcFromTransport(createDefaultRpcTransport({ url, headers: { "x-api-key": key } }));const WSOL = "So11111111111111111111111111111111111111112";const USDC = "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v";const TOKEN_PROGRAMS = new Set(["TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA", "TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"]);// Bad samples first, then two good mints, then any strings from the command line.const inputs = ["SOL", `${USDC} `, USDC.slice(0, 40), USDC.replace("E", "0"), "11111111111111111111111111111111", WSOL, USDC, ...process.argv.slice(2)];const verdict = new Map<string, string>();// Step 1: the string must decode to exactly 32 bytes. No request is made.for (const s of inputs) if (!isAddress(s)) verdict.set(s, "failed step 1: not a 32-byte base58 address");// Steps 2 and 3: one getMultipleAccounts call (kit fills in confirmed commitment) for the survivors.const survivors = [...new Set(inputs.filter(isAddress))].slice(0, 100);const mints: string[] = [];try { const { value } = await rpc.getMultipleAccounts(survivors, { encoding: "base64" }).send(); survivors.forEach((s, i) => { const acc = value[i]; if (!acc) return void verdict.set(s, "failed step 2: no account"); const data = Buffer.from(acc.data[0], "base64"); // Classic mint: 82 bytes. Token-2022 mint with extensions: account type 1 at byte 165. const isMint = TOKEN_PROGRAMS.has(acc.owner) && (data.length === 82 || (data.length > 165 && data[165] === 1)); if (!isMint) return void verdict.set(s, `failed step 3: owner ${acc.owner}, ${data.length} bytes, not a mint`); verdict.set(s, "passed all three steps"); mints.push(s); });} catch (e) { console.error("account read failed:", e instanceof Error ? e.message : e);}// JSON.stringify shows padding inside the quotes.for (const s of inputs) console.log(`${JSON.stringify(s).padEnd(48)} ${verdict.get(s) ?? "not checked"}`);// The first valid pair goes to quote: 100000000 base units is 0.1 of a 9-decimal token.const [src, dst] = mints;if (src && dst) { try { const res = await fetch(url, { method: "POST", headers: { "content-type": "application/json", "x-api-key": key }, body: JSON.stringify({ jsonrpc: "2.0", id: 1, method: "quote", params: [{ src_mint: src, dst_mint: dst, amount_in: 100000000, slippage_bps: 50 }] }), }); const reply = (await res.json()) as { result?: Record<string, unknown>; error?: { code: number; message: string } }; if (reply.error) console.log(`quote failed: ${reply.error.code} ${reply.error.message}`); else console.log("quote:", JSON.stringify({ destAmountOut: reply.result?.destAmountOut, minAmountOut: reply.result?.minAmountOut })); } catch (e) { console.log("quote failed:", e instanceof Error ? e.message : e); }} else console.log("quote skipped: fewer than two valid mints");
run: node main.ts [extra-mint-string ...]
Each input prints through JSON.stringify, so a trailing space or a newline shows inside the quotes. The verdict names the step that failed. The first two inputs that pass go to quote as a pair, with 100000000 base units of the first and 50 basis points of slippage. Wrapped SOL has 9 decimals, so that amount is 0.1 SOL. The reply prints destAmountOut and minAmountOut, in the output token's base units.
The quote call sits in its own try block, apart from the account read. If the account read fails, the file prints why and still shows the step 1 verdicts. If the quote fails, the verdicts are already on screen.
Keys and scope
Both the account read and the quote go to the RPC endpoint, https://services.carbium.sh, with the key in the x-api-key header. The quote is a JSON-RPC POST on that endpoint, one of four methods: quote, swap, health and prices.
A key calls quote only once it carries the QUOTE scope. Without it the call comes back as HTTP 401 with -32001, and the file prints that code and message on the quote line while the verdicts above it stay correct. Give the key the scope under API keys and send the whole key.
Below the Pro plan a getMultipleAccounts call is capped at 100 pubkeys by the burst limit, and each pubkey costs 10 cells, so the file slices its list at 100 and prints "not checked" for each input past that. A longer list needs several calls. The 300-pubkey limit is Pro's.
Where it ends
A mint that passes all three steps is a real token mint on mainnet. Whether it has a route, pool depth or a quotable pair at all is answered by the quote itself, and the file prints whatever that answers.
The file never sends a malformed mint to quote, so it records no reply for one. What a given service answers for a bad mint depends on the service, and the size check works the same way on all of them because it needs only the 32-byte rule.
Step 3 reads a mint's layout and does not read its extensions. A Token-2022 mint can carry a transfer fee or a transfer hook, and those change what a swap delivers or whether it lands. For the fee side see Transfer fee calculator.
A valid, existing mint can still be the wrong mint. A look-alike token with a copied name passes every check here. Checking that the address is the one you intended is a comparison against a list you trust, and no decoder does it for you.
Read the quote itself in Solana swap API quote. For a larger list of addresses checked before a run, see Bulk payout preflight check.
When it goes wrong
| what goes wrong | what to do |
|---|---|
| Step 1 fails on a string that looks right. | Print it with JSON.stringify to expose a trailing space, a newline or a zero-width character, and trim at the point where the input enters your code. If the string holds a 0, O, I or l, it was retyped or read from a font that confuses them, so copy it from the source again. |
| Step 1 passes and step 2 finds no account. | The string decodes to 32 bytes and still names nothing. It is usually a one-character slip in a pasted address, or a mint that was never created on mainnet. Compare it against the source it came from. |
| Step 3 fails with a token program as owner and 165 bytes of data. | The address holds a token account, and a quote needs a mint. The mint is the first 32 bytes of that account's data, so turn them into the mint address with getAddressDecoder().decode(data.subarray(0, 32)) from @solana/kit and use that value as the mint. |
| The quote line prints 401 -32001. | The key lacks the QUOTE scope. Add the scope to the key and send the whole key. The verdicts above the quote line are unaffected. |
Questions
Does a base58 string that decodes to 32 bytes guarantee a real mint?
No. The string only has the right size, so it can be a wallet, a program or an address with no account. Steps 2 and 3 show a mint.
Why not send the quote and read the error?
A parse error from a remote service names a parameter and a rule. Your own check names the input and the step, works on a whole list in one pass and sends nothing for a string that cannot work.
How many mints can I check in one call?
Up to 100 pubkeys per getMultipleAccounts call on plans below Pro. A call of 300 pubkeys is Pro's. The file slices at 100.
Do Token-2022 mints pass the same check?
Yes. The owner test lists both token programs, and the layout test accepts the 82-byte classic mint and a Token-2022 mint with the value 1 at byte 165.