Settlement

Settlement

The one transaction that settles an accepted quote: what the desk builds, what the program checks, and what moves.

When the taker accepts a quote, the desk builds the settlement transaction, simulates it, and hands it to the taker’s wallet with a review of both legs and the fee. The taker’s signature is the only one on it. Its instructions run in the order below, and if any one fails, nothing moves.

The transaction

  1. Compute budget

    A compute unit limit and a priority price.

  2. The taker’s token accounts

    Two idempotent instructions that create the taker’s associated token accounts for the base and the quote mint if they do not exist yet. The taker pays their rent.

  3. Wrap SOL, when the taker pays in SOL

    A transfer of the exact payment into the taker’s wrapped SOL account, then a sync, so the program can move it as a token.

  4. Ed25519 verify

    The native Ed25519 program checks the maker’s signature over the quote message with the maker’s quote key.

  5. settle

    The program instruction, given the position of the Ed25519 instruction above. It checks the quote, records it as used, and moves both legs and the fee.

  6. Unwrap SOL, when the taker receives SOL

    The taker’s wrapped SOL account is closed, which returns its whole balance to the wallet as SOL, including any wrapped SOL it held before.

The position of the Ed25519 instruction is written into settle before signing. A wallet that inserts instructions ahead of it changes that position, and settle then fails with QuoteSignatureInvalid; the desk reports it as the wallet changing the transaction.

What settle checks

In this order, each failure with the program error named:

CheckError
The maker is activeMakerInactive
The side is 0 or 1, and both amounts are above zeroInvalidSide, InvalidAmount
The base and quote mints differ, and both are allowedSameMint, MintNotAllowed
The cluster time is at or before the quote’s expiryQuoteExpired
The Ed25519 instruction comes earlier in the transaction, verifies one signature by the maker’s current quote key, and its message is exactly this quote rebuilt from the settle argumentsQuoteSignatureInvalid
The used-quote account is the one derived from this quote’s hash, and the quote has not settled beforeInvalidUsedQuote, QuoteAlreadyUsed
The fee account is the fee recipient’s associated token account for the quote mintInvalidFeeAccount
Each vault that exists is an SPL Token account of its mint, held by the maker accountInvalidTokenAccount
The vault the maker pays from exists and holds the maker’s legMakerInventoryShort

Because the program rebuilds the signed message from the accounts and arguments of this transaction, a quote cannot be settled by another taker, against another maker, in other mints, or for other amounts.

What moves

The fee is the quote amount times the configured fee in basis points (5 bps today), divided by 10,000 and rounded down. Fees and the token says where it goes.

When the taker buys:

FromToAmount
Taker’s quote token accountMaker’s quote vaultthe quote amount
Taker’s quote token accountFee recipientthe fee
Maker’s base vaultTaker’s base token accountthe base amount

When the taker sells:

FromToAmount
Taker’s base token accountMaker’s base vaultthe base amount
Maker’s quote vaultTaker’s quote token accountthe quote amount minus the fee
Maker’s quote vaultFee recipientthe fee

The maker’s vaults always move exactly the amounts in the quote. The vault the maker pays out of must already exist and hold the maker’s leg. The vault it receives into is created by settle if it does not exist yet, and the taker pays its rent, so a maker can quote one side of a pair after depositing only that side.

The used-quote account

settle creates a small account at the address derived from ["quote", quote hash], holding the maker, the taker and the slot. The taker pays its rent, at the Rent sysvar’s current rate. Its existence is what makes a second settlement of the same quote fail with QuoteAlreadyUsed. If someone sends lamports to that address in advance, settle tops it up and takes it over, so this cannot block a fill.

The event

settle emits Settled with the quote hash, both parties, both mints, the side, both amounts and the fee. The desk’s indexer reads it to record the fill that appears in history. Its fields are in the Program reference.

Compute

Measured in compute units on the project’s local validator, on the current program. Compute units are the same on mainnet for the same program and accounts.

Casesettle instructionWhole transaction
Taker buys SOL, maker vaults exist, wSOL unwrapped to native SOL40,73961,060
Taker sells JitoSOL, maker vaults exist37,75348,881
Taker buys, maker's receiving vault created in the settle40,61352,439
Taker sells, maker's receiving vault created in the settle40,61052,436

Creating the taker’s token accounts or wrapping SOL also adds to the whole transaction.