> For the complete documentation index, see [llms.txt](https://velora-1.gitbook.io/docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://velora-1.gitbook.io/docs/instruction-reference/withdraw.md). Agents can also search and read this documentation through its [MCP server](https://velora-1.gitbook.io/docs/~gitbook/mcp).

# withdraw

Transfers all currently-unlocked, not-yet-claimed tokens from the vault to the recipient. Called by the recipient whenever they want to claim. Idempotent in the sense that calling it twice in a row with nothing newly unlocked just fails with `NothingToWithdraw`.

```rust
pub fn withdraw(ctx: Context<Withdraw>) -> Result<()>
```

## Accounts

| Account                   | Signer | Mut | Notes                                                   |
| ------------------------- | ------ | :-: | ------------------------------------------------------- |
| `recipient`               | ✅      |     | Must equal `stream_config.recipient`.                   |
| `stream_config`           |        |  ✅  | PDA; `amount_claimed` and `status` are updated.         |
| `vault`                   |        |  ✅  | PDA; must match `stream_config.vault` and mint.         |
| `recipient_token_account` |        |  ✅  | Must be owned by `recipient` and match the stream mint. |
| `token_program`           |        |     | SPL Token program.                                      |

No parameters — the amount is computed on-chain.

## Behavior

1. Require `recipient == stream_config.recipient`.
2. Require `status == Active`.
3. `unlocked = compute_unlocked(stream_config, now)` per the schedule.
4. `claimable = unlocked - amount_claimed`; require `claimable > 0`.
5. Transfer `claimable` from the vault (program-signed) to the recipient.
6. `amount_claimed += claimable`; if it now equals `total_amount`, set `status = Completed`.

## Errors

| Code                | Cause                                                               |
| ------------------- | ------------------------------------------------------------------- |
| `Unauthorized`      | Signer is not the stream recipient, or token account owner mismatch |
| `StreamNotActive`   | `status` is `Cancelled` or `Completed`                              |
| `NothingToWithdraw` | Nothing new has unlocked since the last claim                       |
| `InvalidVault`      | `vault` does not match `stream_config.vault`                        |
| `InvalidMint`       | `recipient_token_account` / vault mint mismatch                     |
| `MathOverflow`      | Arithmetic overflow in the unlock calculation                       |

## Example (TypeScript / Anchor)

```ts
await program.methods
  .withdraw()
  .accounts({
    recipient: recipient.publicKey,
    streamConfig,
    vault,
    recipientTokenAccount,
    tokenProgram: TOKEN_PROGRAM_ID,
  })
  .signers([recipient])
  .rpc();

// Inspect progress
const account = await program.account.streamConfig.fetch(streamConfig);
console.log("claimed:", account.amountClaimed.toString());
console.log("status:", account.status); // { active: {} } | { completed: {} } | ...
```


---

# 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 by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://velora-1.gitbook.io/docs/instruction-reference/withdraw.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

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.
