> ## 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.

# Settle a trade atomically (DvP)

> Price a trade from a live Pyth oracle, then settle it with the DvP program: the asset and the USDC payment move in one transaction, or neither moves.

<div className="sol-stepper not-prose" data-path="settle-trade">
<div className="sol-stepper-nav" role="navigation" aria-label="Settle a trade · ~40 min">
<ol>
<li data-state="todo"><a href="/developers/docs/defi/settle-trade/try-it"><span className="sol-stepper-n">Step 1<span className="sol-vh">: </span></span><span className="sol-stepper-t">Watch a trade settle</span></a></li>
<li data-state="todo"><a href="/developers/docs/defi/settle-trade/browser"><span className="sol-stepper-n">Step 2<span className="sol-vh">: </span></span><span className="sol-stepper-t">Read the code</span></a></li>
<li data-state="todo"><a href="/developers/docs/defi/settle-trade/local"><span className="sol-stepper-n">Step 3<span className="sol-vh">: </span></span><span className="sol-stepper-t">Run it from your machine</span></a></li>
<li data-state="todo"><a href="/developers/docs/defi/settle-trade/go-live"><span className="sol-stepper-n">Step 4<span className="sol-vh">: </span></span><span className="sol-stepper-t">Go live</span></a></li>
</ol>
</div>
</div>

Settle a trade so that neither side can end up with nothing: the seller's asset
and the buyer's USDC move in the same transaction, or neither moves. This is
delivery versus payment (DvP). You price the trade from a live market price,
then settle it with the Solana Foundation's DvP program, so there's no program
of your own to write or deploy.

The tutorial takes about 40 minutes and follows one trade: an investor sells 4
shares of the Lisbon Solar Fund (LSF), the restricted demo asset from Issue an
asset, to another approved investor for USDC.

1. **Price** the trade: read the USDC/USD price from a Pyth price feed account
   and convert the dollar price of the shares into USDC.
2. **Record** the terms with the DvP program. It creates a trade record and one
   escrow account per leg, and moves no tokens.
3. **Fund** each leg: the seller sends the LSF and the buyer sends the USDC to
   the trade's escrow accounts, with ordinary token transfers.
4. **Settle**: the settlement agent named in the trade moves both legs in one
   transaction. If either escrow is short, settling fails and nothing moves.
5. **Close**: settling closes the trade record and both escrow accounts.

Every code block on these pages is a tested example, shown with its output on a
local test network (Surfpool) that copies accounts from mainnet when they're
first read. So the price is mainnet's USDC/USD price, and the DvP program is
the one deployed there.

<div className="sol-pcard not-prose" data-path="settle-trade">
<div className="sol-pcard-main">
<p className="sol-pcard-k">Settle a trade atomically · <span className="sol-nowrap">4 steps · \~40 min</span></p>
<p className="sol-pcard-title">[Settle a trade](/developers/docs/defi/settle-trade/try-it)</p>
<p className="sol-pcard-desc">Price a trade from a live oracle, then settle it so the asset and the USDC payment move in one transaction, or neither does.</p>
<ol className="sol-pcard-steps"><li>[Watch a trade settle](/developers/docs/defi/settle-trade/try-it)</li><li>[Read the code](/developers/docs/defi/settle-trade/browser)</li><li>[Run it from your machine](/developers/docs/defi/settle-trade/local)</li><li>[Go live](/developers/docs/defi/settle-trade/go-live)</li></ol>
</div>
<p className="sol-pcard-cta">[Start the tutorial →](/developers/docs/defi/settle-trade/try-it)</p>
</div>

<div className="sol-concept not-prose">
[<span className="sol-concept-name">Transactions</span> <span className="sol-concept-def">A transaction is a signed request to change the shared ledger.</span>](/developers/docs/core/transactions)
</div>

## Is this the right tool?

|                                       | Settlement through an agent's accounts         | DvP program on Solana                                          |
| ------------------------------------- | ---------------------------------------------- | -------------------------------------------------------------- |
| Who holds each leg before settlement  | The agent, or each side's custodian            | An escrow account per leg, controlled by the DvP program       |
| What makes both legs move together    | The agent's process and contracts              | One transaction: both transfers happen, or neither does        |
| What the settlement agent can do      | Move the funds it holds                        | Settle or cancel the trade. It can't change who gets paid      |
| Where the price comes from            | Your market-data feed                          | An oracle account that any app or program can read, here Pyth  |

<Columns cols={2}>
<Card title="A good fit if" icon="check" color="#16a34a">
- Both legs are tokens on Solana, such as a tokenized asset and USDC.
- Neither side should have to hand its leg to the other side first.
- The two parties agree the terms before settlement. The program does no matching or price discovery.
</Card>
<Card title="Not the right fit if" icon="x" color="#dc2626">
- One leg settles off the ledger, such as a bank wire. The program can only hold tokens.
- You need netting, partial fills, or an order book. One trade record is one bilateral trade.
- The asset's mint uses transfer fees, interest-bearing, or Scaled UI Amount. The program rejects those mints; use [DvP using delegation](/developers/docs/tokenization/dvp).
</Card>
</Columns>

## Already know what you need?

<Columns cols={2}>
<Card title="DvP program reference" icon="book" href="/developers/docs/defi/dvp-program">
Program ID, roles, accounts, errors, supported tokens, and limits.
</Card>
<Card title="DvP using delegation" icon="arrow-left-right" href="/developers/docs/tokenization/dvp">
Settle through token delegation instead of escrow.
</Card>
<Card title="Unwind a trade" icon="undo-2" href="/developers/docs/defi/dvp-program/unwind-a-trade">
Reclaim a leg, cancel or reject a trade, and recover a late deposit.
</Card>
<Card title="Issue an asset" icon="layers" href="/developers/docs/assets/issue-asset" arrow>
Create the restricted asset that this tutorial trades.
</Card>
</Columns>
