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

> ## Agent Instructions
> This page is part of the Solana developer documentation at https://solana.com/developers/docs. Append `.md` to any docs URL, or send `Accept: text/markdown`, to get the page as Markdown.
> Index of all pages: https://solana.com/llms.txt. Per-section indexes such as https://solana.com/developers/docs/payments/llms.txt are smaller; prefer them when working on one task.
> Coding agents: install the official Solana skill for current APIs and conventions with `npx skills add https://github.com/solana-foundation/solana-dev-skill` (also served at https://solana.com/SKILL.md).
> Code in the cookbook and task pages comes from tested example files. Prefer it over writing API calls from memory.

# Create a Trade

> Set up the DvP program clients, derive the trade record and its two escrow accounts, and record the terms of a trade with CreateDvp.

`CreateDvp` records the terms of a trade and creates the two escrow accounts the
parties will fund. It moves no tokens. Anyone can send it, so the record may
come from a counterparty, the settlement authority's operator, or a third party,
and every other participant verifies the stored terms before acting on them (see
[fund the legs](/developers/docs/defi/dvp-program/fund-the-legs)).

The snippets use the generated client directly, with placeholder addresses, so
the accounts and signers involved are visible. They target devnet, where the
program is deployed at the same address as mainnet.

## Install

No package is published for the DvP clients as of 2 October 2026. Both are
generated from the program's IDL in the repository and used from source. The
repository has a pnpm lockfile, so its build steps use pnpm.

<Tabs>
  <Tab title="TypeScript">
    ```bash
    git clone https://github.com/solana-foundation/dvp.git
    cd dvp && git checkout df9919ed02c25a93620e7f6820107c9050ce3b92
    pnpm install && pnpm generate-clients && pnpm build
    # The client is clients/typescript/src; copy or link it into your project as ./dvp
    # The client uses ESM re-exports: set "type": "module" in your package.json
    # Then, in your project:
    npm install @solana/kit @solana/kit-plugin-rpc @solana/kit-plugin-signer @solana-program/token-2022 @solana-program/memo
    ```
  </Tab>

  <Tab title="Rust">
    ```bash
    git clone https://github.com/solana-foundation/dvp.git
    cd dvp && git checkout df9919ed02c25a93620e7f6820107c9050ce3b92
    pnpm install && pnpm generate-clients
    # The client is clients/rust; add it to your Cargo.toml as a path dependency
    ```

    ```toml
    [dependencies]
    dvp-swap-program-client = { path = "<clone>/clients/rust", features = ["fetch"] }
    solana-pubkey = { version = "2.2", features = ["curve25519"] }
    solana-instruction = "2.2"
    solana-client = "2.2"
    solana-commitment-config = "2.2"
    spl-token-2022-interface = "1"
    spl-associated-token-account-client = "2"
    rand = "0.8"
    ```

  </Tab>
</Tabs>

These pages are written against commit `df9919e`, with `@solana/kit` 8 in
TypeScript and the `solana-*` 2.x crates in Rust.

The snippets on every step page carry over: each page uses the client, signers,
and addresses defined on earlier pages. `payerSigner`, `userASigner`,
`userBSigner`, and `authoritySigner` are `TransactionSigner`s for the rent
payer, the two parties, and the settlement authority, for example from
`createKeyPairSignerFromBytes`. The Rust snippets build instructions and leave
signing and sending to the caller.

## Derive the addresses

The trade record is a program-derived address over the settlement authority, the
two parties, the two mints, and a nonce. Each escrow is the record's associated
token account for one of the mints. Both derivations need no network access.

Use a random 64-bit nonce for every trade. Because creation is permissionless, a
predictable nonce lets a third party occupy the address a trade was going to
use. Pass the nonce and the amounts as `bigint` in TypeScript; the generated
client rejects a `number`.

Replace each `..._HERE` placeholder with a real address before running.
`address()` validates base58 and throws on a placeholder, and `pubkey!` fails to
compile.

<Tabs>
  <Tab title="TypeScript">
    ```ts
    import { address, createClient } from "@solana/kit";
    import { solanaRpc } from "@solana/kit-plugin-rpc";
    import { signer } from "@solana/kit-plugin-signer";
    import { TOKEN_2022_PROGRAM_ADDRESS } from "@solana-program/token-2022";
    import { findSwapDvpEscrowAta, findSwapDvpPda } from "./dvp";

    const client = createClient()
      .use(signer(payerSigner))
      .use(
        solanaRpc({
          rpcUrl: "https://api.devnet.solana.com",
          rpcSubscriptionsUrl: "wss://api.devnet.solana.com",
        }),
      );

    const settlementAuthority = address("SETTLEMENT_AUTHORITY_ADDRESS_HERE");
    const userA = address("ASSET_LEG_PARTY_ADDRESS_HERE");
    const userB = address("CASH_LEG_PARTY_ADDRESS_HERE");
    const mintA = address("ASSET_MINT_ADDRESS_HERE");
    const mintB = address("CASH_MINT_ADDRESS_HERE");
    const tokenProgramA = TOKEN_2022_PROGRAM_ADDRESS;
    const tokenProgramB = TOKEN_2022_PROGRAM_ADDRESS;

    // A fresh random nonce per trade, as a bigint.
    const nonce = new DataView(
      crypto.getRandomValues(new Uint8Array(8)).buffer,
    ).getBigUint64(0);

    const [swapDvp] = await findSwapDvpPda({
      settlementAuthority,
      userA,
      userB,
      mintA,
      mintB,
      nonce,
    });
    const [escrowA] = await findSwapDvpEscrowAta({
      swapDvp,
      mint: mintA,
      tokenProgram: tokenProgramA,
    });
    const [escrowB] = await findSwapDvpEscrowAta({
      swapDvp,
      mint: mintB,
      tokenProgram: tokenProgramB,
    });
    ```

  </Tab>

  <Tab title="Rust">
    ```rust
    use dvp_swap_program_client::verify::{find_swap_dvp_address, find_swap_dvp_escrow_ata};
    use solana_pubkey::{pubkey, Pubkey};

    const TOKEN_2022_PROGRAM_ID: Pubkey = pubkey!("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb");

    let settlement_authority: Pubkey = pubkey!("SETTLEMENT_AUTHORITY_ADDRESS_HERE");
    let user_a: Pubkey = pubkey!("ASSET_LEG_PARTY_ADDRESS_HERE");
    let user_b: Pubkey = pubkey!("CASH_LEG_PARTY_ADDRESS_HERE");
    let mint_a: Pubkey = pubkey!("ASSET_MINT_ADDRESS_HERE");
    let mint_b: Pubkey = pubkey!("CASH_MINT_ADDRESS_HERE");
    let token_program_a = TOKEN_2022_PROGRAM_ID;
    let token_program_b = TOKEN_2022_PROGRAM_ID;

    // A fresh random nonce per trade.
    let nonce: u64 = rand::random();

    let (swap_dvp, _bump) = find_swap_dvp_address(
        &settlement_authority, &user_a, &user_b, &mint_a, &mint_b, nonce,
    );
    let escrow_a = find_swap_dvp_escrow_ata(&swap_dvp, &mint_a, &token_program_a);
    let escrow_b = find_swap_dvp_escrow_ata(&swap_dvp, &mint_b, &token_program_b);
    ```

  </Tab>
</Tabs>

## Record the terms

`CreateDvp` takes the two amounts in base units, an expiry, the nonce, and four
optional fields: an opaque reference of up to 64 bytes, a settlement destination
for each side, and an earliest settlement time (see Notes). The destination is
where that party's proceeds land at settlement, for example a custodian's
deposit wallet; it defaults to the party's own address and is fixed once the
record exists. Refunds and reclaims always go to the party's own token account
for that leg's mint, never to a destination, whoever funded the escrow.

The payer funds the rent for the record, the nonce tombstone, and both escrows.
On close, that rent goes to whoever signs the closing instruction, not back to
the payer. The tombstone is never closed, so each trade leaves a small permanent
balance. When budgeting the payer's SOL, derive the rent amounts with the
`getMinimumBalanceForRentExemption` RPC method rather than hardcoding them.

<Tabs>
  <Tab title="TypeScript">
    ```ts
    import { getCreateDvpInstruction } from "./dvp";

    const nowSeconds = BigInt(Math.floor(Date.now() / 1000));
    // The expiry agreed with the counterparty, in unix seconds: one day from now.
    // Fund the legs checks the stored record against it.
    const agreedExpiry = nowSeconds + 60n * 60n * 24n;

    const createInstruction = getCreateDvpInstruction({
      payer: payerSigner,
      swapDvp,
      nonceTombstone: (
        await getProgramDerivedAddress({
          programAddress: DVP_SWAP_PROGRAM_PROGRAM_ADDRESS,
          seeds: ["nonce", getAddressEncoder().encode(swapDvp)],
        })
      )[0],
      settlementAuthority,
      userA,
      userB,
      mintA,
      mintB,
      dvpAtaA: escrowA,
      dvpAtaB: escrowB,
      tokenProgramA,
      tokenProgramB,
      amountA: 1_000_000_000n, // 1,000 units of a 6-decimal asset
      amountB: 998_500_000n, // 998.50 units of a 6-decimal cash token
      expiryTimestamp: agreedExpiry,
      nonce,
      refString: "ORDER-2026-10-02-0001",
      userASettlementDestination: null, // defaults to userA
      userBSettlementDestination: null, // defaults to userB
      earliestSettlementTimestamp: null,
    });

    const { context } = await client.sendTransaction([createInstruction]);
    console.log("Trade record:", swapDvp, "signature:", context.signature);
    ```

  </Tab>

  <Tab title="Rust">
    ```rust
    use dvp_swap_program_client::instructions::CreateDvpBuilder;
    use dvp_swap_program_client::DVP_SWAP_PROGRAM_ID;

    let payer: Pubkey = pubkey!("PAYER_ADDRESS_HERE");
    let (nonce_tombstone, _) =
        Pubkey::find_program_address(&[b"nonce", swap_dvp.as_ref()], &DVP_SWAP_PROGRAM_ID);
    let now_seconds: i64 = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)?
        .as_secs() as i64;
    // The expiry agreed with the counterparty, in unix seconds: one day from now.
    // Fund the legs checks the stored record against it.
    let agreed_expiry: i64 = now_seconds + 60 * 60 * 24;

    let create_ix = CreateDvpBuilder::new()
        .payer(payer)
        .swap_dvp(swap_dvp)
        .nonce_tombstone(nonce_tombstone)
        .settlement_authority(settlement_authority)
        .user_a(user_a)
        .user_b(user_b)
        .mint_a(mint_a)
        .mint_b(mint_b)
        .dvp_ata_a(escrow_a)
        .dvp_ata_b(escrow_b)
        .token_program_a(token_program_a)
        .token_program_b(token_program_b)
        .amount_a(1_000_000_000) // 1,000 units of a 6-decimal asset
        .amount_b(998_500_000) // 998.50 units of a 6-decimal cash token
        .expiry_timestamp(agreed_expiry)
        .nonce(nonce)
        .ref_string("ORDER-2026-10-02-0001".to_string())
        .instruction();
    ```

  </Tab>
</Tabs>

The TypeScript snippet needs `getProgramDerivedAddress` and `getAddressEncoder`
from `@solana/kit`, and `DVP_SWAP_PROGRAM_PROGRAM_ADDRESS` from the client, in
scope. The Rust builder leaves the system program and associated token program
at their defaults.

## Notes

- **Expiry.** Only Settle checks the expiry. Reclaim, Cancel, and Reject work
  after it, and a deposit that lands after expiry is recoverable by the leg's
  party. The expiry can be at most one year after creation.
- **Time is network time.** The program reads the onchain clock, which can
  differ from wall-clock time, at times by tens of seconds, and can jump
  forward. Leave a margin between the expiry and the intended settlement time,
  avoid funding or settling near either boundary, and do not rely on the expiry
  to enforce a deadline to the second. The program README suggests budgeting
  about a minute of drift.
- **Earliest settlement.** Optional. If set, Settle is rejected before it, and
  it must not be later than the expiry.
- **Create-time checks.** Create fails with `PartyNotSignerCapable` if a party
  is not a system-owned account (this rules out an SPL Token multisig),
  `SettlementDestinationIsSwapDvp`, `SelfDvp`, `SameMint`, `ZeroAmount`,
  `SettlementAuthorityIsParty`, or `ExpiryNotInFuture`. The full list is in the
  [errors table](/developers/docs/defi/dvp-program#errors).

## Next steps

- [Fund the legs](/developers/docs/defi/dvp-program/fund-the-legs): each party transfers
  its leg into the trade's escrow
