> ## Documentation Index
> Fetch the complete documentation index at: https://docs.kuru.io/llms.txt
> Use this file to discover all available pages before exploring further.

# SDK Swaps

> Estimate and execute exact-input Spot swaps with explicit slippage protection.

```typescript theme={null}
import { abi } from "@toxicflow-labs/ts-sdk";
import { formatUnits, parseUnits } from "viem";

const quoteDecimals = await publicClient.readContract({
  address: quoteToken,
  abi: abi.erc20MetadataAbi,
  functionName: "decimals"
});
const amountIn = parseUnits("10", quoteDecimals);
const quote = await publicClient.readContract({
  address: market,
  abi: abi.orderBookAbi,
  functionName: "estimateSwap",
  args: [Number(userId), true, amountIn],
});
const minAmountOut = quote.amountOut * 995n / 1000n;
if (minAmountOut === 0n) throw new Error("No usable nonzero output quote");

await kuru.spot.swap({
  market,
  userId,
  isBuy: true,
  amountIn,
  minAmountOut,
  deadline: BigInt(Math.floor(Date.now() / 1000) + 60)
});
```

For buys, parse `amountIn` with the quote token's decimals. For sells, use the base token's decimals. The estimate returns `amountInUsed` and `amountOut` in token atoms. The SDK swap write returns a transaction hash, not that result struct. Await a successful receipt and read `SpotSwap` or the resulting balances for actual amounts. Format token amounts with `formatUnits` and the corresponding token decimals. The typed ABI read above avoids the SDK 0.2.0 estimate helper’s `unknown` return type. `userId` is a uint40, so conversion to a JavaScript number is exact once its range has been validated.

Estimation uses current chain state but cannot predict stateful post-fill-hook output or later state changes. Always use a deadline and nonzero slippage bound appropriate to the application.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.