Automating Conditional Orders on Kwenta with Gelato Automation

Gelato Team

Jul 10, 2023

Kwenta is a DeFi platform for trading options and futures on Optimism, giving traders access to both on-chain and real-world assets. With its Smart Margin Wallet, advanced trading operations are simplified for users.

One of Kwenta’s standout features is its support for conditional orders (stop and limit orders), powered by Gelato Automation.

What is Gelato Automation?

Gelato Automation enables developers to automate smart contracts with time-based or custom-logic triggers. For Kwenta, this means limit and stop-market orders are executed automatically and reliably when predefined conditions are met.

The Conditional Orders

Conditional orders are standard in traditional trading: stop orders trigger a sell when an asset’s price falls below a threshold, and limit orders only execute at a specified price or better.

On-chain, these orders are computationally complex. Gelato manages this complexity for Kwenta, automating and executing conditional orders seamlessly.

Gelato’s Role in Kwenta Simplified

When Kwenta users set conditional orders, Gelato ensures they are monitored and executed when the conditions are met. The integration works in three main stages:

Placing Conditional Orders

Kwenta’s _placeConditionalOrder function registers the order and creates a Gelato task for automated monitoring and execution.

function _placeConditionalOrder(
    bytes32 _marketKey,
    int256 _marginDelta,
    int256 _sizeDelta,
    uint256 _targetPrice,
    ConditionalOrderTypes _conditionalOrderType,
    uint256 _desiredFillPrice,
    bool _reduceOnly
) internal {
    if (_sizeDelta == 0) revert ZeroSizeDelta();

    if (_marginDelta > 0) {
        _sufficientMargin(_marginDelta);
        committedMargin += _abs(_marginDelta);
    }

    // Create and submit Gelato task
    bytes32 taskId = _createGelatoTask();

    // Store the order internally
    conditionalOrders[conditionalOrderId] = ConditionalOrder({  });

    EVENTS.emitConditionalOrderPlaced({  });

    ++conditionalOrderId;
}

Creating Gelato Tasks

The _createGelatoTask function links Kwenta to Gelato by creating a monitoring task for each order.

function _createGelatoTask() internal returns (bytes32 taskId) {
    IOps.ModuleData memory moduleData = _createGelatoModuleData();

    taskId = IOps(OPS).createTask({
        execAddress: address(this),
        execData: abi.encodeCall(
            this.executeConditionalOrder, conditionalOrderId
        ),
        moduleData: moduleData,
        feeToken: ETH
    });
}

Executing Conditional Orders

The executeConditionalOrder function finalizes the trade when the conditions are met. It:

  • Retrieves and removes the order from Kwenta records

  • Pays execution fees to Gelato

  • Determines the execution price from current market data

  • Ensures reduce-only orders can only decrease positions

  • Releases any committed margin

  • Submits the order for execution and emits an event upon fill

In short, Gelato ensures conditional orders execute correctly, reducing friction for Kwenta users.

What’s Next for Kwenta and Gelato?

The partnership is evolving. Kwenta plans to upgrade automation through Gelato while integrating faster price feeds from Pyth Oracle Network. Together, faster execution and improved price accuracy will lead to more reliable, efficient trading with reduced latency and better risk management.

About Gelato

Gelato is web3’s decentralized backend, trusted by 400+ projects to power millions of automated transactions across DeFi, NFT, and Gaming.

Its services include:

  • Web3 Functions: Connect smart contracts to off-chain data & computation.

  • Automate: Automate smart contract execution with custom triggers.

  • Relay: Enable gasless, secure transactions with a simple API.

  • Gasless Wallet: SDK combining Gelato Relay + Safe’s Smart Contract Wallet for Account Abstraction.

Witness the ongoing journey toward a decentralized future led by Gelato!

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.

Ready to build?

Start with a testnet, launch your mainnet in days, and scale with industry-leading UX.