> For the complete documentation index, see [llms.txt](https://docs.surged.fun/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.surged.fun/integration/sdk/trading.md).

# Buying and selling

The encoders return `{ to, data, value }`. Send it however you like; nothing in this package talks to the network.

## Buying

```ts
import { encodeBuy, minOutWithSlippage, previewBuy, curveStateFromApi } from '@surged/sdk'

const state = curveStateFromApi(launch)
const quote = previewBuy(state, quoteIn, { currentBlock: BigInt(head) + 1n })

const call = encodeBuy({
  pairToken: launch.pairToken,
  quoteIn,
  minTokensOut: minOutWithSlippage(quote.tokensOut, 500),
  recipient: account.address,
})

await wallet.sendTransaction({ to: launch.curve, ...call })
```

`recipient` is who receives the tokens and, just as important, **who the snipe tax is measured against**. Buying to a different address does not dodge it.

With a native quote, which is every launch today, the USDC travels as the transaction's `value`; `encodeBuy` sets it for you.

## Selling

The curve pulls the tokens from you, so it needs an allowance first. Two transactions:

```ts
import { encodeSell, launcherTokenAbi, minOutWithSlippage, previewSell } from '@surged/sdk'
import { encodeFunctionData } from 'viem'

const quote = previewSell(state, tokensIn)

await wallet.sendTransaction({
  to: launch.token,
  data: encodeFunctionData({
    abi: launcherTokenAbi,
    functionName: 'approve',
    args: [launch.curve, tokensIn],
  }),
})

await wallet.sendTransaction({
  to: launch.curve,
  ...encodeSell({
    tokensIn,
    minQuoteOut: minOutWithSlippage(quote.quoteOut, 500),
    recipient: account.address,
  }),
})
```

Wait for the approval's receipt before sending the sell. Arc has no public mempool and blocks are 500 ms, so this is fast, but a sell that arrives before its approval is mined simply reverts.

## Gas

Gas is paid in USDC, the same asset a buy spends.

* **Never spend the whole balance on a buy.** Hold back enough for the transaction.
* **Do not hardcode the reserve.** Arc's base fee floor is 20 gwei but the fee is not fixed; it reached 196 gwei on the day mainnet opened. Read the base fee of the latest block and leave headroom over it.

## Confirming

```ts
const receipt = await publicClient.waitForTransactionReceipt({ hash })
if (receipt.status !== 'success') {
  // Mined and rejected by the contract. See Handling errors.
}
```

A mined transaction is not a successful one. Check `status`; a revert still produces a receipt.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.surged.fun/integration/sdk/trading.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
