> ## Documentation Index
> Fetch the complete documentation index at: https://pond.dflow.net/llms.txt
> Use this file to discover all available pages before exploring further.

# Maker Async Fills

> How a maker fills or closes an order after DFlow selects its rfqOrder quote

An `rfqOrder` quote settles async, in two transactions. The user's transaction opens an RFQ order onchain that escrows the user's input, the settlement mint. The maker fills the order in a later transaction, on its own schedule before the deadline. DFlow does not call `/submit-transaction` for an `rfqOrder` quote.

## Selected Quote

DFlow calls `POST /selected` when it hands the transaction to the user. The call is a notification: DFlow does not wait for the answer, and the maker declines an order by not filling it.

```json theme={null}
{
  "requestId": "<same across retries>",
  "quoteId": "<the maker's quote id>",
  "actionType": "rfqOrder",
  "taker": "<user wallet, base58>",
  "orderAddress": "<base58>"
}
```

The maker checks that `quoteId` is one of its live quotes and records or queues `orderAddress` for filling. The call is idempotent per `quoteId` and `orderAddress`, and is delivered with the same retry as an [`rfqFill` selection](https://pond.dflow.net/equities/makers/sync-fills#selected-quote).

The notification arrives before the user signs. The order account may not exist yet, and may never exist if the user declines or the transaction fails or expires. The maker does not refuse the notification because the account is missing. It watches the chain for the account until the open transaction's blockhash expires, about 150 slots after DFlow built it.

When the account appears, the maker checks it before it acquires the equity. The order must name this maker, its mints must be the quote's `tokenIn` and `tokenOut`, its `quoted_in` and `quoted_out` must equal the values the quote implies, as in the [last look checks](https://pond.dflow.net/equities/makers/sync-fills.md#last-look), and its `maker_fee_ubps` must be the quote request's `feeBps` × 100. The fill instruction only has to match or improve on the order's own values, so this check is what ties the order to the maker's quote. DFlow does not send a signature for it.

## Fill Deadline

An order account stores a `last_fillable_slot`, inclusive. It is the earlier of the slot of the quote's expiry and the open slot plus up to 150 slots. DFlow sets the quote's expiry, 60 seconds after the quote by default, and the maker cannot change it. A maker that needs a different window declines the quote.

After `last_fillable_slot` the order can only be closed and refunded.

## RFQ Order Program

Orders live on the RFQ order program, `8dybMBo3dhQR49ACuWzZej9WiRzPpNkmpdjH7iBk6ip3`. Each address below derives from that program ID. DFlow will move the program to `DF1owRfqRoL3MrMETrzP7dxFKcQUwB15NGZ2HAggQFfA`, and every address below will change with it.

| Address | Seeds |
| - | - |
| Order | `["rfq_order", token_authority, maker, nonce]`, with `nonce` as a little-endian u64 |
| Order vault | `["rfq_order_vault", order]` |
| Event authority | `["__event_authority"]` |

An instruction or account discriminator is the first 8 bytes of the SHA-256 of its Anchor name.

| Item | Discriminator |
| - | - |
| `open_rfq_order` | `[181, 125, 33, 230, 205, 63, 67, 108]` |
| `fill_rfq_order` | `[50, 203, 135, 122, 42, 118, 61, 47]` |
| `close_rfq_order` | `[136, 239, 94, 148, 191, 126, 236, 96]` |
| `RfqOrder` account | `[54, 87, 220, 57, 114, 186, 87, 151]` |

### Order Account Layout

An order with a maker fee is 344 bytes: the 8-byte discriminator, then a 336-byte body. An order without one is 312 bytes and ends before `maker_fee_account`. The offsets below start after the discriminator.

| Offset | Field | Type |
| - | - | - |
| 0 | `refund_wallet` | Pubkey |
| 32 | `destination_wallet` | Pubkey |
| 64 | `rent_recipient` | Pubkey |
| 96 | `token_authority` | Pubkey |
| 128 | `maker` | Pubkey |
| 160 | `input_mint` | Pubkey |
| 192 | `output_mint` | Pubkey |
| 224 | `fee_account` | Pubkey |
| 256 | `nonce` | u64 |
| 264 | `quoted_in` | u64 |
| 272 | `quoted_out` | u64 |
| 280 | `last_fillable_slot` | u64 |
| 288 | `platform_fee_ubps` | u32 |
| 292 | `builder_code` | u16 |
| 294 | `bump` | u8 |
| 295 | `vault_bump` | u8 |
| 296 | `flags` | u8 |
| 297 | `maker_fee_ubps` | u16 |
| 304 | `maker_fee_account` | Pubkey |

`platform_fee_ubps` is the fee rate, where `1000000` is 100%. In `flags`, bit 0 charges the fee on the input instead of the output. Bit 1 requires the fill in the same transaction as the open, which is set for `rfqFill` orders and never for `rfqOrder` orders. Bit 2 charges the [maker fee](https://pond.dflow.net/equities/makers/maker-fees.md) on the input instead of the output. `maker_fee_ubps` is the maker fee rate, zero when the order has no maker fee, and `maker_fee_account` the token account it is paid to, present only when there is one.

## Fill an Order

The maker fills the order with its own transaction before the deadline, using `fill_rfq_order`. [Read the Fill Instruction](https://pond.dflow.net/equities/makers/sync-fills.md#read-the-fill-instruction) describes the data both fill instructions share. The accounts are under [Fill Instruction Accounts](#fill-instruction-accounts).

1. Read the order account and check it as described above.
2. Build the fill from the order's fields: the mints, the destination, refund and rent recipient accounts, and the `quoted_in` and `quoted_out` values.
3. Add compute budget instructions, set the maker as signer and fee payer, and sign.
4. Submit the transaction before `last_fillable_slot`.

The fill instruction carries `quoted_in` and `quoted_out`. The maker starts from the order's values, which it reads from the order account. They are the maker's own quote:

| Field | Meaning |
| - | - |
| `quoted_in` | The most input the maker receives after any input-side platform fee. |
| `quoted_out` | What the maker pays for `quoted_in`, before any output-side fee. |

* **Input:** a fill takes the lesser of the escrowed input and the amount that leaves the maker `quoted_in` after an input-side platform fee. The maker can state a `quoted_in` below the order's to take less.
* **Output:** the maker pays `received × quoted_out ÷ quoted_in`, rounded down, where `received` is the input its token account gained. The price does not change with the size of the fill. A platform fee or a Token-2022 transfer fee on the input reduces `received` and so reduces the payment.
* **Result:** a fill closes the order. Input the fill does not consume returns to the user in the same transaction. The events report the measured amounts.

### Price Improvement

A maker can fill at a better price than the order's. The program accepts a fill when:

* `quoted_in` is above zero and at most the order's `quoted_in`.
* `quoted_out` is at least the order's `quoted_out`.

The fill's price, `quoted_out ÷ quoted_in`, is then never below the order's, so the user gets the order's price or better and never less. A fill with a lower `quoted_out` or a higher `quoted_in` reverts.

* **Higher `quoted_out`:** the maker pays more for the same input.
* **Lower `quoted_in`:** the maker takes less input and the rest returns to the user as a refund. The payment is still `received × quoted_out ÷ quoted_in`, so taking `quoted_in` of input pays the whole `quoted_out`.

For an order with `quoted_in` 201,000,000 and `quoted_out` 1,005,000,000, a fill that states the same `quoted_in` and a `quoted_out` of 1,010,000,000 pays 1,010,000,000 for 201,000,000 of input. A fill that states a `quoted_in` of 100,000,000 and a `quoted_out` of 1,005,000,000 takes 100,000,000 of input, pays 1,005,000,000, and refunds the remaining input to the user.

The maker reads the escrowed amount from the order's escrow token account. The maker is responsible for stating a `quoted_in` the escrow covers. A fill that states a `quoted_in` above what the escrow holds takes the whole escrow and pays in proportion to it, as in the formula above.

### Fill Instruction Accounts

The instruction has 18 fixed accounts. The program pins each one to the order or to a known program id, except the maker's own token accounts, 12 and 13.

| Position | Account | Writable | Meaning |
| - | - | - | - |
| 0 | `maker` | Yes, signer | Must be the order's maker. Pays rent for any user token account the fill creates, reimbursed from the order's rent. |
| 1 | `order` | Yes | The RFQ order. The fill closes it. |
| 2 | `order_vault` | Yes | The order's escrow token account. |
| 3 | `input_mint` | Yes | The mint the maker receives. Writable so withheld Token-2022 transfer fees can be harvested. |
| 4 | `output_mint` | No | The mint the maker pays. |
| 5 | `input_token_program` | No | The owner of `input_mint`. |
| 6 | `output_token_program` | No | The owner of `output_mint`. |
| 7 | `destination_ata` | Yes | The destination wallet's token account for `output_mint`. It receives the payment. |
| 8 | `destination_wallet` | No | The order's destination wallet. |
| 9 | `refund_ata` | Yes | The refund wallet's token account for `input_mint`. It receives any unused input. |
| 10 | `refund_wallet` | No | The order's refund wallet. |
| 11 | `rent_recipient` | Yes | Receives the order's rent. |
| 12 | `maker_receive_account` | Yes | The maker's token account that receives the input. Its mint must be `input_mint`. |
| 13 | `maker_source_account` | Yes | The maker's token account that pays the output. |
| 14 | `associated_token_program` | No | The associated token program. |
| 15 | `system_program` | No | The system program. |
| 16 | `event_authority` | No | The program's event authority. |
| 17 | `program` | No | The RFQ order program. |

Remaining accounts follow, in this order:

1. **Fee recipient:** when the order has a platform fee. It must be the order's fee account, and it is writable.
2. **Maker fee account:** the order's `maker_fee_account`, when the order has a maker fee. See [Maker Fee](https://pond.dflow.net/equities/makers/maker-fees.md).
3. **Transfer hook accounts:** any extra accounts the Token-2022 transfer hooks of the mints need.

## Fill Conditions

A fill succeeds only when all of these hold. Otherwise the transaction reverts and nothing moves.

* The maker signs, and the slot is at or before `last_fillable_slot`.
* Every account is the one the order names, apart from the maker's two token accounts.
* The fill's `quoted_in` is above zero and at most the order's, and its `quoted_out` is at least the order's.
* The maker's input account gains something. A Token-2022 transfer fee or an input-side platform fee cannot take the whole input.
* The payment is at least one unit: `received × quoted_out` must be at least `quoted_in`.
* The maker's source account holds the payment plus any sell-side maker fee and pays out exactly that, and its receive account keeps exactly `received` less any buy-side maker fee.
* With a maker fee, the remaining accounts name the order's `maker_fee_account`, as in [Maker Fee](https://pond.dflow.net/equities/makers/maker-fees.md).
* The user's account receives the whole payment less any output-side platform fee. The destination is not frozen and the output mint has no transfer fee.
* Any refund, when the escrow exceeds what the fill takes, can reach the user's refund account.
* Each Token-2022 mint's transfer hook gets its extra accounts.

A fee account that is missing, frozen, or of another mint never blocks a fill. The fee is skipped and the user keeps it. A missing or frozen maker fee account never blocks one either: the maker fee is skipped and the maker keeps it.

## Close an Order

Before `last_fillable_slot`, only the order's maker can close the order. A close refunds the whole escrow to the user. The maker closes an order it cannot fill, for example when it cannot acquire the equity. The user cannot close an order. After `last_fillable_slot`, anyone can close it.

The `close_rfq_order` instruction data is its discriminator alone. It takes 12 accounts, followed by any Token-2022 transfer hook accounts for the refund.

| Position | Account | Writable | Meaning |
| - | - | - | - |
| 0 | `closer` | Yes, signer | The order's maker before `last_fillable_slot`, any account after. Pays for the refund account if the close creates it, reimbursed from the order's rent. |
| 1 | `order` | Yes | The RFQ order. The close closes it. |
| 2 | `order_vault` | Yes | The order's escrow token account. |
| 3 | `input_mint` | Yes | The escrowed mint. Writable so withheld Token-2022 transfer fees can be harvested. |
| 4 | `input_token_program` | No | The owner of `input_mint`. |
| 5 | `refund_ata` | Yes | The refund wallet's token account for `input_mint`. It receives the escrow. |
| 6 | `refund_wallet` | No | The order's refund wallet. |
| 7 | `rent_recipient` | Yes | Receives the order's rent. |
| 8 | `associated_token_program` | No | The associated token program. |
| 9 | `system_program` | No | The system program. |
| 10 | `event_authority` | No | The program's event authority. |
| 11 | `program` | No | The RFQ order program. |

## Order Events

Each transition of an order emits one `RfqOrderEvent`: open, fill, cancel (a close before the deadline) and expire (a close after it). The open event names the order and its maker. [Onchain Fill Parsing](https://pond.dflow.net/equities/onchain-fill-parsing.md) describes how to read the events.
