539 days ago
Automating Conditional Orders on Kwenta with Gelato Automation
Kwenta is a platform in the DeFi world that allows the trading of options and futures on Optimism. Traders can engage with a diverse array of both on-chain and real-world assets. Plus, with the innovative Smart Margin Wallet feature, advanced trading operations are streamlined and simplified.
One of the superpowers of Kwenta is its support for conditional orders, which include stop and limit orders. And the secret sauce behind this is the automation provided by Gelato.
So let's explore this exciting partnership and how Gelato's automation is improving the Kwenta experience.
What is Gelato Automation?
Gelato Automation enables developers to automate smart contracts securely and reliably using time-based and custom-logic triggers.
This allows web3 applications to automate complex processes, including executing smart contracts when certain conditions are met in the context of Kwenta, Gelato automates limit and stop-market orders.
The Conditional Orders
To understand how Gelato works with Kwenta, we first need to delve into conditional orders. Conditional orders in traditional trading are instructions sent to the broker to execute a trade if certain conditions are met. These could be stop orders, which trigger a sell order when the price of a stock falls to a certain level, or limit orders, which only execute at a specified price (or better).
Within Kwenta's smart contract-based framework, these orders can be intricate and computationally demanding. This is where Gelato comes in. The platform is designed to manage and automate the execution of these conditional orders seamlessly and efficiently.
Gelato's Role in Kwenta Simplified
When a user on Kwenta wants to make a trade only if a specific condition is met (say, a cryptocurrency reaches a certain price), they create what's known as a conditional order. Let's break down how this works with Gelato:
Gelato's role within Kwenta can be broken down into some key functions. Let's take a closer look at each one:
Placing Conditional Orders
The _placeConditionalOrder
function in Kwenta's smart contract is responsible for placing a conditional order. This order is registered both within the Kwenta system and with Gelato. It checks if the order has sufficient margin and if the position size changes (non-zero sizeDelta). If more margin is needed for the order, the function verifies the sufficiency and commits the margin. Then it creates a Gelato task related to this order.
function _placeConditionalOrder(
bytes32 _marketKey,
int256 _marginDelta,
int256 _sizeDelta,
uint256 _targetPrice,
ConditionalOrderTypes _conditionalOrderType,
uint256 _desiredFillPrice,
bool _reduceOnly
) internal {
if (_sizeDelta == 0) revert ZeroSizeDelta();
// if more margin is desired on the position we must commit the margin
if (_marginDelta > 0) {
_sufficientMargin(_marginDelta);
committedMargin += _abs(_marginDelta);
}
//Create and submit Gelato task for this conditional order
bytes32 taskId = _createGelatoTask();
// internally store the conditional order
conditionalOrders[conditionalOrderId] = ConditionalOrder({
…
});
EVENTS.emitConditionalOrderPlaced({
…
});
++conditionalOrderId;
}
Creating Gelato Tasks
The function _createGelatoTask
is responsible for creating a new Gelato task corresponding to each conditional order. This is where the link between Kwenta and Gelato is established.
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 is responsible for executing a conditional order on Kwenta. When called, it carries out several steps to complete the order.
It follows a series of steps to ensure the order is executed. First, it retrieves the order details and removes them from Kwenta's records.
Then, it pays any necessary fees to Gelato and figures out which market the order belongs to. It determines the price at which the order will be executed based on the current market conditions. If the order is marked as "reduce only," it ensures that it can only decrease the size of the position. Any margin that was set aside for the order is released.
Finally, it modifies the margin and submits the order to the market for execution. Once the order is filled, an event is triggered to let everyone know.
In simple terms, the executeConditionalOrder function ensures the order is executed correctly and keeps everything running smoothly on Kwenta.
What's Next for Kwenta and Gelato?
The Kwenta-Gelato partnership is just getting started. Kwenta is set to enhance its trading experience by improving automation with Gelato and integrating faster price feeds from Pyth's Oracle Network. Gelato tasks will be upgraded for quicker execution, while Pyth's more frequent price feeds will reduce latency and pricing discrepancies. This will lead to more reliable and efficient trading, enhancing risk management and trade automation.
About Gelato
Get ready to witness a new era of web3 as Gelato, relied upon by over 400 web3 projects, powers the execution of millions of transactions in DeFi, NFT, and Gaming.
Gelato currently offers four services:
-
Web3 Functions: Connect your smart contracts to off-chain data & computation by running decentralized cloud functions.
-
Automate: Automate your smart contracts by executing transactions automatically in a reliable, developer-friendly & decentralized manner
-
Relay: Give your users access to reliable, robust, and scalable gasless transactions via a simple-to-use API
-
Gasless Wallet: A powerful SDK that enables developers to provide a world-class UX by combining Gelato Relay + Safe's Smart Contract Wallet, enabling Account Abstraction
Witness the ongoing journey towards a decentralized future led by Gelato!