Skip to main content
On Solana, accounts must hold a rent-exempt balance (lamports) to exist. When you close an empty token account, those lamports are returned to the destination you specify. If you set outcomeAccountRentRecipient when requesting an order, retrieve that value from your stored order params and reuse it as the close destination. Otherwise, choose any destination (typically the user’s wallet).Closing a token account is a standard SPL Token action. DFlow does not provide a dedicated endpoint for this.
After losing a position, the outcome token account is worthless. Close the empty account to reclaim the rent lamports.
1

Choose the rent destination

Decide where the reclaimed rent should go. If your app set outcomeAccountRentRecipient when creating the order, use that address.
const userPublicKey = keypair.publicKey.toBase58();

// Optional: reuse the address you set when creating the order.
// If you did not set one, default to the user's wallet.
const outcomeAccountRentRecipient =
  storedOrderParams?.outcomeAccountRentRecipient ?? null;

const rentDestination = outcomeAccountRentRecipient ?? userPublicKey;
2

Burn remaining tokens and close the account

A token account must be empty to close. If it still has a balance, burn the remaining outcome tokens and then close the account to reclaim rent.
import {
  Connection,
  PublicKey,
  Transaction,
  sendAndConfirmTransaction,
} from "@solana/web3.js";
import {
  getAccount,
  createBurnInstruction,
  createCloseAccountInstruction,
} from "@solana/spl-token";

const connection = new Connection(SOLANA_RPC_URL, "confirmed");
const owner = keypair; // signer and token account owner

const tokenAccount = new PublicKey("OUTCOME_TOKEN_ACCOUNT");
const outcomeMint = new PublicKey("OUTCOME_TOKEN_MINT");
const rentRecipient = new PublicKey(rentDestination);

const account = await getAccount(connection, tokenAccount);

const instructions = [];
if (account.amount > 0n) {
  instructions.push(
    createBurnInstruction(
      tokenAccount,
      outcomeMint,
      owner.publicKey,
      account.amount
    )
  );
}

instructions.push(
  createCloseAccountInstruction(
    tokenAccount,
    rentRecipient,
    owner.publicKey
  )
);

const tx = new Transaction().add(...instructions);
await sendAndConfirmTransaction(connection, tx, [owner]);

API Routes

This recipe does not call DFlow APIs. It uses a Solana RPC endpoint and the SPL Token program.

Cookbook Repository

This recipe, along with many more, is available in the DFlow Cookbook Repo. You can clone it and start coding immediately.