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

# Watch a trade settle

> See a DvP trade at work: the price comes from a live Pyth feed, settlement fails with the cash leg missing, then both legs move in one transaction.

<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="current"><a href="/developers/docs/defi/settle-trade/try-it" aria-current="step"><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>

An investor sells 4 LSF, shares of the restricted demo asset from Issue an
asset, to another approved investor at \$1.25 each, paid in USDC. A settlement
agent, such as the venue that matched the trade, records the terms and is the
only one who can settle. Press **Run** to step through this tutorial's code on
a local test network (Surfpool). Nothing is sent from your browser, and you
need no code or wallet.

<div className="sol-term not-prose" data-term="paths/settle-trade/demo" data-kind="trade" data-steps="3" data-l-run="Run" data-l-run-step="Run step" data-l-next="Next step" data-l-all="Run all" data-l-again="Run again" data-l-running="Running…" data-l-tx="Sending transaction…" data-l-http="Requesting…" data-l-call="Running…" data-l-confirming="Confirming…" data-l-done="Done" data-l-copied="Copied" data-l-step="Step">
<div className="sol-term-head"><span className="sol-term-title">4 LSF for USDC · delivery versus payment</span><span className="sol-term-env">Local test network (Surfpool)</span><button type="button" className="sol-term-copy" data-act="copy">Copy</button></div>
<div className="sol-term-stage" data-stage="trade">
<div className="sol-term-item"><span className="sol-term-item-label">Seller</span><span className="sol-term-item-sub">delivers 4 LSF</span><span className="sol-term-pill" data-at="0" data-tone="info">Holds 1000 LSF</span><span className="sol-term-pill" data-at="1" data-tone="info">Holds 1000 LSF</span><span className="sol-term-pill" data-at="2" data-tone="warn">4 LSF in escrow</span><span className="sol-term-pill" data-at="3" data-tone="ok" data-final="">Paid in USDC · 996 LSF left</span></div>
<div className="sol-term-item"><span className="sol-term-item-label">Buyer</span><span className="sol-term-item-sub">pays in USDC</span><span className="sol-term-pill" data-at="0" data-tone="info">Holds 10 USDC</span><span className="sol-term-pill" data-at="1" data-tone="info">Holds 10 USDC</span><span className="sol-term-pill" data-at="2" data-tone="warn">Not paid yet</span><span className="sol-term-pill" data-at="3" data-tone="ok" data-final="">Received 4 LSF</span></div>
<div className="sol-term-item"><span className="sol-term-item-label">Trade</span><span className="sol-term-item-sub">DvP program</span><span className="sol-term-pill" data-at="0" data-tone="todo">Not priced</span><span className="sol-term-pill" data-at="1" data-tone="info">Created · \$5.00</span><span className="sol-term-pill" data-at="2" data-tone="error">Settle failed · nothing moved</span><span className="sol-term-pill" data-at="3" data-tone="ok" data-final="">Settled · record closed</span></div>
</div>
<ol className="sol-term-plan">
<li data-step="1"><span className="sol-term-plan-n" aria-hidden="true">1</span><span className="sol-term-plan-body"><strong className="sol-term-plan-title">Price the trade and create it</strong> <span className="sol-term-plan-desc">The settlement agent reads the USDC/USD price from Pyth's price feed account, converts 4 LSF at \$1.25 a share into USDC, and creates the trade with the DvP program.</span></span></li>
<li data-step="2"><span className="sol-term-plan-n" aria-hidden="true">2</span><span className="sol-term-plan-body"><strong className="sol-term-plan-title">Settle with the cash leg missing</strong> <span className="sol-term-plan-desc">The issuer approves the trade's escrow and the seller funds the asset leg. The buyer hasn't paid yet, so `SettleDvp` fails onchain with `Custom: 4` (*leg not funded*) and nothing moves.</span></span></li>
<li data-step="3"><span className="sol-term-plan-n" aria-hidden="true">3</span><span className="sol-term-plan-body"><strong className="sol-term-plan-title">Fund the cash leg and settle</strong> <span className="sol-term-plan-desc">The buyer sends the USDC to its escrow. The same `SettleDvp` now moves the LSF to the buyer and the USDC to the seller in one transaction, and closes the trade.</span></span></li>
</ol>
<div className="sol-term-screen" role="region" tabIndex="0" aria-label="4 LSF for USDC · delivery versus payment: Output">
<div className="sol-term-step" data-step="1" data-pending="tx">
<div className="sol-term-row sol-term-note">\# Step 1/3 · Price the trade and create it</div>
<div className="sol-term-row sol-term-cmd"><span className="sol-term-ps" aria-hidden="true">\></span> await priceTrade(4\_000\_000n, 1\_250\_000n)</div>
<div className="sol-term-row sol-term-ln">USDC/USD from Pyth\: 0.99985925 ± 0.00100406 (published 46 s ago)</div>
<div className="sol-term-row sol-term-ln"><span className="sol-term-k">Price\:</span> 4 LSF × \$1.25 = \$5.00 = 5.000704 USDC</div>
<div className="sol-term-row sol-term-cmd"><span className="sol-term-ps" aria-hidden="true">\></span> await createTrade(agent, terms)</div>
<div className="sol-term-row sol-term-ln">Trade record\: <span className="sol-h" title="82dFJRtUvMUDDTPTL9E3EedtDeWVChCY7P1LwSXrEzyP"><span className="sol-h-f">82dFJRtUvMUDDTPTL9E3EedtDeWVChCY7P1LwSXrEzyP</span><span className="sol-h-s" aria-hidden="true">82dF…EzyP</span></span></div>
<div className="sol-term-row sol-term-ln" data-indent="2">asset leg\: 4 LSF, escrow <span className="sol-h" title="GE848CF1hsrjBgwFooTUKXn9ZJPcao55UhGNMphZru2h"><span className="sol-h-f">GE848CF1hsrjBgwFooTUKXn9ZJPcao55UhGNMphZru2h</span><span className="sol-h-s" aria-hidden="true">GE84…ru2h</span></span></div>
<div className="sol-term-row sol-term-ln" data-indent="2">cash leg\: 5.000704 USDC, escrow <span className="sol-h" title="7ZnhTD17hRP1iYgFw9D65LBU4EnmXZYYg1GrUrPSLPtF"><span className="sol-h-f">7ZnhTD17hRP1iYgFw9D65LBU4EnmXZYYg1GrUrPSLPtF</span><span className="sol-h-s" aria-hidden="true">7Znh…LPtF</span></span></div>
<div className="sol-term-row sol-term-ln">Record checked\: owner, size, address and terms match</div>
<div className="sol-term-row sol-term-ln"><span className="sol-term-k">Signature\:</span> <span className="sol-h" title="3djiyakhgCcQeieyRmaTWnX4wqoa3RavvrqjQb3SsdwxVf7XcqmB5m6nV3BDFasBg4Nss9hoTiFy6Waw1JbxFHtV"><span className="sol-h-f">3djiyakhgCcQeieyRmaTWnX4wqoa3RavvrqjQb3SsdwxVf7XcqmB5m6nV3BDFasBg4Nss9hoTiFy6Waw1JbxFHtV</span><span className="sol-h-s" aria-hidden="true">3dji…FHtV</span></span></div>
<div className="sol-term-result"><span className="sol-term-badge" data-tone="info">USDC/USD read from Pyth</span><span className="sol-term-badge" data-tone="ok">Trade created</span></div>
</div>
<div className="sol-term-step" data-step="2" data-pending="tx">
<div className="sol-term-row sol-term-note">\# Step 2/3 · Settle with the cash leg missing</div>
<div className="sol-term-row sol-term-cmd"><span className="sol-term-ps" aria-hidden="true">\></span> await approveEscrow(issuer, trade)</div>
<div className="sol-term-row sol-term-ln">Issuer approved the LSF escrow for the trade. <span className="sol-term-k">Signature\:</span> <span className="sol-h" title="2mZuEQfUJxkS8uy4zrZLRPgkutCncf4RCDKevk5r5J9LCHtaMNFBWwxtvaMbQRrjwfx7QFcXqQPEgsNv7wkLKazN"><span className="sol-h-f">2mZuEQfUJxkS8uy4zrZLRPgkutCncf4RCDKevk5r5J9LCHtaMNFBWwxtvaMbQRrjwfx7QFcXqQPEgsNv7wkLKazN</span><span className="sol-h-s" aria-hidden="true">2mZu…KazN</span></span></div>
<div className="sol-term-row sol-term-cmd"><span className="sol-term-ps" aria-hidden="true">\></span> await fundAssetLeg(seller, trade)</div>
<div className="sol-term-row sol-term-ln">Seller funded the asset leg. <span className="sol-term-k">Signature\:</span> <span className="sol-h" title="24cpZdS3DyNWaFGkWeepUEogfgshrDxXJcmUJr9aiHwxLNFpWAyoHZ19MUSFcx3eywCepP6AwpB9t2heEhKJvu8A"><span className="sol-h-f">24cpZdS3DyNWaFGkWeepUEogfgshrDxXJcmUJr9aiHwxLNFpWAyoHZ19MUSFcx3eywCepP6AwpB9t2heEhKJvu8A</span><span className="sol-h-s" aria-hidden="true">24cp…vu8A</span></span></div>
<div className="sol-term-row sol-term-cmd"><span className="sol-term-ps" aria-hidden="true">\></span> await settleUnfunded(agent, trade)</div>
<div className="sol-term-row sol-term-ln" data-tone="error">Settle with the cash leg unfunded (skipPreflight)\: failed onchain</div>
<div className="sol-term-row sol-term-ln" data-tone="error">Failed <span className="sol-term-k">signature\:</span> <span className="sol-h" title="2FfFCTfYghi7RJX1YKDhTCpxcLQfCc5ZMs5CVCuosPfmTmoSyGVE6aPHwsyv8BWkaePqSjpjYWqXbF825G7cUj6"><span className="sol-h-f">2FfFCTfYghi7RJX1YKDhTCpxcLQfCc5ZMs5CVCuosPfmTmoSyGVE6aPHwsyv8BWkaePqSjpjYWqXbF825G7cUj6</span><span className="sol-h-s" aria-hidden="true">2FfF…cUj6</span></span></div>
<div className="sol-term-row sol-term-ln" data-tone="error" data-indent="2"><span className="sol-term-k">err\:</span> `{"InstructionError":[4,{"Custom":4}]}`</div>
<div className="sol-term-row sol-term-ln" data-tone="error" data-indent="2">DvP error 4\: DvP leg is not funded with the expected amount</div>
<div className="sol-term-row sol-term-ln" data-indent="2">fee paid anyway\: 5000 lamports</div>
<div className="sol-term-row sol-term-ln">Nothing moved\: asset escrow 4 LSF \| cash escrow 0 USDC \| buyer 0 LSF \| seller 0 USDC</div>
<div className="sol-term-result"><span className="sol-term-badge" data-tone="ok">Asset leg funded</span><span className="sol-term-badge" data-tone="error">Failed\: leg not funded (Custom\: 4)</span><span className="sol-term-badge" data-tone="info">Nothing moved</span></div>
</div>
<div className="sol-term-step" data-step="3" data-pending="tx">
<div className="sol-term-row sol-term-note">\# Step 3/3 · Fund the cash leg and settle</div>
<div className="sol-term-row sol-term-cmd"><span className="sol-term-ps" aria-hidden="true">\></span> await fundCashLeg(buyer, trade)</div>
<div className="sol-term-row sol-term-ln">Buyer funded the cash leg. <span className="sol-term-k">Signature\:</span> <span className="sol-h" title="3FYLcpq3YKym1EbKMmDP7ZPvEcXxH62qU7VtnryzbxUm8uP8xTpvkoo3kNWv5y1jcyxrpegfD16T1fxmmdwD8eYo"><span className="sol-h-f">3FYLcpq3YKym1EbKMmDP7ZPvEcXxH62qU7VtnryzbxUm8uP8xTpvkoo3kNWv5y1jcyxrpegfD16T1fxmmdwD8eYo</span><span className="sol-h-s" aria-hidden="true">3FYL…8eYo</span></span></div>
<div className="sol-term-row sol-term-cmd"><span className="sol-term-ps" aria-hidden="true">\></span> await settle(agent, trade)</div>
<div className="sol-term-row sol-term-ln">Settled both legs in one transaction. <span className="sol-term-k">Signature\:</span> <span className="sol-h" title="WSZ95HL5Urh75SqheweTvM9ZdKzK5QDmh1nepwUBd31BzMruREbduWU4F1MYFX15JduNTBaRgWLo9ruSZ64iDTy"><span className="sol-h-f">WSZ95HL5Urh75SqheweTvM9ZdKzK5QDmh1nepwUBd31BzMruREbduWU4F1MYFX15JduNTBaRgWLo9ruSZ64iDTy</span><span className="sol-h-s" aria-hidden="true">WSZ9…iDTy</span></span></div>
<div className="sol-term-row sol-term-ln"><span className="sol-term-k">Balances\:</span> buyer 4 LSF, 4.999296 USDC \| seller 996 LSF, 5.000704 USDC</div>
<div className="sol-term-row sol-term-ln">Trade record and escrows closed\: true</div>
<div className="sol-term-result"><span className="sol-term-badge" data-tone="ok">Settled · both legs in one transaction</span></div>
</div>
</div>
<div className="sol-term-success"><span className="sol-term-success-icon" aria-hidden="true">✓</span><span>Settled: 4 LSF reached the buyer and the USDC reached the seller in the same transaction. The earlier settle, with the cash leg missing, failed and moved nothing.</span></div>
<div className="sol-term-foot"><button type="button" className="sol-term-btn" data-act="run">Run step 1</button><button type="button" className="sol-term-btn" data-act="all" data-variant="secondary">Run all</button><span className="sol-term-status"></span><span className="sol-term-sr" role="status"></span></div>
</div>

The agent sent the same `SettleDvp` instruction twice. The first time the cash
leg was missing, so nothing moved. The second time both escrows held their
agreed amounts, and the LSF and the USDC changed hands in one transaction.

## Where the price came from

The first line of the run is the USDC/USD price, read from Pyth's price feed
account on mainnet. The shares cost \$5.00 for 4 LSF, and the buyer pays in
USDC, so the code divides the dollar amount by the USDC/USD price and rounds up
to the next USDC base unit. First it checks that the account belongs to Pyth's
receiver program, holds the USDC/USD feed, and has a recent, precise enough
price.

The price is fixed into the trade record when the trade is created. The DvP
program never reads the oracle: at settlement it only checks that each escrow
holds the agreed amount.

## Why the first settle failed

```text title="Failed settle (excerpt of the output)"
Settle with the cash leg unfunded (skipPreflight): failed onchain
  err: {"InstructionError":[4,{"Custom":4}]}
  DvP error 4: DvP leg is not funded with the expected amount
  fee paid anyway: 5000 lamports
Nothing moved: asset escrow 4 LSF | cash escrow 0 USDC | buyer 0 LSF | seller 0 USDC
```

How to read it:

- The settle transaction has five instructions. The first four create the token
  accounts that receive each side's proceeds, if they don't exist yet.
  Instruction 4, counting from 0, is `SettleDvp`.
- `{"Custom":4}` is the DvP program's error 4, `LegNotFunded`: an escrow holds
  less than its agreed amount. The buyer hadn't sent the USDC yet.
- Transactions are all-or-nothing, so none of the five instructions took
  effect. The LSF stayed in its escrow, and the buyer and the seller received
  nothing.
- It was sent with `skipPreflight`, so it landed as a failed transaction and the
  agent still paid the 5,000-lamport fee. Sent normally, the RPC node's
  simulation would reject it before it lands.

The trade stayed open, so the buyer could still pay. Once the cash escrow held
the agreed USDC, the same instruction settled both legs and closed the trade
record and both escrow accounts.

<div className="sol-concept not-prose">
[<span className="sol-concept-name">Program-derived addresses</span> <span className="sol-concept-def">A PDA is an address derived from a program plus some seeds.</span>](/developers/docs/core/pda)
</div>

## Why the issuer approved the escrow

LSF token accounts start frozen until the issuer approves them, and the trade's
escrow is a new LSF token account owned by the trade record. So before the
seller could fund the asset leg, the issuer approved (thawed) the escrow, as it
would a new investor. The asset's rules apply to the trade too: the issuer
could pause the asset or freeze the escrow while the trade is open (see
[supported tokens](/developers/docs/defi/dvp-program#supported-tokens)).

Next, you read the code that prices, records, and settles this trade.
