---
title: "SDK API"
description: "Complete API reference for @clanktrade/sdk."
---

> Documentation Index
> Fetch the complete documentation index at: https://developer.clank.trade/llms.txt
> Use this file to discover all available pages before exploring further.

# SDK API

`@clanktrade/sdk` is the complete trading client for Clank launches on
Robinhood Chain. It discovers the current venue, quotes buys and sells in native
ETH, and returns unsigned transaction instructions for the integrating wallet.

## Install

```sh
npm install @clanktrade/sdk viem
pnpm add @clanktrade/sdk viem
yarn add @clanktrade/sdk viem
bun add @clanktrade/sdk viem
```

```ts
import {
  createClankClient,
  type ClankTradeClient,
  type ClankTradeInstruction,
  type ClankTradeQuote,
  type ClankTradeSide,
  type ClankTradeVenue,
} from "@clanktrade/sdk";
```

The SDK supports Robinhood Chain (`4663`) and a local Robinhood fork (`31337`).
It depends on a Viem `PublicClient` for reads and simulations but never opens a
wallet or broadcasts a transaction.

## `createClankClient`

Discovers a launch from the canonical factory, reads its curve state, and
returns a client bound to its current trading venue.

```ts
const clank = await createClankClient({
  publicClient,
  launchToken,
});
```

### Parameters

| Name           | Type           | Description                                       |
| -------------- | -------------- | ------------------------------------------------- |
| `publicClient` | `PublicClient` | Viem client connected to a supported chain.       |
| `launchToken`  | `Address`      | Token address registered by the Clank V2 factory. |

### Venue resolution

| Curve state | Result                                          |
| ----------: | ----------------------------------------------- |
|         `0` | Returns a client with `venue: "bonding-curve"`. |
|  `1` or `2` | Throws while the launch is graduating.          |
|         `3` | Returns a client with `venue: "uniswap-v4"`.    |
|         `4` | Throws because trading is unavailable.          |
|       Other | Throws an unknown-state error.                  |

Create a new client after graduation or whenever the cached venue may be stale.

## Client properties

Every `ClankTradeClient` exposes three readonly properties:

| Property      | Type              | Description                                       |
| ------------- | ----------------- | ------------------------------------------------- |
| `venue`       | `ClankTradeVenue` | `"bonding-curve"` or `"uniswap-v4"`.              |
| `launchToken` | `Address`         | Launch token this client trades.                  |
| `pairToken`   | `Address`         | Curve or pool pair token; zero address means ETH. |

## `quoteBuy`

Quotes a buy paid with native ETH. For an ERC-20 pair token, the SDK includes
the best supported direct, WETH, or USDG route in the quote.

```ts
const quote = await clank.quoteBuy({
  ethIn: 100000000000000000n,
  trader,
  slippageBps: 100,
});
```

### Parameters

| Name          | Type      | Required | Description                                |
| ------------- | --------- | :------: | ------------------------------------------ |
| `ethIn`       | `bigint`  |   Yes    | Native ETH input in wei.                   |
| `trader`      | `Address` |   Yes    | Trader and launch-token recipient.         |
| `slippageBps` | `number`  |    No    | Slippage in basis points; defaults to 100. |

Returns `Promise<ClankTradeQuote>`. Quoting only reads or simulates contracts;
it does not require a wallet signature.

## `buildBuy`

Builds the transaction for a previously returned buy quote.

```ts
const instructions = await clank.buildBuy({
  quote,
  trader,
  deadline,
});
```

### Parameters

| Name       | Type              | Required | Description                                  |
| ---------- | ----------------- | :------: | -------------------------------------------- |
| `quote`    | `ClankTradeQuote` |   Yes    | Unmodified quote returned by this client.    |
| `trader`   | `Address`         |   Yes    | Must match the address stored in the quote.  |
| `deadline` | `bigint`          |    No    | Unix timestamp in seconds for routed trades. |

Returns `Promise<readonly ClankTradeInstruction[]>`. A buy currently produces
one trade instruction:

| Venue and pair                      | Transaction target |
| ----------------------------------- | ------------------ |
| Bonding curve paired with ETH       | Bonding curve      |
| Bonding curve paired with an ERC-20 | `ClankRouterEth`   |
| Graduated Uniswap V4 market         | Universal Router   |

When omitted, `deadline` defaults to 60 seconds after the latest block
timestamp. Direct bonding-curve buys do not enforce a router deadline.

## `quoteSell`

Quotes a launch-token sale whose final output is native ETH. When the market is
paired with an ERC-20, the SDK includes the pair-token-to-ETH route.

```ts
const quote = await clank.quoteSell({
  launchTokensIn: 1000000000000000000000n,
  trader,
  slippageBps: 100,
});
```

### Parameters

| Name             | Type      | Required | Description                                |
| ---------------- | --------- | :------: | ------------------------------------------ |
| `launchTokensIn` | `bigint`  |   Yes    | Launch-token input in its smallest unit.   |
| `trader`         | `Address` |   Yes    | Trader, token owner, and ETH recipient.    |
| `slippageBps`    | `number`  |    No    | Slippage in basis points; defaults to 100. |

Returns `Promise<ClankTradeQuote>`. The quote simulation uses `trader` as the
account where the venue requires account-aware simulation.

## `buildSell`

Builds any missing approvals followed by the sale transaction.

```ts
const instructions = await clank.buildSell({
  quote,
  trader,
  deadline,
});
```

### Parameters

| Name       | Type              | Required | Description                                  |
| ---------- | ----------------- | :------: | -------------------------------------------- |
| `quote`    | `ClankTradeQuote` |   Yes    | Unmodified quote returned by this client.    |
| `trader`   | `Address`         |   Yes    | Must match the address stored in the quote.  |
| `deadline` | `bigint`          |    No    | Unix timestamp in seconds for routed trades. |

Returns `Promise<readonly ClankTradeInstruction[]>` in submission order.

| Venue                | Possible instructions                                             |
| -------------------- | ----------------------------------------------------------------- |
| Direct bonding curve | Curve approval, then sell.                                        |
| Routed bonding curve | `ClankRouterEth` approval, then sell.                             |
| Graduated Uniswap V4 | Token-to-Permit2 approval, Permit2-to-router approval, then sell. |

Approvals with sufficient, unexpired allowances are omitted.

## `ClankTradeQuote`

Quotes are readonly snapshots. Do not edit or combine fields from separate
quotes.

```ts
type ClankTradeQuote = {
  readonly venue: "bonding-curve" | "uniswap-v4";
  readonly side: "buy" | "sell";
  readonly launchToken: Address;
  readonly pairToken: Address;
  readonly amountIn: bigint;
  readonly amountOut: bigint;
  readonly minimumAmountOut: bigint;
  readonly fee: bigint | null;
  readonly refund: bigint;
  readonly trader: Address;
  readonly route: {
readonly label: string;
readonly commands: Hex;
readonly inputs: readonly Hex[];
  };
};
```

| Field              | Buy meaning                                      | Sell meaning                                    |
| ------------------ | ------------------------------------------------ | ----------------------------------------------- |
| `amountIn`         | ETH offered in wei.                              | Launch tokens offered.                          |
| `amountOut`        | Launch tokens expected.                          | ETH expected in wei.                            |
| `minimumAmountOut` | Minimum launch tokens across the complete route. | Minimum ETH across the complete route.          |
| `fee`              | Curve fee in pair-token units, or `null` on V4.  | Curve fee in pair-token units, or `null` on V4. |
| `refund`           | Unspent pair token from a terminal curve buy.    | Always zero.                                    |

`route.label` is display metadata. `route.commands` and `route.inputs` are the
encoded Universal Router route consumed by the corresponding build method.

## `ClankTradeInstruction`

```ts
type ClankTradeInstruction = {
  readonly label: string;
  readonly to: Address;
  readonly data: Hex;
  readonly value?: bigint;
};
```

| Field   | Description                                                |
| ------- | ---------------------------------------------------------- |
| `label` | Human-readable approval or trade step.                     |
| `to`    | Transaction recipient.                                     |
| `data`  | Encoded transaction calldata.                              |
| `value` | Native ETH value; omitted when the instruction sends none. |

## Submit instructions

The application owns wallet access, gas policy, broadcasting, confirmations,
retries, and recovery. Submit instructions sequentially and stop after any
failed receipt.

```ts
for (const instruction of instructions) {
  const hash = await walletClient.sendTransaction({
account: trader,
to: instruction.to,
data: instruction.data,
value: instruction.value ?? 0n,
  });
  const receipt = await publicClient.waitForTransactionReceipt({ hash });
  if (receipt.status !== "success") {
throw new Error(`${instruction.label} reverted`);
  }
}
```

## Errors and validation

The SDK rejects:

- unsupported chain IDs and tokens absent from the canonical factory
- launches that are graduating, rescued, or in an unknown state
- non-positive inputs and V4 inputs outside the `uint128` range
- slippage below `0` or above `9,999` basis points
- missing ETH-to-pair-token routes
- quotes used with the wrong venue, side, launch token, or trader
- sell amounts outside Permit2's `uint160` range

Request a fresh quote after an error caused by changing price, liquidity, state,
account, or chain. See [Errors](/reference/errors) for contract-level failures.

Source: https://developer.clank.trade/reference/sdk/index.mdx
