
💻Conditional Orders
Conditional Orders Life Cycle

Structure
/// "co" == conditional order
/// @notice order details used to create an
/// order on a perps market within a co
struct OrderDetails {
// order market id
uint128 marketId;
// order account id
uint128 accountId;
// order size delta (of asset units expressed in
// decimal 18 digits). It can be positive or negative
int128 sizeDelta;
// settlement strategy used for the order
uint128 settlementStrategyId;
// acceptable price set at submission
uint256 acceptablePrice;
// bool to indicate if the order is reduce only;
// i.e. it can only reduce the position size
bool isReduceOnly;
// tracking code to identify the integrator
bytes32 trackingCode;
// address of the referrer
address referrer;
}
/// @notice co
struct ConditionalOrder {
// order details
OrderDetails orderDetails;
// address of the signer of the order
address signer;
// a means to prevent replay attacks and
// identify the order
uint256 nonce;
// option to require all extra conditions
// to be verified on-chain
bool requireVerified;
// address that can execute the order
// *if* requireVerified is false
address trustedExecutor;
// max fee denominated in $sUSD that
// can be paid to the executor
uint256 maxExecutorFee;
// array of extra conditions to be met
// on-chain *if* requireVerified is true
bytes[] conditions;
}Off-Chain Submission Process in Smart Margin v3
Conditional Order Placement
Order Conditions: A trader must first define the conditions for the execution of a conditional order.
Verification Choice: The trader decides if these conditions should be verified on-chain or off-chain.
Setting the Verification Flag: To make this distinction, the
ConditionalOrder.requireVerifiedflag is set totruefor on-chain verification, orfalsefor off-chain.On-Chain Verification: If
requireVerifiedistrue, the conditions inConditionalOrder.conditionsneed to be met on-chain.Off-Chain Verification: If
requireVerifiedisfalse, the specified conditions inConditionalOrder.conditionswill not undergo on-chain verification.Trusted Executor Assignment: For off-chain verification (
requireVerifiedisfalse), aConditionalOrder.trustedExecutoraddress must be set, indicating the trusted party for execution.Role of Trusted Executor: This executor is the sole entity capable of executing the order when
requireVerifiedisfalse, acting on the presumption that off-chain conditions are met.Note: The trader relies on the
ConditionalOrder.trustedExecutorto execute the order only when the off-chain conditions are satisfied.
Fee Specification: The trader specifies a maximum fee,
ConditionalOrder.maxExecutorFee, they are willing to pay the executor for successful execution.Order Signing: The trader signs the
ConditionalOrderusing their private key.Order Submission: The signed
ConditionalOrderis submitted to Kwenta, or another chosen party, for storage and processing by backend infrastructure.
On-Chain Execution Procedure
Account Crediting: If not done already, the trader credits their account to cover future execution fees. In Synthetix v3 Andromeda,
$USDCcan be converted to$snxUSDfor credit as demonstrated here.
Execution (Off- and On-Chain)
Condition Monitoring: Kwenta's backend infrastructure continuously monitors the conditions set in
ConditionalOrder.conditions.Execution Initiation: Upon condition fulfillment, an executor calls
Engine.execute, submitting theConditionalOrder(_co), its signature (_signature), and a proposed execution fee (_feein$snxUSD).Execution Validity Checks: The transaction will fail if the
_feeexceeds either theConditionalOrder.maxExecutorFeeor the account's available credit.On-Chain Verification for Execution: If
ConditionalOrder.requireVerifiedistrue, on-chain verification of conditions occurs before execution. This is gas-intensive and therefore expensive.Executor Flexibility with On-Chain Verification: Any address can execute the order if
requireVerifiedistrue.Off-Chain Verification for Execution: If
requireVerifiedisfalse, no on-chain verification occurs before execution, making it less gas-intensive and cheaper.Executor Restriction with Off-Chain Verification: The
ConditionalOrder.trustedExecutormust be the one executing the order ifrequireVerifiedisfalse.Execution Requirements: The
ConditionalOrderwill only be executed if specific criteria are met, including fee limits, account credit, nonce uniqueness, authorized signer, and signature validity.Order Execution: If all conditions are satisfied, the
ConditionalOrderis executed, and theConditionalOrder.nonceis marked as used.
Credit & Debit Management
With the integration of Conditional Orders (COs), the Engine contract now manages a credit balance for each account, utilized for executing successful conditional orders. This balance, denominated in $snxUSD, is not factored into the account's total margin, ensuring that its debit does not affect open position liquidation risks. Unlike previous versions, the credit in the engine is never locked, as conditional order submission is entirely off-chain, preventing the establishment of a fixed credit balance for outstanding conditional orders.
refer to this test suite for programmatic examples of managing account credit
Last updated
Was this helpful?