Skip to content
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.
4 LSF for USDC · delivery versus paymentLocal test network (Surfpool)
Sellerdelivers 4 LSFHolds 1000 LSFHolds 1000 LSF4 LSF in escrowPaid in USDC · 996 LSF left
Buyerpays in USDCHolds 10 USDCHolds 10 USDCNot paid yetReceived 4 LSF
TradeDvP programNot pricedCreated · $5.00Settle failed · nothing movedSettled · record closed
  1. Price the trade and create it 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.
  2. Settle with the cash leg missing 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.
  3. Fund the cash leg and settle 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.
# Step 1/3 · Price the trade and create it
await priceTrade(4_000_000n, 1_250_000n)
USDC/USD from Pyth: 0.99985925 ± 0.00100406 (published 46 s ago)
Price: 4 LSF × $1.25 = $5.00 = 5.000704 USDC
await createTrade(agent, terms)
Trade record: 82dFJRtUvMUDDTPTL9E3EedtDeWVChCY7P1LwSXrEzyP
asset leg: 4 LSF, escrow GE848CF1hsrjBgwFooTUKXn9ZJPcao55UhGNMphZru2h
cash leg: 5.000704 USDC, escrow 7ZnhTD17hRP1iYgFw9D65LBU4EnmXZYYg1GrUrPSLPtF
Record checked: owner, size, address and terms match
Signature: 3djiyakhgCcQeieyRmaTWnX4wqoa3RavvrqjQb3SsdwxVf7XcqmB5m6nV3BDFasBg4Nss9hoTiFy6Waw1JbxFHtV
USDC/USD read from PythTrade created
# Step 2/3 · Settle with the cash leg missing
await approveEscrow(issuer, trade)
Issuer approved the LSF escrow for the trade. Signature: 2mZuEQfUJxkS8uy4zrZLRPgkutCncf4RCDKevk5r5J9LCHtaMNFBWwxtvaMbQRrjwfx7QFcXqQPEgsNv7wkLKazN
await fundAssetLeg(seller, trade)
Seller funded the asset leg. Signature: 24cpZdS3DyNWaFGkWeepUEogfgshrDxXJcmUJr9aiHwxLNFpWAyoHZ19MUSFcx3eywCepP6AwpB9t2heEhKJvu8A
await settleUnfunded(agent, trade)
Settle with the cash leg unfunded (skipPreflight): failed onchain
Failed signature: 2FfFCTfYghi7RJX1YKDhTCpxcLQfCc5ZMs5CVCuosPfmTmoSyGVE6aPHwsyv8BWkaePqSjpjYWqXbF825G7cUj6
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
Asset leg fundedFailed: leg not funded (Custom: 4)Nothing moved
# Step 3/3 · Fund the cash leg and settle
await fundCashLeg(buyer, trade)
Buyer funded the cash leg. Signature: 3FYLcpq3YKym1EbKMmDP7ZPvEcXxH62qU7VtnryzbxUm8uP8xTpvkoo3kNWv5y1jcyxrpegfD16T1fxmmdwD8eYo
await settle(agent, trade)
Settled both legs in one transaction. Signature: WSZ95HL5Urh75SqheweTvM9ZdKzK5QDmh1nepwUBd31BzMruREbduWU4F1MYFX15JduNTBaRgWLo9ruSZ64iDTy
Balances: buyer 4 LSF, 4.999296 USDC | seller 996 LSF, 5.000704 USDC
Trade record and escrows closed: true
Settled · both legs in one transaction
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.
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

Failed settle (excerpt of the output)
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.

​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). Next, you read the code that prices, records, and settles this trade.