5/5
## Understanding Virtual Inventory in GMX Synthetics This lesson explores the concept of Virtual Inventory within the GMX Synthetics protocol. While the precise calculation of price impact differs slightly for token swaps versus opening/closing positions, both mechanisms leverage the idea of virtual inventory to achieve a common goal: preventing price manipulation within GMX markets. ## The Core Concept: What is Virtual Inventory? Virtual Inventory is a crucial mechanism employed by GMX Synthetics to safeguard against price manipulation when users swap tokens or manage positions. Its primary function is to ensure that the price impact incurred by a user's action isn't solely determined by the liquidity or open interest within the *specific market* they are interacting with. Instead, the protocol incorporates the *global state* of relevant assets or open interest across *all* GMX markets. By considering this broader context, Virtual Inventory makes it uneconomical for users to attempt to minimize their price impact fees by splitting large transactions (swaps or position openings/closings) across multiple similar markets. ## Virtual Inventory for Swaps When dealing with token swaps, the Virtual Inventory logic resides within the `SwapPricingUtils.sol` contract. **Definition:** For swaps, Virtual Inventory encompasses two distinct values tracked globally across the protocol: 1. The total amount of the designated "long token" (e.g., WETH, WBTC) held across all relevant GMX liquidity pools. 2. The total amount of the designated "short token" (e.g., USDC, USDT) held across all relevant GMX liquidity pools. **Code Insights:** Within `SwapPricingUtils.sol` (around lines 138-153), the code references these global amounts, often named similar to `virtualPoolAmountForLongToken` and `virtualPoolAmountForShortToken`. A conditional check, typically `if (hasVirtualInventory)`, indicates that if this feature is enabled for the market, the price impact might be calculated based on this global virtual inventory state rather than just the local pool's liquidity. **Preventing Manipulation Example:** Consider a user wanting to swap a substantial amount of USDC for WETH. GMX might offer several markets where WETH is the long token, such as WETH/USDC and WETH/USDT. * **Without Virtual Inventory:** The user could divide their large USDC swap into smaller swaps across both the WETH/USDC and WETH/USDT markets. Each smaller swap would face a lower price impact within its respective market compared to executing the entire swap in a single market. * **With Virtual Inventory:** The price impact calculation considers the *total* global amount of WETH and the relevant short tokens (USDC, USDT) across *all* GMX markets. The system effectively calculates two potential price impacts: one based on the specific market's liquidity and another based on the global virtual inventory. The user ultimately pays a price impact fee corresponding to the *larger* of these two calculated values. This significantly disincentivizes splitting the trade, as the global state will reflect the true size of the intended swap. ## Virtual Inventory for Positions For opening and closing long or short positions, the Virtual Inventory logic is handled in the `PositionPricingUtils.sol` contract. **Definition:** In the context of positions, Virtual Inventory represents the *net global open interest* across all GMX markets for a specific index token (e.g., ETH, BTC). This is typically represented as a single signed integer (`int256 virtualInventory`): * `virtualInventory > 0`: Indicates a net *short* open interest globally. The value represents the magnitude of this net short imbalance (more short positions than long positions overall). * `virtualInventory < 0`: Indicates a net *long* open interest globally. The *absolute value* (`-virtualInventory`) represents the magnitude of this net long imbalance (more long positions than short positions overall). * `virtualInventory == 0`: Indicates that global long and short open interest for the index are perfectly balanced. **Code Insights:** * The function `getPriceImpactUsd` within `PositionPricingUtils.sol` (around line 159) is responsible for calculating the price impact for position actions. * This function typically retrieves the global net open interest by calling a utility function like `MarketUtils.getVirtualInventoryForPositions`, obtaining the `int256 virtualInventory` value (around line 174). * Similar to swaps, a check like `if (hasVirtualInventory)` (around line 176) determines if the virtual inventory should influence the final price impact calculation. * The interpretation of the `int256 virtualInventory` value is often clarified in helper functions like `getNextOpenInterestForVirtualInventory` (around line 243). This function translates the signed integer into unsigned long and short open interest figures based on its sign: ```solidity // Simplified logic from getNextOpenInterestForVirtualInventory function interpretVirtualInventory(int256 virtualInventory) internal pure returns (uint256 longOI, uint256 shortOI) { if (virtualInventory > 0) { // Positive value means net short OI globally shortOI = virtualInventory.toUint256(); longOI = 0; } else { // Negative or zero value means net long OI or balanced globally // Use absolute value for long OI magnitude longOI = (-virtualInventory).toUint256(); shortOI = 0; } // Further calculations adjust these based on the user's position delta... } ``` **Preventing Manipulation Example:** Imagine GMX has two markets tracking the ETH index (e.g., ETH/USD perpetuals, maybe another ETH-based market). A user wishes to open a very large long ETH position. * **Without Virtual Inventory:** The user could split their large long position across the two separate ETH markets. By doing so, they would face a smaller price impact in each individual market compared to opening the full position size in one market. * **With Virtual Inventory:** The system checks the *global* net open interest for ETH across all markets, represented by `virtualInventory`. The price impact calculation incorporates this global imbalance. If there's already a significant net long open interest globally (`virtualInventory < 0`), attempting to open another large long position will incur a substantial price impact based on this global state, irrespective of how the user splits the position between markets. As with swaps, the final price impact fee is the *maximum* of the impact calculated from the specific market's open interest and the impact calculated using the global virtual inventory. ## Key Takeaways * Virtual Inventory operates on a **global scale**, considering the state across all relevant GMX markets, not just the one being interacted with. * For **swaps**, it tracks the total global amounts of the specific long and short tokens involved. * For **positions**, it tracks the net global open interest for an index token, represented as a signed integer (`int256`). * The price impact fee paid by the user is ultimately determined by the **maximum** of two calculations: one based on the **local market's state** (liquidity or open interest) and one based on the **global virtual inventory state**. * This mechanism effectively **deters price manipulation** and prevents users from unfairly avoiding fees by fragmenting large swaps or positions across multiple markets.
This lesson explores the concept of Virtual Inventory within the GMX Synthetics protocol. While the precise calculation of price impact differs slightly for token swaps versus opening/closing positions, both mechanisms leverage the idea of virtual inventory to achieve a common goal: preventing price manipulation within GMX markets.
Virtual Inventory is a crucial mechanism employed by GMX Synthetics to safeguard against price manipulation when users swap tokens or manage positions. Its primary function is to ensure that the price impact incurred by a user's action isn't solely determined by the liquidity or open interest within the specific market they are interacting with.
Instead, the protocol incorporates the global state of relevant assets or open interest across all GMX markets. By considering this broader context, Virtual Inventory makes it uneconomical for users to attempt to minimize their price impact fees by splitting large transactions (swaps or position openings/closings) across multiple similar markets.
When dealing with token swaps, the Virtual Inventory logic resides within the SwapPricingUtils.sol
contract.
Definition: For swaps, Virtual Inventory encompasses two distinct values tracked globally across the protocol:
The total amount of the designated "long token" (e.g., WETH, WBTC) held across all relevant GMX liquidity pools.
The total amount of the designated "short token" (e.g., USDC, USDT) held across all relevant GMX liquidity pools.
Code Insights: Within SwapPricingUtils.sol
(around lines 138-153), the code references these global amounts, often named similar to virtualPoolAmountForLongToken
and virtualPoolAmountForShortToken
. A conditional check, typically if (hasVirtualInventory)
, indicates that if this feature is enabled for the market, the price impact might be calculated based on this global virtual inventory state rather than just the local pool's liquidity.
Preventing Manipulation Example:
Consider a user wanting to swap a substantial amount of USDC for WETH. GMX might offer several markets where WETH is the long token, such as WETH/USDC and WETH/USDT.
Without Virtual Inventory: The user could divide their large USDC swap into smaller swaps across both the WETH/USDC and WETH/USDT markets. Each smaller swap would face a lower price impact within its respective market compared to executing the entire swap in a single market.
With Virtual Inventory: The price impact calculation considers the total global amount of WETH and the relevant short tokens (USDC, USDT) across all GMX markets. The system effectively calculates two potential price impacts: one based on the specific market's liquidity and another based on the global virtual inventory. The user ultimately pays a price impact fee corresponding to the larger of these two calculated values. This significantly disincentivizes splitting the trade, as the global state will reflect the true size of the intended swap.
For opening and closing long or short positions, the Virtual Inventory logic is handled in the PositionPricingUtils.sol
contract.
Definition: In the context of positions, Virtual Inventory represents the net global open interest across all GMX markets for a specific index token (e.g., ETH, BTC). This is typically represented as a single signed integer (int256 virtualInventory
):
virtualInventory > 0
: Indicates a net short open interest globally. The value represents the magnitude of this net short imbalance (more short positions than long positions overall).
virtualInventory < 0
: Indicates a net long open interest globally. The absolute value (-virtualInventory
) represents the magnitude of this net long imbalance (more long positions than short positions overall).
virtualInventory == 0
: Indicates that global long and short open interest for the index are perfectly balanced.
Code Insights:
The function getPriceImpactUsd
within PositionPricingUtils.sol
(around line 159) is responsible for calculating the price impact for position actions.
This function typically retrieves the global net open interest by calling a utility function like MarketUtils.getVirtualInventoryForPositions
, obtaining the int256 virtualInventory
value (around line 174).
Similar to swaps, a check like if (hasVirtualInventory)
(around line 176) determines if the virtual inventory should influence the final price impact calculation.
The interpretation of the int256 virtualInventory
value is often clarified in helper functions like getNextOpenInterestForVirtualInventory
(around line 243). This function translates the signed integer into unsigned long and short open interest figures based on its sign:
Preventing Manipulation Example:
Imagine GMX has two markets tracking the ETH index (e.g., ETH/USD perpetuals, maybe another ETH-based market). A user wishes to open a very large long ETH position.
Without Virtual Inventory: The user could split their large long position across the two separate ETH markets. By doing so, they would face a smaller price impact in each individual market compared to opening the full position size in one market.
With Virtual Inventory: The system checks the global net open interest for ETH across all markets, represented by virtualInventory
. The price impact calculation incorporates this global imbalance. If there's already a significant net long open interest globally (virtualInventory < 0
), attempting to open another large long position will incur a substantial price impact based on this global state, irrespective of how the user splits the position between markets. As with swaps, the final price impact fee is the maximum of the impact calculated from the specific market's open interest and the impact calculated using the global virtual inventory.
Virtual Inventory operates on a global scale, considering the state across all relevant GMX markets, not just the one being interacted with.
For swaps, it tracks the total global amounts of the specific long and short tokens involved.
For positions, it tracks the net global open interest for an index token, represented as a signed integer (int256
).
The price impact fee paid by the user is ultimately determined by the maximum of two calculations: one based on the local market's state (liquidity or open interest) and one based on the global virtual inventory state.
This mechanism effectively deters price manipulation and prevents users from unfairly avoiding fees by fragmenting large swaps or positions across multiple markets.
A technical explanation to Virtual Inventory in GMX Synthetics - Understand how GMX prevents price manipulation using virtual inventory for both swaps and perpetual positions. Learn how global token amounts and net open interest across all markets influence price impact calculations.
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