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:
Where:
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:1
Execution fee
Limit order: 1.8 ADA (3 x 0.6 ADA). Market order: 0.6 ADA (1 x 0.6 ADA).
2
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.3
Collateral
Minimum total 2 ADA. If execution fee + UTxO deposit is less than 2 ADA, add the difference.
4
Receiving deposit
ADA needed for the user’s output after execution. Depends on expected output asset and address.
Transaction building flow
1
Select input UTxOs
Cover all costs: input amount + fees + deposits.
2
Extract user credentials
Payment and stake key hashes from the user’s address.
3
Calculate order index
Position of the order output in the transaction.
4
Build datum
Construct all fields except beacon. See datum building.
5
Calculate beacon
Use the first input UTxO reference and order index. See beacon calculation.
6
Serialize and assemble
Serialize datum with the calculated beacon to CBOR. Calculate deposits, add collateral if needed, create the order output with script hash, stake credentials, value, and datum. Add a change output for the user.
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.