0/5
## Introduction to GMX V2: Decentralized Perpetual Exchange Welcome to this exploration of GMX V2, a prominent decentralized perpetual exchange protocol operating within the DeFi ecosystem. This lesson provides an overview of GMX V2, outlines what you can expect to learn in this course, discusses the motivations for diving deep into this protocol, and details the necessary prerequisites for understanding the subsequent material. Our focus spans both the theoretical underpinnings of GMX V2 and the practical application of interacting with its smart contracts using Solidity and Foundry. GMX V2 functions as a **Decentralized Perpetual Exchange**. This platform allows users to engage in several core DeFi activities: * **Create Long and Short Positions:** Speculate on the future price direction of assets. * **Utilize Leverage:** Amplify potential trading gains (and losses) up to 100x. * **Swap Tokens:** Exchange assets directly at prevailing market prices. * **Submit Limit Orders:** Place orders to swap tokens only when a specific price target is met. Throughout this course, we will delve into the mechanics behind these features. You will learn not just *what* GMX V2 does, but *how* it achieves this functionality on-chain. ### What You Will Learn This course aims to provide a comprehensive understanding of GMX V2, covering: 1. **GMX V2 Protocol Mechanics:** We will dissect the architecture and core logic of the GMX V2 system, explaining how it facilitates perpetual swaps and other functionalities. 2. **Advanced DeFi Concepts (GMX V2 Context):** While you might be familiar with some DeFi terms, we will examine them specifically through the lens of GMX V2: * **Perpetual Swap:** The fundamental financial instrument offered by GMX V2. * **Leverage:** How GMX V2 enables and manages leveraged positions. * **Long and Short Positions:** The mechanics of establishing and maintaining directional bets. * **Funding Fee:** The crucial mechanism ensuring the perpetual contract price stays close to the underlying asset's index price, involving periodic payments between long and short positions. * *Note:* Our explanations will be tailored to how these concepts are implemented within GMX V2, which may differ slightly from traditional finance interpretations. 3. **Practical Smart Contract Interaction:** You will gain hands-on experience by writing Solidity smart contracts designed to interact directly with the GMX V2 protocol. This involves understanding its contract interfaces and composing function calls to execute trades or manage positions programmatically. We will utilize the Foundry development framework for testing these interactions. ### Why Learn About GMX V2? Understanding a sophisticated DeFi protocol like GMX V2 offers several advantages: * **Expand DeFi Knowledge:** Move beyond basic token swaps and lending/borrowing into the more complex world of decentralized derivatives and perpetual exchanges. * **Enhance Security Skills:** Knowledge of perpetual exchange mechanics is highly valuable for participating in smart contract audits, security reviews, and bug bounty programs targeting GMX V2 or similar protocols. Understanding the design intricacies helps identify potential vulnerabilities. * **Build Integrated Applications:** Developers seeking to build applications that leverage GMX V2's liquidity or trading capabilities (e.g., yield strategies, automated trading bots, portfolio managers) will benefit immensely from understanding its contract architecture and integration points. * **Foundation for Protocol Development:** For those interested in designing or building their own decentralized perpetual exchange, studying GMX V2's implementation provides valuable insights and serves as a practical case study. ### Prerequisites for Success To fully grasp the concepts and effectively participate in the practical exercises of this course, a solid foundation in several areas is essential: 1. **DeFi Fundamentals:** * Familiarity with common stablecoins and crypto assets: **DAI, USDC, WETH (Wrapped Ether), WBTC (Wrapped Bitcoin)**. * Understanding of **ERC20 token decimals** and their implications. * Knowledge of **Automated Market Maker (AMM)** concepts. * Understanding of **Price Oracles** and how protocols source external price data. * Awareness of Layer 2 scaling solutions, specifically **Arbitrum**, where GMX V2 primarily operates. * A general grasp of common DeFi concepts and vocabulary. 2. **Solidity Proficiency (Intermediate to Advanced):** * **Solidity `library`:** A strong understanding of how libraries work, their purpose, and their deployment characteristics. * **`delegatecall`:** This low-level call opcode is **critically important**. You must understand how `delegatecall` executes code from another contract (often a library) within the context of the *calling* contract, meaning it modifies the calling contract's storage. **Crucially, recognize that state-changing calls made *through* a Solidity `library` typically utilize `delegatecall`**. Ensure you are comfortable with its behavior and security implications *before* proceeding. * **`multicall` Pattern:** Familiarity with the technique of batching multiple function calls into a single transaction. GMX V2 utilizes this pattern extensively for efficiency and atomicity. 3. **Foundry Skills (Advanced):** * **Fork Testing:** Experience with setting up and running Foundry tests against a forked state of a live blockchain (e.g., Arbitrum mainnet). This is vital for testing interactions with deployed protocols like GMX V2. * **Debugging with `console.log`:** Proficiency in using Foundry's `console.log` functionality within Solidity tests to debug contract execution flow and inspect variable states. ### Key Technical Concepts: Multicall and Delegatecall in GMX V2 Understanding the interplay between the `multicall` pattern and `delegatecall` is fundamental to grasping how interactions with GMX V2 often occur. GMX V2 employs contracts like `PayableMulticall.sol` to allow users or other contracts to execute multiple actions atomically within a single transaction. Consider the `multicall` function often found in such contracts: ```solidity // Simplified example signature from PayableMulticall.sol function multicall(bytes[] calldata data) external payable virtual returns (bytes[] memory results); ``` The core logic of this function typically involves: 1. Accepting an array (`data`) where each element is the encoded function call data for a desired action. 2. Iterating through this array. 3. For each element `data[i]`, executing the encoded function call within the context of the `PayableMulticall` contract itself using `delegatecall`. A common implementation looks like: `(bool success, bytes memory result) = address(this).delegatecall(data[i]);` 4. Checking the `success` status of each `delegatecall` and reverting the entire transaction if any call fails. 5. Collecting and returning the results (`result`) from each successful call. This pattern leverages `delegatecall` to execute the batched operations. If these operations involve calls to functions defined in Solidity libraries linked to the `PayableMulticall` contract (or its inheriting contracts), those library functions will also execute via `delegatecall`, modifying the state of the main contract. This architectural choice makes understanding `delegatecall` absolutely essential for comprehending GMX V2's execution flow and potential security considerations.
An introductory overview to GMX V2 Course Introduction - Get started with the GMX V2 DeFi course, understanding what the protocol offers, course objectives, and key prerequisites. Learn about perpetual swaps, leverage, and the essential Solidity/Foundry skills needed.
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