Skip to content
This step runs the trade from your terminal. The trade needs an asset to sell, so you add the DvP client and three files to your Issue an asset project. The output under each block is from a local Surfpool network copying mainnet.

​1. Start from your Issue an asset project

If you haven’t done it yet, follow Run it from your machine in the Issue an asset tutorial. You need its config.ts, create-asset.ts, approve-holder.ts, and blocked-transfer.ts, and an issuer key file. Each run creates a new LSF asset, and the issuer approves the investors and the trade’s escrow. Then install the two extra packages the trade uses: the SPL Token client for the USDC leg and the Memo program address that SettleDvp takes.
Terminal
The examples are tested with @solana-program/token 0.17 and @solana-program/memo 0.15.

​2. Add the DvP client

No package is published for the DvP client. Generate it from the program’s IDL at the commit the DvP program pages use, and copy it into your project as ./dvp. Run this in your project folder. It clones the repository next to the project and needs pnpm:
Terminal
The client depends only on @solana/kit, which the project already has.

​3. Start a local network

Run the trade on a local Surfpool network that copies accounts from mainnet: the DvP program, the Pyth price feed account, and the USDC mint are mainnet’s, and the test USDC is free.
  1. Install the Surfpool CLI and start it from mainnet, airdropping SOL to your issuer (replace <issuer address> with the address of your key file):
    Terminal
    Without --airdrop, Surfpool only funds the Solana CLI default key file, ~/.config/solana/id.json.
  2. In the terminal where you run the code, point it at Surfpool and at mainnet USDC. Unset, the code uses devnet and devnet USDC:
    Terminal
  3. Give the issuer test USDC with Surfpool’s surfnet_setTokenAccount cheatcode. Each run sends 6 USDC to the buyer, so 60 USDC covers ten runs. Repeat it to top up:
    Terminal
Devnet. The same code runs against devnet with the three variables unset. The examples were tested on a Surfpool network copying devnet, whose DvP program is an older build than the client’s commit (see Program ID). The issuer needs SOL from the Solana Faucet and 6 devnet USDC per run from Circle’s faucet. The devnet USDC/USD feed can update less often, so if the price check refuses the price as too old, set export MAX_PRICE_AGE_SECONDS=600.

​4. Add the price

Save this as price.ts. It’s the code from the previous step.
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
Surfpool copies the feed account once, the first time it’s read, so a later run on the same network fails the age check with price is … s old. Reset the account to copy it again, then rerun:
Terminal

​5. Add the trade

Save this as trade.ts. It imports the client from ./dvp and failure from the Issue an asset tutorial’s blocked-transfer.ts.
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
The agent pays the deposits for the trade record, its two escrow accounts, and a small marker account that stops the same trade address being used twice. When the agent settles, the record and the escrows are closed and their deposits go back to the agent. The marker account is never closed.

​6. Run it

Save this as trade-run.ts (run.ts is the Issue an asset script). It creates a new LSF asset, gives the seller, the buyer, and the agent a little SOL for fees and the buyer 6 USDC, approves the two investors (the seller gets 1000 LSF), then prices the trade, records it, funds the asset leg, tries to settle, funds the cash leg, and settles. The output starts with the new asset and the two approvals.
trade-run.ts
Output of npx tsx trade-run.tsLocal test network (Surfpool)
npx tsx trade-run.ts
Mint: PsxKt9wuT8VuCQDMkNCXrigRMeVgaopeNqBXCdeXJ5p
Extensions: MetadataPointer, PausableConfig, PermanentDelegate, DefaultAccountState, TokenMetadata
Signature: 27HUffYm1hJekCNgSjBW3AS4r1D2nBBEpqcvgrrBUV2EGZeqWhw9Txt3PUo3azPKN7yd9LA44i7cDxfJMPSznyo7
Fee: 10000 lamports
Opened 2itvBPXadQbV3ktWMEWsySC7gosnis8dFXxTeusj3weB state: Frozen
Approved 2itvBPXadQbV3ktWMEWsySC7gosnis8dFXxTeusj3weB state: Initialized balance: 1000 LSF
Signature: ebrnZWeehNAvJh8bGSs6aasntCS253LSekR52yivCspE8fzmpXpBFabPcb3ukXiYkZ8uSxPtYZ6Rsp3nGGGz4hS
Opened 7r6cYT2HEtnEEFdQYBiiNjMDFZG9tqEmVJDSyM9Fdhao state: Frozen
Approved 7r6cYT2HEtnEEFdQYBiiNjMDFZG9tqEmVJDSyM9Fdhao state: Initialized balance: 0 LSF
Signature: 4r8aaALCczy2tkg872LkibuyaZ48PSMSzF5iGEMRNbfhbFpMquum47MXfnV1bVZ5jddQsmB2qYe88eQ2ni23orVW
USDC/USD from Pyth: 0.99985944 ± 0.00100387 (published 4 s ago)
Price: 4 LSF × $1.25 = $5.00 = 5.000703 USDC
Trade record: 88XeBphVdRDrQRhirLyVNLGGLQmAoDmx5WtADNPM311C
asset leg: 4 LSF, escrow 9fQeTCqRsNfeUDdJ8QqvdBmYC14vh7JwiFUwAmpuvf97
cash leg: 5.000703 USDC, escrow HK92sq9rms2c1Nobn3w81mSGKeuQyhyAG6yQ1yYSjfFD
Record checked: owner, size, address and terms match
Signature: 2dStdFAm2x64nLj4bVyzZGbge2y6WHK1HcvpPt64aaVxRRrvQ44uT3XKH97bNQWAVHSwkcvjBZpdCDYSnUpMzNQG
Issuer approved the LSF escrow for the trade. Signature: 2nkyJ2GnC9nw3y1Z2XAsrGYf5ZiDbVbGiSHUnVXc8Nk9cAcRyyNwMLQ6QLaFVc7FasWheDHP2s1F2R4zsfHwfzPu
Seller funded the asset leg. Signature: 5V8hmsGn1xzUDUgkhkN7Y77nmuG3iPqH71aFXsmyU14cy2j5kwAZ8edSXiAFwd96RLqjTK8bPey4kg4AsnPsfkDz
Settle with the cash leg unfunded (skipPreflight): failed onchain
Failed signature: 2BLhaueJbDmv4UEqW9Zpgi2HzopT93keyqGkYwu3SkgdDsJ1JFCUQu2kLCHaGZwReypmGbvmQK4uvFxUShGkNFUU
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
Buyer funded the cash leg. Signature: Cci8NmtgrYLv8YAPbn6Vx8TzFkwbX9EgFjeSCy8Ltg9CMARPXe54eEtXBtv2koY7vDjotzynthDEAUnjiUHkkmn
Settled both legs in one transaction. Signature: 67VeoGZmkBBNuFnrpVVrPP77JXovfQVSPSVQ6cLWz3Hfe428WYyMATQXeMNWxEzncWjoS4FZhweUEWJY5VdtNvf6
Balances: buyer 4 LSF, 0.999297 USDC | seller 996 LSF, 5.000703 USDC
Trade record and escrows closed: true
First settle failed: leg not fundedSettled
Then run it:
Terminal
Expect the states and errors shown under the script, with your own addresses and signatures, and a USDC amount that follows the current price.

​Run the tests instead

The code on these pages and its tests live in the docs repository under packages/docs-examples/paths/defi/settle-trade. The test starts its own Surfpool network copying mainnet, so you need no Surfpool CLI, keys, SOL, or USDC:
Terminal
The tests pass when the first settle fails with {"Custom":4} and moves nothing, and the second moves 4 LSF to the buyer and the USDC to the seller and closes the trade.