{"record":{"id":"54d5b07d9d72fca0","repo":"nautechsystems/nautilus_trader","slug":"input-token-balance-is-below-the-swap-amount","errorCode":null,"errorMessage":"Input token {} balance {} is below the swap amount {}","messagePattern":"Input token (.+?) balance (.+?) is below the swap amount (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/adapters/blockchain/src/execution/client.rs","lineNumber":5183,"sourceCode":"    }\n    .abi_encode();\n    let balance = required_verification(\n        executor\n            .verification\n            .verify_decoded_call(\n                None,\n                &plan.token_in,\n                U256::ZERO,\n                &balance_call,\n                block,\n                |result| ERC20::balanceOfCall::abi_decode_returns(result).map_err(Into::into),\n            )\n            .await,\n        \"swap input balance\",\n    )?;\n\n    if balance.value < plan.amount_in {\n        anyhow::bail!(\n            \"Input token {} balance {} is below the swap amount {}\",\n            plan.token_in,\n            balance.value,\n            plan.amount_in\n        );\n    }\n    decisions.push(verification_decision(&balance, Some(block), Some(block)));\n\n    Ok(decisions)\n}\n\n/// Converts a base-denominated order quantity into raw token units with exact integer\n/// scaling by the base token decimals.\n///\n/// `Quantity::raw` is scaled to the greater of its declared precision and `FIXED_PRECISION`;\n/// token amounts are scaled to the token's decimals. Quantities not exactly representable in\n/// token units are rejected rather than rounded.\nfn quantity_to_raw_amount(quantity: Quantity, decimals: u8) -> anyhow::Result<U256> {","sourceCodeStart":5165,"sourceCodeEnd":5201,"githubUrl":"https://github.com/nautechsystems/nautilus_trader/blob/18893faf8b356be3320add8de2f861b0b647cf06/crates/adapters/blockchain/src/execution/client.rs#L5165-L5201","documentation":"Before submitting a swap, the client checks the wallet's balance of the input token. This error is thrown when the balance is strictly less than the swap's amount_in, meaning the swap cannot be funded and would revert on-chain.","triggerScenarios":"Submitting a swap plan where token_in's balance (queried via the token contract) is below plan.amount_in — e.g. plan sized from stale balance data, tokens transferred out, or amount including fees the wallet cannot cover.","commonSituations":"Stale balance cache after recent transfers or another trade; token decimals mismatch causing an inflated amount_in; funds locked in another position or pending withdrawal; swap sized from a quote denominated in a different unit.","solutions":["Re-fetch the on-chain balance and resize the swap to amount_in <= balance","Verify token decimals are correct so amount_in is scaled properly","Transfer or acquire the shortfall of token_in before retrying","Check for other pending transactions consuming the same balance (nonce queue)"],"exampleFix":"// before: sizing from stale cached balance\nlet plan = SwapPlan::new(token_in, cached_balance);\n// after: refresh balance and clamp the amount\nlet balance = token.balance_of(wallet).await?;\nlet plan = SwapPlan::new(token_in, plan.amount_in.min(balance));","handlingStrategy":"validation","validationCode":"let balance = token.balance_of(wallet).await?;\nanyhow::ensure!(balance >= plan.amount_in, \"insufficient token_in balance: {} < {}\", balance, plan.amount_in);","typeGuard":null,"tryCatchPattern":"match res {\n    Err(e) if e.to_string().contains(\"balance\") => {\n        let balance = token.balance_of(wallet).await?;\n        submit_swap(plan.resized_to(balance)).await?;\n    }\n    other => other?,\n}","preventionTips":["Refresh on-chain balances immediately before sizing orders","Validate token decimals against the contract, not config alone","Account for pending outgoing transfers in available balance","Clamp swap sizes to current balance rather than cached values"],"tags":["blockchain","balance","erc20","swap"],"backgroundTag":"value-out-of-range","analyzedSha":"18893faf8b356be3320add8de2f861b0b647cf06","analyzedAt":"2026-09-08T20:49:34.690Z","contentChangedAt":"2026-09-08T20:49:34.690Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}