---
title: Unwind a Trade
description:
  Take a leg back with Reclaim, unwind a trade with Cancel or Reject, and
  recover a deposit that landed after the trade closed with Recover.
seoTitle: Unwind a DvP Trade on Solana
url: /docs/defi/dvp-program/unwind-a-trade
type: tutorial
prerequisites:
  - /docs/defi/dvp-program/fund-the-legs
related:
  - /docs/defi/dvp-program/settle-the-trade
  - /docs/defi/dvp-program
---

Four instructions return funds without settling: Reclaim, Cancel, and Reject
while the trade is open, and Recover for a deposit that arrives after it has
closed. None of them checks the expiry. A frozen escrow or a changed mint can
still hold a leg, as the notes at the end describe.

| Instruction  | Signer               | Scope                         | Trade afterwards |
| ------------ | -------------------- | ----------------------------- | ---------------- |
| `ReclaimDvp` | `user_a` or `user_b` | The signer's own leg          | Still open       |
| `CancelDvp`  | Settlement authority | Both legs                     | Closed           |
| `RejectDvp`  | `user_a` or `user_b` | Both legs                     | Closed           |
| `RecoverDvp` | `user_a` or `user_b` | The signer's leg, after close | Already closed   |

In each case the escrow is drained of whatever it holds to the leg's party:
escrow A to `user_a`'s token account for `mint_a`, escrow B to `user_b`'s for
`mint_b`, whoever funded it. The memo program account is supplied for refund
accounts that require a memo.

This page continues from the earlier steps: the client and signers from
[create a trade](/docs/defi/dvp-program/create-a-trade), `userAAtaA` and
`userBAtaB` from [fund the legs](/docs/defi/dvp-program/fund-the-legs), and
`MEMO_PROGRAM_ID` and `authority` from
[settle the trade](/docs/defi/dvp-program/settle-the-trade) carry over.

## Reclaim a leg

Reclaim returns the signer's leg and leaves the trade open, so the leg can be
funded again. Use it when the issue is on the counterparty's leg; Cancel and
Reject close the whole trade.

<Tabs items={["TypeScript", "Rust"]}>
  <Tab value="TypeScript">
    ```ts
    import { MEMO_PROGRAM_ADDRESS } from "@solana-program/memo";
    import { getReclaimDvpInstruction } from "./dvp";

    // The asset-leg party takes its asset back.
    const reclaimInstruction = getReclaimDvpInstruction({
      signer: userASigner,
      swapDvp,
      mint: mintA,
      dvpSourceAta: escrowA,
      signerDestAta: userAAtaA,
      tokenProgram: tokenProgramA,
      memoProgram: MEMO_PROGRAM_ADDRESS,
    });

    await client.sendTransaction([reclaimInstruction]);
    ```

  </Tab>

  <Tab value="Rust">
    ```rust
    use dvp_swap_program_client::instructions::ReclaimDvpBuilder;

    // The asset-leg party takes its asset back.
    let reclaim_ix = ReclaimDvpBuilder::new()
        .signer(user_a)
        .swap_dvp(swap_dvp)
        .mint(mint_a)
        .dvp_source_ata(escrow_a)
        .signer_dest_ata(user_a_ata_a)
        .token_program(token_program_a)
        .memo_program(MEMO_PROGRAM_ID)
        .instruction();
    ```

  </Tab>
</Tabs>

## Cancel or reject

Cancel is signed by the settlement authority and Reject by either party. Both
refund whatever each escrow holds to the leg's party and close the record and
both escrows, with the closed-account rent going to the signer. The two
instructions take the same accounts; only the signer differs.

<Tabs items={["TypeScript", "Rust"]}>
  <Tab value="TypeScript">
    ```ts
    import { getCancelDvpInstruction, getRejectDvpInstruction } from "./dvp";

    const unwindAccounts = {
      swapDvp,
      mintA,
      mintB,
      dvpAtaA: escrowA,
      dvpAtaB: escrowB,
      userAAtaA,
      userBAtaB,
      tokenProgramA,
      tokenProgramB,
      memoProgram: MEMO_PROGRAM_ADDRESS,
      legAExtrasCount: 0,
    };

    // Signed by the settlement authority:
    const cancelInstruction = getCancelDvpInstruction({
      settlementAuthority: authoritySigner,
      ...unwindAccounts,
    });

    // Or signed by either party:
    const rejectInstruction = getRejectDvpInstruction({
      signer: userBSigner,
      ...unwindAccounts,
    });
    ```

  </Tab>

  <Tab value="Rust">
    ```rust
    use dvp_swap_program_client::instructions::{CancelDvpBuilder, RejectDvpBuilder};

    // Signed by the settlement authority:
    let cancel_ix = CancelDvpBuilder::new()
        .settlement_authority(authority)
        .swap_dvp(swap_dvp)
        .mint_a(mint_a)
        .mint_b(mint_b)
        .dvp_ata_a(escrow_a)
        .dvp_ata_b(escrow_b)
        .user_a_ata_a(user_a_ata_a)
        .user_b_ata_b(user_b_ata_b)
        .token_program_a(token_program_a)
        .token_program_b(token_program_b)
        .memo_program(MEMO_PROGRAM_ID)
        .leg_a_extras_count(0)
        .instruction();

    // Or signed by either party:
    let reject_ix = RejectDvpBuilder::new()
        .signer(user_b)
        .swap_dvp(swap_dvp)
        .mint_a(mint_a)
        .mint_b(mint_b)
        .dvp_ata_a(escrow_a)
        .dvp_ata_b(escrow_b)
        .user_a_ata_a(user_a_ata_a)
        .user_b_ata_b(user_b_ata_b)
        .token_program_a(token_program_a)
        .token_program_b(token_program_b)
        .memo_program(MEMO_PROGRAM_ID)
        .leg_a_extras_count(0)
        .instruction();
    ```

  </Tab>
</Tabs>

The refund accounts (`userAAtaA`, `userBAtaB`) must exist for any leg that holds
a balance; an uninitialized refund account makes the token transfer fail and
reverts the instruction. A leg that was never funded needs no transfer and
tolerates a missing account.

## Recover a late deposit

Settle, Cancel, and Reject all close both escrow token accounts, so a plain
transfer into a closed escrow fails. A deposit only lands after close if
something recreates the escrow first, for example a funding transaction that
includes a create-idempotent associated token account instruction ahead of the
transfer and was in flight when the counterparty sent Reject. The tokens then
sit in an escrow whose trade no longer exists: the record is gone, so Reclaim
cannot find it, and the nonce tombstone prevents a new trade from being created
at the same address and capturing the deposit.

To use `RecoverDvp`, the leg's party supplies the original seed inputs
(settlement authority, both parties, both mints, nonce). The program re-derives
the record address, requires its tombstone as proof the trade existed, drains
the escrow to the signer's own token account, and closes it, returning the
escrow rent to the signer. The signer's leg is chosen by which party signs:
`mint_a` for `user_a`, `mint_b` for `user_b`.

<Tabs items={["TypeScript", "Rust"]}>
  <Tab value="TypeScript">
    ```ts
    import { getAddressEncoder, getProgramDerivedAddress } from "@solana/kit";
    import { DVP_SWAP_PROGRAM_PROGRAM_ADDRESS, getRecoverDvpInstruction } from "./dvp";

    const [nonceTombstone] = await getProgramDerivedAddress({
      programAddress: DVP_SWAP_PROGRAM_PROGRAM_ADDRESS,
      seeds: ["nonce", getAddressEncoder().encode(swapDvp)],
    });

    const recoverInstruction = getRecoverDvpInstruction({
      signer: userASigner,
      swapDvp,
      nonceTombstone,
      mint: mintA,
      dvpEscrowAta: escrowA,
      signerDestAta: userAAtaA,
      tokenProgram: tokenProgramA,
      memoProgram: MEMO_PROGRAM_ADDRESS,
      settlementAuthority,
      userA,
      userB,
      mintA,
      mintB,
      nonce,
    });

    await client.sendTransaction([recoverInstruction]);
    ```

  </Tab>

  <Tab value="Rust">
    ```rust
    use dvp_swap_program_client::instructions::RecoverDvpBuilder;

    let recover_ix = RecoverDvpBuilder::new()
        .signer(user_a)
        .swap_dvp(swap_dvp)
        .nonce_tombstone(nonce_tombstone)
        .mint(mint_a)
        .dvp_escrow_ata(escrow_a)
        .signer_dest_ata(user_a_ata_a)
        .token_program(token_program_a)
        .memo_program(MEMO_PROGRAM_ID)
        .settlement_authority(settlement_authority)
        .user_a(user_a)
        .user_b(user_b)
        .mint_a(mint_a)
        .mint_b(mint_b)
        .nonce(nonce)
        .instruction();
    ```

  </Tab>
</Tabs>

Recovery depends on the party keeping the create parameters. A system that funds
trades stores the seed inputs with each order until the trade has closed and the
escrow balances have been confirmed empty.

## Notes

- **Frozen legs.** On a gated mint, a frozen escrow blocks the token transfer
  these instructions perform until the freeze authority thaws it. See the
  [overview](/docs/defi/dvp-program#mint-configuration-constraints).
- **Mint changes after creation.** The unwind paths skip the extension and
  mint-authority checks so the program's own checks do not block a recovery. The
  token program's own rules still apply. A mint can only be closed at zero
  supply, so it cannot change while a balance sits in escrow, but a mint closed
  and recreated as NonTransferable or with a transfer fee before a late deposit
  lands can make that deposit unmovable or fee-bearing. That is the
  mint-authority dependency listed under
  [supported tokens](/docs/defi/dvp-program#supported-tokens).

## Next steps

- [DvP program](/docs/defi/dvp-program): the overview, the supported tokens, and
  the mint configuration constraints
- [Delivery vs Payment (DvP) using delegation](/docs/tokenization/dvp): the same
  exchange through delegated authority, without a program
