onChainOrder contract handles placing limit and market orders on-chain. Orders are submitted to the contract and executed by the protocol’s execution engine when price conditions are met.
Contract information
- Script hash
- Reference UTxO
- Script CBOR
Transaction structure
Inputs
Any user UTxOs to cover:- Input asset amount (what the user is selling)
- Transaction fees
- Order collateral (minimum 2 ADA)
- Deposit ADA for outputs
Order output
Send to the script address constructed from the script hash and optionally the user’s stake credentials. Address construction:| Asset | Amount |
|---|---|
| Input asset | inputAmount |
| ADA (execution fee) | feePerExStep * (orderType === 'limit' ? 3 : 1) |
| ADA (deposit) | depositAdaForOrder + depositAdaForReceive |
| ADA (collateral) | Additional if total < 2 ADA |
feePerExStep=600_000lovelace (0.6 ADA per execution step)- Limit orders require 3x fee (1.8 ADA), market orders 1x fee (0.6 ADA)
depositAdaForOrder= minimum ADA to store the order UTxOdepositAdaForReceive= minimum ADA for the user’s output after execution- Additional collateral added if total < 2 ADA minimum
Order types
Limit order
- Execution fee: 1.8 ADA (3 x 0.6 ADA)
- Beacon prefix:
0x00 - Executes only when market price reaches the specified price
- Can be cancelled by the user
Market order
- Execution fee: 0.6 ADA (1 x 0.6 ADA)
- Beacon prefix:
0x01 - Executes at current market price
- Can be cancelled by the user
Deposit ADA calculation
Order output must contain enough ADA to cover:Minimum UTxO deposit
Calculated based on output value size (input asset + ADA) and datum size (~200–300 bytes). Use Cardano’s
minUTxOValue formula or protocol parameters.Collateral
Minimum total 2 ADA. If execution fee + UTxO deposit is less than 2 ADA, add the difference.
| Component | Amount |
|---|---|
| Input asset | 50 ADA |
| Execution fee | 0.6 ADA |
| UTxO deposit | ~1.5 ADA |
| Receiving deposit | ~1.5 ADA |
| Additional collateral | 0 ADA (total already > 2) |
| Total | ~53.6 ADA |
Transaction building flow
Build datum
Construct all fields except beacon. See datum building.
Calculate beacon
Use the first input UTxO reference and order index. See beacon calculation.
Important notes
The beacon calculation uses the first transaction input. Add the UTxO to transaction inputs before calculating the beacon.
- Order index must match the actual index of the order output in the transaction outputs array.
- Minimum collateral — total deposits must be at least 2 ADA. Additional collateral is added automatically if needed.
- Stake credentials are optional but recommended for proper fund ownership and staking rewards.
- Price inversion — buy orders require inverted price ratios (
denom/numinstead ofnum/denom). - Asset format — ADA is
["", ""], tokens are[policyId, tokenName]in hex. - Execution — orders are executed by protocol batchers when conditions are met.