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

# Presale SDK

> Add precommit and deposit buttons to your website.

Use this SDK to add **Precommit** and **Deposit** buttons to your website. Connect the user's Solana wallet, then use the examples below.

Only need to show totals and dates? Use the [display guide](https://docs.star.fun/developers/fundraise-api.md); no SDK required.

## Install

```bash theme={null}
pnpm add @starfun/sdk@0.1.0 @solana/web3.js@^1 @solana/spl-token@^0.4 bn.js
pnpm add -D @types/bn.js
```

Using `@starfun/sdk-vault`? Every function keeps its name; import from `@starfun/sdk/raise` instead.

## Set up

The examples use a connected `wallet` with `publicKey` and `signTransaction`.

```ts theme={null}
import BN from 'bn.js';
import { Connection, PublicKey, Transaction } from '@solana/web3.js';
import { getMint } from '@solana/spl-token';
import {
  StarVault,
  buildLockCommitmentTx,
  buildWithdrawCommitmentTx,
  fetchCommitmentRecord,
  getVaultStatusKey,
} from '@starfun/sdk/raise';

const connection = new Connection(RPC_URL, 'confirmed');
const vaultAddress = new PublicKey(VAULT_ADDRESS);
const programId = new PublicKey(VAULT_PROGRAM_ID);
const vault = await StarVault.create(connection, vaultAddress, programId);
const quoteMint = new PublicKey(vault.account.quoteMint);
const quoteTokenProgramId = new PublicKey(vault.account.quoteTokenProgram);
const mint = await getMint(connection, quoteMint, 'confirmed', quoteTokenProgramId);

async function send(tx: Transaction) {
  const latest = await connection.getLatestBlockhash('confirmed');
  tx.feePayer = wallet.publicKey;
  tx.recentBlockhash = latest.blockhash;
  const signed = await wallet.signTransaction(tx);
  const signature = await connection.sendRawTransaction(signed.serialize());
  console.log('Submitted:', signature); // Save this receipt before waiting.
  const result = await connection.confirmTransaction({ signature, ...latest }, 'confirmed');
  if (result.value.err) throw new Error(`Transaction failed: ${signature}`);
  return signature;
}
```

Amounts use quote-token base units: for a six-decimal mint, 25 tokens is `new BN('25000000')`. Read `mint.decimals`; keep amounts as decimal strings or `BN`, never floating-point numbers. Users also need SOL for transaction fees and account rent.

## Show the right action

Read the raise status with `getVaultStatusKey(vault.account.status)` after every refresh, then show the matching action:

| Stage | Status | What to show |
| - | - | - |
| Not open yet | `pending` | Countdown to the start, no actions |
| Pre-commit | `precommitActive` | **Precommit** button, limited to the remaining precommit cap |
| Pre-sale | `active`, between the start and end time | **Deposit** button |
| Settling | `awaitingTgeRequest` or `tgeRequested` | "Raise closed, launch pending", no actions |
| Claim | `tgeAccepted` | **Claim tokens** once launched, plus **Claim excess** if any |
| Refunds | `cancelled`, or the raise ended below its minimum | **Withdraw commitment** and/or **Claim refund** |

* Precommits are binding. Users can only withdraw them if the raise is cancelled.
* A countdown reaching zero doesn't mean the status changed. Refresh and let the program decide.
* The table says which button to offer. The program still checks each user's eligibility, so handle rejections.

## Precommit

Refresh the vault before showing an action. Precommits lock funds until conversion or cancellation; users cannot withdraw at will.

```ts theme={null}
await vault.fetch();
if (getVaultStatusKey(vault.account.status) !== 'precommitActive') {
  throw new Error('Precommit is not open');
}
const signature = await send(
  await buildLockCommitmentTx(
    connection,
    {
      user: wallet.publicKey,
      vault: vaultAddress,
      quoteMint,
      quoteTokenProgramId,
      amount: new BN('25000000'), // 25 tokens ONLY for a six-decimal mint.
      ensureAta: true,
      wrapWsol: true,
    },
    programId,
  ),
);
```

Conversion into a deposit is a separate transaction after the live raise starts. Star or your integration operator submits it using `buildConvertCommitmentTx`.

## Deposit during the live raise

```ts theme={null}
await vault.fetch();
if (getVaultStatusKey(vault.account.status) !== 'active') {
  throw new Error('Presale is not active');
}
const signature = await send(
  await vault.deposit(
    new BN('25000000'), // 25 tokens ONLY for a six-decimal mint.
    wallet.publicKey,
    { quoteTokenProgramId, ensureAtas: true, wrapWsol: true },
  ),
);
```

Also check the [start and end times](https://docs.star.fun/developers/fundraise-api.md) when displaying the deposit button. The program enforces the live window. For SOL raises, automatic wrapping requires the atom amount to fit within `Number.MAX_SAFE_INTEGER`.

Read positions with `fetchCommitmentRecord(connection, vaultAddress, wallet.publicKey, programId)` and `StarVault.fetchDepositorRecord(...)` using the same arguments. Their `null` result can mean a missing account or a read error.

## Withdrawals and refunds

An unconverted commitment is withdrawable **only after cancellation**:

```ts theme={null}
await vault.fetch();
const commitment = await fetchCommitmentRecord(
  connection,
  vaultAddress,
  wallet.publicKey,
  programId,
);
if (getVaultStatusKey(vault.account.status) !== 'cancelled' || !commitment) {
  throw new Error('Cancelled commitment required');
}
const remaining = commitment.lockedAmount
  .sub(commitment.withdrawnAmount)
  .sub(commitment.convertedAmount);
if (commitment.converted || remaining.lte(new BN(0))) throw new Error('Nothing to withdraw');
await send(
  await buildWithdrawCommitmentTx(
    connection,
    {
      user: wallet.publicKey,
      vault: vaultAddress,
      quoteMint,
      quoteTokenProgramId,
      amount: remaining,
      ensureAta: true,
    },
    programId,
  ),
);
```

For eligible cancelled or failed deposits, use `claimRefund`. After accepted launch allocation, use `claimExcessRefund` for unclaimed excess:

```ts theme={null}
const refundDeposit = async () =>
  send(await vault.claimRefund(wallet.publicKey, { tokenProgramId: quoteTokenProgramId }));
const refundExcess = async () =>
  send(await vault.claimExcessRefund(wallet.publicKey, { tokenProgramId: quoteTokenProgramId }));
```

These return quote tokens. Project-token claims after launch use `buildClaimTransaction` from the same `@starfun/sdk/raise` import, or the project's claim link on Star.

## Handle errors

Disable repeat submissions while signing/sending. Refresh the position and [fundraise totals](https://docs.star.fun/developers/fundraise-api.md) after confirmation.

| What happened | What to do |
| - | - |
| User rejected signing | Return to idle; nothing was sent |
| Not enough quote tokens or SOL | Say which balance is short (SOL covers fees and account rent) |
| Stage changed or precommit cap full | Refresh and update the available actions |
| Program or simulation error | Show a useful message; don't skip preflight to force it through |
| Confirmation timed out | Show "pending", keep the saved signature, and check its status before retrying |
| Blockhash expired before sending | Rebuild the transaction and ask for a new signature |
| RPC or indexer unavailable | Keep the last known data, mark it as stale, and retry with backoff |

> [!WARNING]
>
> Never build a new deposit until you've checked the last saved signature. A send can fail with an
> error and still land, so a blind retry can charge the user twice.

Test the package in your production browser bundle; its CommonJS dependencies may require bundler configuration.


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