# `Strategy` Exercises `Strategy` is a contract that handles authorization and calls internal functions of `GmxHelper` to interact with GMX. You need to complete the implementation of the `Strategy.sol` contract. ## Task 1: Implement `totalValueInToken` ```solidity function totalValueInToken() external view returns (uint256) {} ``` This function will calculate the total value of `WETH` managed by this contract. Return the sum of `WETH` in this contract, collateral in GMX and profit and loss of the position managed by this contract. > Hint: > > - Call the internal function `getPositionWithPnlInToken` to get the amount of collateral plus the profit and loss of the current short position. ## Task 2: Implement `increase` ```solidity function increase(uint256 wethAmount) external payable auth returns (bytes32 orderKey) {} ``` This function will create a market increase order, adding `wethAmount` as collateral. > Hint: > > - Call an internal function in `GmxHelper` ## Task 3: Implement `decrease` ```solidity function decrease(uint256 wethAmount, address callbackContract) external payable auth returns (bytes32 orderKey) {} ``` This function will create a market decrease order. - When `callbackContract` is `address(0)`, create a market decrease order with the specified inputs. When this function is called from the `Vault`, `callbackContract` will not be `address(0)`. - Set `callbackGasLimit` to max callback gas limit by calling `getMaxCallbackGasLimit`. - Check `msg.value` is greater than the max callback gas limit. - Calculate `longTokenAmount` to withdraw from the current position. ``` long token amount = position collateral amount * wethAmount / position with pnl in token ``` Here `wethAmount` is the amount calculated by the `Vault` base on the profit and loss of this strategy. Amount of collateral to remove from the position is proportional to `wethAmount / position with pnl in token` - Create a market decrease order. > Hints: > > - Get position collateral amount by calling `getPositionCollateralAmount` > - Get position collateral amount with profit and loss by calling `getPositionWithPnlInToken` ## Task 4: Implement `cancel` ```solidity function cancel(bytes32 orderKey) external payable auth {} ``` This function will cancel an order. > Hint: > > - Call an internal function in `GmxHelper` ## Task 5: Implement `claim` ```solidity function claim() external {} ``` This function will claim funding fees. > Hint: > > - Call an internal function in `GmxHelper` ## Test ```shell forge test --fork-url $FORK_URL --fork-block-number $FORK_BLOCK_NUM --match-path test/app/Strategy.test.sol -vvv ```
Strategy ExercisesStrategy is a contract that handles authorization and calls internal functions of GmxHelper to interact with GMX.
You need to complete the implementation of the Strategy.sol contract.
totalValueInTokenThis function will calculate the total value of WETH managed by this contract.
Return the sum of WETH in this contract, collateral in GMX and profit and loss of the position managed by this contract.
Hint:
Call the internal function
getPositionWithPnlInTokento get the amount of collateral plus the profit and loss of the current short position.
increaseThis function will create a market increase order, adding wethAmount as collateral.
Hint:
Call an internal function in
GmxHelper
decreaseThis function will create a market decrease order.
When callbackContract is address(0), create a market decrease order with the specified inputs.
When this function is called from the Vault, callbackContract will not be address(0).
Set callbackGasLimit to max callback gas limit by calling getMaxCallbackGasLimit.
Check msg.value is greater than the max callback gas limit.
Calculate longTokenAmount to withdraw from the current position.
Here wethAmount is the amount calculated by the Vault base on the profit and loss of this strategy.
Amount of collateral to remove from the position is proportional to wethAmount / position with pnl in token
Create a market decrease order.
Hints:
Get position collateral amount by calling
getPositionCollateralAmountGet position collateral amount with profit and loss by calling
getPositionWithPnlInToken
cancelThis function will cancel an order.
Hint:
Call an internal function in
GmxHelper
claimThis function will claim funding fees.
Hint:
Call an internal function in
GmxHelper
A practical exercise to `Strategy` Exercises - Implement Solidity functions within a `Strategy` contract to handle GMX interactions via `GmxHelper`. Learn to calculate total value, increase/decrease positions, cancel orders, and claim funding fees.
Previous lesson
Previous
Next lesson
Next
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 January 28, 2026
Duration: 9min
Duration: 1h 19min
Duration: 1h 25min
Duration: 16min
Duration: 11min
Duration: 12min
Duration: 7min
Duration: 2min
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 January 28, 2026