2/5
## Analyzing a DeFi Bracket Order Transaction This lesson explores the structure and parameters of a common decentralized finance (DeFi) transaction: opening a leveraged position while simultaneously setting both a Take Profit (TP) and a Stop Loss (SL) order. We'll analyze how these actions are bundled together efficiently and how to interpret the specific parameters that define the conditional orders, using a real-world example of a short ETH position. The goal of the transaction we'll examine is to open a short position on Ethereum (ETH) and establish two conditional orders: one to close the position if the ETH price falls to $2200 (Take Profit), and another to close it if the price rises to $2260 (Stop Loss). The entire operation occurs within a single blockchain transaction. ### The Multicall Structure: Atomicity and Efficiency Modern DeFi protocols often utilize a `multicall` pattern, as seen in this transaction's interaction with an `ExchangeRouter` contract. The `multicall` function allows multiple distinct function calls to be grouped and executed sequentially within a single, atomic transaction. This offers two key benefits: 1. **Atomicity:** Either all the bundled actions succeed, or the entire transaction reverts. This prevents partial execution, such as opening a position but failing to set the Stop Loss. 2. **Gas Efficiency:** Bundling calls often results in lower overall gas costs compared to executing each action in separate transactions. Within this specific `multicall` transaction, we observe a repeating sequence: 1. A call to `sendWnt(...)`. 2. A call to `createOrder(...)`. This pattern repeats for each order being created. The `sendWnt` function call preceding each `createOrder` serves to send the required fee (likely in Wrapped Native Token, such as WETH or WNT) needed to execute the conditional order (`createOrder`) later when its price trigger is met. This fee typically covers the gas cost for the keeper network that monitors and executes these off-chain triggered orders. In our example transaction, the `multicall` contains three `createOrder` calls: 1. The first creates the initial short position (details not covered here). 2. The second establishes the Stop Loss order. 3. The third establishes the Take Profit order. We will now dive into the parameters of the Stop Loss and Take Profit orders. ### Decoding the Stop Loss Order By inspecting the second `createOrder` call within the transaction trace, we can analyze its input parameters to understand how the Stop Loss is defined. **1. Identifying the Order Type:** The `orderType` parameter holds a numerical value, which in this case is `6`. To understand what this means, we refer to the protocol's `Order.sol` smart contract, specifically the `OrderType` enum: ```solidity enum OrderType { MarketSwap, // 0 LimitSwap, // 1 MarketIncrease, // 2 LimitIncrease, // 3 MarketDecrease, // 4 LimitDecrease, // 5 StopLossDecrease // 6 // ... other types } ``` Counting from 0, we see that `orderType` 6 corresponds to `StopLossDecrease`. This enum member signifies an order designed to decrease (close or partially close) an existing position when the market price moves unfavorably beyond a specified trigger price. **2. Verifying the Trigger Price:** Within the input parameters (often nested under a field like `numbers`), we find the `triggerPrice`. For this Stop Loss order, the value is `2260000000000000`. This large integer doesn't directly represent $2260; it includes fixed-point decimal precision used by the smart contract to avoid floating-point arithmetic issues. Assuming the price feed used by this protocol has 12 decimals of precision (a common convention, but always verify for the specific protocol), we can interpret the value: `Trigger Price = 2260000000000000 / 10^12 = 2260.0` This confirms the Stop Loss is set to trigger if the ETH price rises to $2260 USD. Understanding the decimal precision is crucial for correctly interpreting on-chain price data. **3. Confirming Position Sizing:** The parameters `sizeDeltaUsd` and `initialCollateralDeltaAmount` specify the amount of the position to be closed when the order triggers. In this transaction, these values are set to match the *entire size and collateral* of the user's current position. This configuration ensures that if the Stop Loss price is hit, the *entire* short position will be closed, not just a portion of it. ### Decoding the Take Profit Order Next, we examine the third `createOrder` call in the `multicall` sequence, which corresponds to the Take Profit order. **1. Identifying the Order Type:** The `orderType` parameter for this call is `5`. Referring back to the `OrderType` enum in `Order.sol`: ```solidity enum OrderType { // ... previous types ... MarketDecrease, // 4 LimitDecrease, // 5 // <--- Take Profit StopLossDecrease // 6 // ... other types } ``` `OrderType` 5 corresponds to `LimitDecrease`. In the context of closing a position based on favorable price movement, a `LimitDecrease` order functions as a Take Profit. It's designed to decrease the position when the price reaches or surpasses the specified trigger price in a favorable direction (price going down for a short position). **2. Verifying the Trigger Price:** The `triggerPrice` parameter for this order is `2200000000000000`. Applying the same 12-decimal precision assumption: `Trigger Price = 2200000000000000 / 10^12 = 2200.0` This confirms the Take Profit is correctly set to trigger if the ETH price drops to $2200 USD. **3. Confirming Position Sizing:** Similar to the Stop Loss, the `sizeDeltaUsd` and `initialCollateralDeltaAmount` parameters are set to the full position size. This means the Take Profit order will also close the entire position when executed. **4. Understanding Profit/Loss Handling (Swap Type):** A crucial parameter for decrease orders is `decreasePositionSwapType`. For this Take Profit order, its value is `1`. To interpret this, we look at the `DecreasePositionSwapType` enum, also likely defined in `Order.sol`: ```solidity enum DecreasePositionSwapType { NoSwap, // 0 SwapPnlTokenToCollateralToken, // 1 // <--- Used here SwapCollateralTokenToPnlToken // 2 } ``` Value `1` corresponds to `SwapPnlTokenToCollateralToken`. This setting dictates how the realized profit or loss (PnL) is handled when the position is closed. Since this is a short ETH position, the profit would naturally be realized in ETH (as the liability decreases). However, with `SwapPnlTokenToCollateralToken` selected, the protocol will automatically swap the profit (ETH) into the user's collateral token. Assuming the collateral used was USDC, the ETH profit will be converted to USDC and added to the user's balance within the protocol upon execution of the Take Profit order. If `NoSwap` (0) were selected, the profit would remain in ETH. ### Key Concepts Recap Analyzing this transaction highlights several important DeFi concepts: * **Multicall:** Enables bundling multiple actions into a single, atomic, and often gas-efficient transaction. Essential for complex operations like opening a position with bracket orders (TP/SL). * **Order Types (`OrderType` enum):** Smart contracts use enums to define distinct behaviors and trigger logic for various conditional orders (Market, Limit, Stop Loss, etc.). Understanding the specific enum definition is key to interpreting order parameters. * **Trigger Price & Decimals:** Prices in smart contracts are typically stored as large integers representing fixed-point numbers. You must know the contract's or price feed's decimal precision to convert these values into human-readable prices. * **Position Sizing Parameters:** Fields like `sizeDeltaUsd` control how much of a position an order affects. Setting them to the full position amount ensures a complete closure upon execution. * **PnL Swap Logic (`DecreasePositionSwapType` enum):** Determines the final token denomination of realized profits or losses when closing a position, allowing automatic conversion to the collateral token if desired. By understanding these parameters and structures, you can effectively analyze and debug complex DeFi transactions involving conditional orders, gaining deeper insight into how trading strategies are implemented on-chain.
This lesson explores the structure and parameters of a common decentralized finance (DeFi) transaction: opening a leveraged position while simultaneously setting both a Take Profit (TP) and a Stop Loss (SL) order. We'll analyze how these actions are bundled together efficiently and how to interpret the specific parameters that define the conditional orders, using a real-world example of a short ETH position.
The goal of the transaction we'll examine is to open a short position on Ethereum (ETH) and establish two conditional orders: one to close the position if the ETH price falls to $2200 (Take Profit), and another to close it if the price rises to $2260 (Stop Loss). The entire operation occurs within a single blockchain transaction.
Modern DeFi protocols often utilize a multicall
pattern, as seen in this transaction's interaction with an ExchangeRouter
contract. The multicall
function allows multiple distinct function calls to be grouped and executed sequentially within a single, atomic transaction. This offers two key benefits:
Atomicity: Either all the bundled actions succeed, or the entire transaction reverts. This prevents partial execution, such as opening a position but failing to set the Stop Loss.
Gas Efficiency: Bundling calls often results in lower overall gas costs compared to executing each action in separate transactions.
Within this specific multicall
transaction, we observe a repeating sequence:
A call to sendWnt(...)
.
A call to createOrder(...)
.
This pattern repeats for each order being created. The sendWnt
function call preceding each createOrder
serves to send the required fee (likely in Wrapped Native Token, such as WETH or WNT) needed to execute the conditional order (createOrder
) later when its price trigger is met. This fee typically covers the gas cost for the keeper network that monitors and executes these off-chain triggered orders.
In our example transaction, the multicall
contains three createOrder
calls:
The first creates the initial short position (details not covered here).
The second establishes the Stop Loss order.
The third establishes the Take Profit order.
We will now dive into the parameters of the Stop Loss and Take Profit orders.
By inspecting the second createOrder
call within the transaction trace, we can analyze its input parameters to understand how the Stop Loss is defined.
1. Identifying the Order Type:
The orderType
parameter holds a numerical value, which in this case is 6
. To understand what this means, we refer to the protocol's Order.sol
smart contract, specifically the OrderType
enum:
Counting from 0, we see that orderType
6 corresponds to StopLossDecrease
. This enum member signifies an order designed to decrease (close or partially close) an existing position when the market price moves unfavorably beyond a specified trigger price.
2. Verifying the Trigger Price:
Within the input parameters (often nested under a field like numbers
), we find the triggerPrice
. For this Stop Loss order, the value is 2260000000000000
. This large integer doesn't directly represent $2260; it includes fixed-point decimal precision used by the smart contract to avoid floating-point arithmetic issues.
Assuming the price feed used by this protocol has 12 decimals of precision (a common convention, but always verify for the specific protocol), we can interpret the value:
Trigger Price = 2260000000000000 / 10^12 = 2260.0
This confirms the Stop Loss is set to trigger if the ETH price rises to $2260 USD. Understanding the decimal precision is crucial for correctly interpreting on-chain price data.
3. Confirming Position Sizing:
The parameters sizeDeltaUsd
and initialCollateralDeltaAmount
specify the amount of the position to be closed when the order triggers. In this transaction, these values are set to match the entire size and collateral of the user's current position. This configuration ensures that if the Stop Loss price is hit, the entire short position will be closed, not just a portion of it.
Next, we examine the third createOrder
call in the multicall
sequence, which corresponds to the Take Profit order.
1. Identifying the Order Type:
The orderType
parameter for this call is 5
. Referring back to the OrderType
enum in Order.sol
:
OrderType
5 corresponds to LimitDecrease
. In the context of closing a position based on favorable price movement, a LimitDecrease
order functions as a Take Profit. It's designed to decrease the position when the price reaches or surpasses the specified trigger price in a favorable direction (price going down for a short position).
2. Verifying the Trigger Price:
The triggerPrice
parameter for this order is 2200000000000000
. Applying the same 12-decimal precision assumption:
Trigger Price = 2200000000000000 / 10^12 = 2200.0
This confirms the Take Profit is correctly set to trigger if the ETH price drops to $2200 USD.
3. Confirming Position Sizing:
Similar to the Stop Loss, the sizeDeltaUsd
and initialCollateralDeltaAmount
parameters are set to the full position size. This means the Take Profit order will also close the entire position when executed.
4. Understanding Profit/Loss Handling (Swap Type):
A crucial parameter for decrease orders is decreasePositionSwapType
. For this Take Profit order, its value is 1
. To interpret this, we look at the DecreasePositionSwapType
enum, also likely defined in Order.sol
:
Value 1
corresponds to SwapPnlTokenToCollateralToken
. This setting dictates how the realized profit or loss (PnL) is handled when the position is closed. Since this is a short ETH position, the profit would naturally be realized in ETH (as the liability decreases). However, with SwapPnlTokenToCollateralToken
selected, the protocol will automatically swap the profit (ETH) into the user's collateral token. Assuming the collateral used was USDC, the ETH profit will be converted to USDC and added to the user's balance within the protocol upon execution of the Take Profit order. If NoSwap
(0) were selected, the profit would remain in ETH.
Analyzing this transaction highlights several important DeFi concepts:
Multicall: Enables bundling multiple actions into a single, atomic, and often gas-efficient transaction. Essential for complex operations like opening a position with bracket orders (TP/SL).
Order Types (OrderType
enum): Smart contracts use enums to define distinct behaviors and trigger logic for various conditional orders (Market, Limit, Stop Loss, etc.). Understanding the specific enum definition is key to interpreting order parameters.
Trigger Price & Decimals: Prices in smart contracts are typically stored as large integers representing fixed-point numbers. You must know the contract's or price feed's decimal precision to convert these values into human-readable prices.
Position Sizing Parameters: Fields like sizeDeltaUsd
control how much of a position an order affects. Setting them to the full position amount ensures a complete closure upon execution.
PnL Swap Logic (DecreasePositionSwapType
enum): Determines the final token denomination of realized profits or losses when closing a position, allowing automatic conversion to the collateral token if desired.
By understanding these parameters and structures, you can effectively analyze and debug complex DeFi transactions involving conditional orders, gaining deeper insight into how trading strategies are implemented on-chain.
A detailed guide to Debugging GMX Take Profit and Stop Loss Orders - Learn to dissect `multicall` transactions using tools like Tenderly to understand how GMX Take Profit and Stop Loss orders are created. Decode `OrderType` enums and verify trigger prices by analyzing `createOrder` parameters on-chain.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Cyfrin Updraft has partnered with GMX to provide SSCD+ certifications vouchers to the first 25 students who demonstrate the completion of this course and are building on top of GMX. Join the GMX Discord server to redeem your coupon, or navigate to the docs to learn more!
Course Overview
About the course
Mechanics and contract architecture of the GMX protocol
Token pricing and fees
Liquidity: GM pools and GLV vaults
Math, funding rates, liquidation pricing, P&L calculations
Limit orders, take profit orders, stop loss, and stop market orders
Auto-cancel and auto-deleveraging
GLP, esGMX, GMX staking and delegation
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Security researcher
$49,999 - $120,000 (avg. salary)
Last updated on June 26, 2025
Duration: 8min
Duration: 1h 19min
Duration: 1h 24min
Duration: 16min
Duration: 11min
Duration: 11min
Duration: 6min
Course Overview
About the course
Mechanics and contract architecture of the GMX protocol
Token pricing and fees
Liquidity: GM pools and GLV vaults
Math, funding rates, liquidation pricing, P&L calculations
Limit orders, take profit orders, stop loss, and stop market orders
Auto-cancel and auto-deleveraging
GLP, esGMX, GMX staking and delegation
DeFi Developer
$75,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Security researcher
$49,999 - $120,000 (avg. salary)
Last updated on June 26, 2025