5/5
# `GmxHelper` Exercises `GmxHelper` is a contract that interacts with GMX to manage position size and fees. You need to complete the implementation of the `GmxHelper.sol` contract. ## Task 1: Implement `getSizeDeltaUsd` ```solidity function getSizeDeltaUsd( // Long token price from Chainlink (1e8 = 1 USD) uint256 longTokenPrice, // Current position size uint256 sizeInUsd, // Current collateral amount locked in the position uint256 collateralAmount, // Long token amount to add or remove uint256 longTokenAmount, // True for market increase bool isIncrease ) internal view returns (uint256 sizeDeltaUsd) {} ``` This function calculates the change in position size needed so that the new position size is the current collateral price times the new collateral amount. Return `0` if `sizeDeltaUsd` is less than `0`. ## Task 2: Implement `createIncreaseShortPositionOrder` ```solidity function createIncreaseShortPositionOrder( // Execution fee to send to the order vault uint256 executionFee, // Long token amount to add to the current position uint256 longTokenAmount ) internal returns (bytes32 orderKey) {} ``` This function creates a market increase order for a short position. ### Task 2.1: Calculate the position size delta Call `getSizeDeltaUsd` to calculate the delta for the next position size. > Hint: > > - Get `sizeInUsd` and `collateralAmount` from the current `position` ### Task 2.2: Create market increase order Call `exchangeRouter` to create a market increase order for a short position. ## Task 3: Implement `createDecreaseShortPositionOrder` ```solidity function createDecreaseShortPositionOrder( // Execution fee to send to the order vault uint256 executionFee, // Long token amount to remove from the current position uint256 longTokenAmount, // Receiver of long token address receiver, // Callback contract used to handle withdrawal from the vault address callbackContract, // Max gas to send to the callback contract uint256 callbackGasLimit ) internal returns (bytes32 orderKey) {} ``` This function creates a market decrease order for a short position. ### Task 3.1: Calculate the position size delta Call `getSizeDeltaUsd` to calculate the delta for the next position size. ### Task 3.2: Create market decrease order Call `exchangeRouter` to create a market decrease order for a short position. > Hints: > > - Set `receiver`, `callbackContract` and `callbackGasLimit` from the inputs. > - Set `decreasePositionSwapType` to `Order.DecreasePositionSwapType.SwapPnlTokenToCollateralToken` ## Task 4: Implement `cancelOrder` ```solidity function cancelOrder(bytes32 orderKey) internal {} ``` This function cancels an order. > Hint: > > - Call `exchangeRouter.cancelOrder` ## Task 5: Implement `claimFundingFees` ```solidity function claimFundingFees() internal {} ``` This function claims funding fees. > Hint: > > - You only need to claim funding fees for the long token since this is the only collateral used by this contract.
GmxHelper
ExercisesGmxHelper
is a contract that interacts with GMX to manage position size and fees.
You need to complete the implementation of the GmxHelper.sol
contract.
getSizeDeltaUsd
This function calculates the change in position size needed so that the new position size is the current collateral price times the new collateral amount.
Return 0
if sizeDeltaUsd
is less than 0
.
createIncreaseShortPositionOrder
This function creates a market increase order for a short position.
Call getSizeDeltaUsd
to calculate the delta for the next position size.
Hint:
Get
sizeInUsd
andcollateralAmount
from the currentposition
Call exchangeRouter
to create a market increase order for a short position.
createDecreaseShortPositionOrder
This function creates a market decrease order for a short position.
Call getSizeDeltaUsd
to calculate the delta for the next position size.
Call exchangeRouter
to create a market decrease order for a short position.
Hints:
Set
receiver
,callbackContract
andcallbackGasLimit
from the inputs.Set
decreasePositionSwapType
toOrder.DecreasePositionSwapType.SwapPnlTokenToCollateralToken
cancelOrder
This function cancels an order.
Hint:
Call
exchangeRouter.cancelOrder
claimFundingFees
This function claims funding fees.
Hint:
You only need to claim funding fees for the long token since this is the only collateral used by this contract.
A practical exercise to `GmxHelper` Exercises - Implement Solidity functions within `GmxHelper` to manage GMX V2 short positions. Learn to calculate size deltas, create/cancel orders, and claim funding fees.
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