From 6ab4722de7a563fce87454967262355bf05e6f2b Mon Sep 17 00:00:00 2001 From: jakehobbs Date: Tue, 26 May 2026 11:18:03 -0700 Subject: [PATCH 1/2] fix(swap-docs): correct amount types to bigint + viem helpers for v5 SDK The JS SDK v5 takes bigint values for fromAmount/minimumToAmount, not hex strings. Use parseEther/parseUnits from viem for readability. Also clarify in the FAQ that bigints apply to the SDK while hex strings apply to the raw API. Co-Authored-By: Claude Opus 4.6 (1M context) --- .../pages/transactions/swap-tokens/client.mdx | 3 ++- .../pages/transactions/swap-tokens/index.mdx | 15 +++++++++------ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/content/wallets/pages/transactions/swap-tokens/client.mdx b/content/wallets/pages/transactions/swap-tokens/client.mdx index 7d80b8dbc..48ccd1af2 100644 --- a/content/wallets/pages/transactions/swap-tokens/client.mdx +++ b/content/wallets/pages/transactions/swap-tokens/client.mdx @@ -6,6 +6,7 @@ You need the following environment variables: ```ts title="requestQuote.ts" + import { parseEther } from "viem"; import { swapActions } from "@alchemy/wallet-apis/experimental"; import { client } from "./client"; @@ -16,7 +17,7 @@ You need the following environment variables: const { quote, ...calls } = await swapClient.requestQuoteV0({ fromToken: "0x...", toToken: "0x...", - minimumToAmount: "0x...", + minimumToAmount: parseEther("0.0001"), }); // Display the swap quote, including the minimum amount to receive and the expiry diff --git a/content/wallets/pages/transactions/swap-tokens/index.mdx b/content/wallets/pages/transactions/swap-tokens/index.mdx index 61ce864be..5ad67fc0e 100644 --- a/content/wallets/pages/transactions/swap-tokens/index.mdx +++ b/content/wallets/pages/transactions/swap-tokens/index.mdx @@ -31,15 +31,17 @@ Swaps work like any other Smart Wallet transaction, so you can sponsor gas for g When requesting a swap quote, specify either an amount in, or a minimum amount out. ```tsx +import { parseUnits, parseEther } from "viem"; + // Mode 1: Swap exact input amount { - fromAmount: "0x2710"; -} // Swap exactly 0.01 USDC (10000 in hex, 6 decimals) + fromAmount: parseUnits("0.01", 6); +} // Swap exactly 0.01 USDC // Mode 2: Get minimum output amount { - minimumToAmount: "0x5AF3107A4000"; -} // Get at least 0.0001 ETH (18 decimals). We calculate how much USDC you need to spend to get at least your desired ETH amount. + minimumToAmount: parseEther("0.0001"); +} // Get at least 0.0001 ETH. We calculate how much USDC you need to spend to get at least your desired ETH amount. ``` ## Prerequisites @@ -76,10 +78,11 @@ Currently, the Swap API supports only single-chain swaps. Cross-chain swaps are ## How do you encode values? -Values are passed as hexadecimal strings. The Swap API does not add complexity to consider decimals, so 0x01 is always the smallest amount of a given asset. +**JavaScript SDK**: amounts are passed as `bigint` values. For example, 1 ETH is `1_000_000_000_000_000_000n` and 1 USDC is `1_000_000n`. + +**Raw API**: values are passed as hexadecimal strings. The Swap API does not add complexity to consider decimals, so `0x01` is always the smallest amount of a given asset. 1 ETH, or DAI (18 decimals) is `0xDE0B6B3A7640000` 1 USDC (6 decimals) is `0xF4240` -This removes any ambiguity— if it’s numerical, it’s a hex. ## What is the expiry? From 63dc1bb02d9e3d5c6febf08bd1c14de9b1b3f4c2 Mon Sep 17 00:00:00 2001 From: jakehobbs Date: Tue, 26 May 2026 11:20:42 -0700 Subject: [PATCH 2/2] fix(swap-docs): use parseUnits consistently; decimals depend on toToken Co-Authored-By: Claude Opus 4.6 (1M context) --- content/wallets/pages/transactions/swap-tokens/client.mdx | 4 ++-- content/wallets/pages/transactions/swap-tokens/index.mdx | 8 ++++---- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/content/wallets/pages/transactions/swap-tokens/client.mdx b/content/wallets/pages/transactions/swap-tokens/client.mdx index 48ccd1af2..a1aff1af9 100644 --- a/content/wallets/pages/transactions/swap-tokens/client.mdx +++ b/content/wallets/pages/transactions/swap-tokens/client.mdx @@ -6,7 +6,7 @@ You need the following environment variables: ```ts title="requestQuote.ts" - import { parseEther } from "viem"; + import { parseUnits } from "viem"; import { swapActions } from "@alchemy/wallet-apis/experimental"; import { client } from "./client"; @@ -17,7 +17,7 @@ You need the following environment variables: const { quote, ...calls } = await swapClient.requestQuoteV0({ fromToken: "0x...", toToken: "0x...", - minimumToAmount: parseEther("0.0001"), + minimumToAmount: parseUnits("1", 6), // decimals must match toToken (e.g. 6 for USDC, 18 for ETH) }); // Display the swap quote, including the minimum amount to receive and the expiry diff --git a/content/wallets/pages/transactions/swap-tokens/index.mdx b/content/wallets/pages/transactions/swap-tokens/index.mdx index 5ad67fc0e..8f2bb6f91 100644 --- a/content/wallets/pages/transactions/swap-tokens/index.mdx +++ b/content/wallets/pages/transactions/swap-tokens/index.mdx @@ -31,17 +31,17 @@ Swaps work like any other Smart Wallet transaction, so you can sponsor gas for g When requesting a swap quote, specify either an amount in, or a minimum amount out. ```tsx -import { parseUnits, parseEther } from "viem"; +import { parseUnits } from "viem"; // Mode 1: Swap exact input amount { fromAmount: parseUnits("0.01", 6); -} // Swap exactly 0.01 USDC +} // Swap exactly 0.01 USDC (6 decimals) // Mode 2: Get minimum output amount { - minimumToAmount: parseEther("0.0001"); -} // Get at least 0.0001 ETH. We calculate how much USDC you need to spend to get at least your desired ETH amount. + minimumToAmount: parseUnits("1", 6); +} // Get at least 1 USDC. Decimals must match toToken (e.g. 6 for USDC, 18 for ETH). We calculate how much you need to spend to receive at least your desired amount. ``` ## Prerequisites