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

# Close a Position

> Close a basket position with client-signed transactions.

Closing follows the same client-signed **prepare → sign → submit → poll** flow as
[opening](/sdk/open-position), with two differences:

* **No `amount`** — close always sells the wallet's **full holding** of the basket's
  tokens. It is balance-driven: whatever of the basket the wallet holds gets closed,
  exactly what [`positions.getPosition`](/sdk/positions#position-by-product-sdk-positions)
  reports. There is no partial close.
* The prepare call is `close.prepare` / the one-call helper is `close.execute`.

<Warning>
  Like opening, closing requires a **write-scoped** API key and the user's wallet pays its
  own gas — it must hold some SOL.
</Warning>

## One-call flow (you hold the keypair)

```ts theme={null}
const result = await cesto.close.execute({
  user: wallet,
  slug: 'defi-blue-chip',
  signTransactions, // same callback as open — signs the prepared transactions locally
});
result.status;       // 'COMPLETED' | 'PARTIALLY_COMPLETED' | 'FAILED'
result.transactions; // per-leg { nodeId, ok, signature?, error? }
```

See [Open a Position](/sdk/open-position#one-call-flow-you-hold-the-keypair) for a full
`signTransactions` implementation with `@solana/web3.js`.

## Split flow (browser wallet signs)

<Steps>
  <Step title="Backend: prepare">
    ```ts theme={null}
    const prepared = await cesto.close.prepare({
      user: userWalletAddress,
      slug: 'defi-blue-chip',
    });
    // { executionId, transactions: [{ nodeId, transaction }], expiresAt }
    ```

    Send `prepared.transactions` to the browser immediately — the set expires **\~60 seconds**
    after prepare.
  </Step>

  <Step title="Browser: the user signs">
    Sign the transactions **as-is** — any modified byte is rejected at submit. Same signing
    code as the [open flow](/sdk/open-position#split-flow-browser-wallet-signs).
  </Step>

  <Step title="Backend: submit and wait">
    ```ts theme={null}
    await cesto.positions.submit({
      executionId: prepared.executionId,
      transactions: signed,
    });

    const result = await cesto.positions.waitForExecution(prepared.executionId);
    ```
  </Step>
</Steps>

## Method reference

### `close.prepare(params)`

<ParamField path="user" type="string" required>
  Solana address of the wallet closing the position.
</ParamField>

<ParamField path="slug" type="string" required>
  Basket to close, by slug (a product id also works).
</ParamField>

Returns `{ executionId, transactions, expiresAt }`.

`positions.submit`, `getExecution`, and `waitForExecution` are shared with the open flow —
see the [method reference](/sdk/open-position#method-reference) there.

## Results, errors, and security

Terminal statuses (`COMPLETED`, `PARTIALLY_COMPLETED`, `FAILED`), partial-completion
semantics, the common-error table, and the security model are identical to opening — see
[Open a Position](/sdk/open-position#execution-results). Two close-specific notes:

* A wallet with **no holdings** of the basket has nothing to sell — check
  `positions.getPosition(...).hasPosition` before preparing a close.
* A `PARTIALLY_COMPLETED` close means some legs sold and some didn't; the remaining
  holdings are still in the wallet and a fresh `close.prepare` will target exactly what's
  left.
