Skip to content
This is the code behind the trade in the previous step, in two files: price.ts reads the price and converts the shares into USDC, and trade.ts records, funds, and settles the trade. Press Run under each block to step through its output on a local test network (Surfpool). Nothing is sent from your browser, and there’s nothing to install.

​1. Price the trade

price.ts
Output of priceTrade(4_000_000n, 1_250_000n)Local test network (Surfpool)
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
USDC/USD read from Pyth
A Pyth price feed account holds Pyth’s latest price for one feed, and anyone can read it with an ordinary account lookup. The code reads the USDC/USD feed account Dpw1…eFbXPyth USDC/USD price feed accountDpw1EAVrSB1ibxiDQyTAW6Zip3J4Btk2x4SgApQCeFbXView on Solana Explorer (Pyth USDC/USD price feed account) and checks it before using the price:
  1. The owner. The account must belong to Pyth’s receiver program (rec5EKMG…LtFJ). Anyone can create an account with bytes that look like a price, but only that program can write to accounts it owns.
  2. The layout and feed. The first 8 bytes identify a PriceUpdateV2 account, and the feed ID inside it must be USDC/USD.
  3. Verification. The update must be fully verified (byte 40 is 1).
  4. Age. publishTime must be at most MAX_PRICE_AGE_SECONDS old, 120 by default. Pyth updates this feed on mainnet every 55 seconds, or sooner when the price moves by 0.5% (sponsored feeds).
  5. Confidence. Pyth publishes a confidence interval with each price. The code refuses a price whose interval is wider than 0.5% of the price.
priceTrade then converts the dollar amount into USDC at that price, rounding up to the next base unit. Pyth’s values are integers with an exponent: a price of 99987452 with an exponent of -8 is 0.99987452. The feed address and the receiver program address are the same on mainnet and devnet (see Pyth’s contract addresses).

​2. Record, fund, and settle

trade.ts
Settle with the cash leg unfunded, then with both legs fundedLocal test network (Surfpool)
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
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
Failed: leg not funded (Custom: 4)Settled · both legs in one transaction
trade.ts uses the client generated from the DvP program’s IDL (./dvp).
  • createTrade derives the trade record’s address from the agent, the two parties, the two mints, and a random nonce, and sends CreateDvp with both amounts and an expiry one day ahead. The agent pays the deposits for the record and both escrows, and gets them back when it settles. Then verifyTrade reads the record back.
  • verifyTrade checks that the record is owned by the DvP program, is exactly 458 bytes, sits at the address its terms derive, and holds the agreed parties, mints, amounts, and payout addresses. Creating a record is open to anyone, so every party checks it before acting on it (see verify the terms). settle runs the same check before the agent signs.
  • approveEscrow thaws the LSF escrow with the issuer’s freeze authority. Without it, the seller’s transfer into a frozen account fails.
  • fundAssetLeg and fundCashLeg are plain TransferChecked transfers into the escrows. LSF is a Token-2022 token and USDC an SPL Token token; one trade can mix the two.
  • settle creates the four token accounts that settlement pays into, if they don’t exist, and sends SettleDvp, signed by the agent. The program checks both escrows, moves 4 LSF to the buyer, and the USDC to the seller, returns any surplus, and closes the record and both escrows.
  • settleUnfunded sends the same settle with skipPreflight, so the failed attempt lands onchain and the code can read its error.
The DvP program moves the tokens by calling the Token-2022 program for the LSF leg and the Token program for the USDC leg, inside the one settle transaction. To run this code yourself and change the trade, continue to the next step.