5/5
# `Vault` Exercises `Vault` is a contract that users deposit and withdraw WETH from. Authorized accounts will be able to transfer WETH in this contract into the strategy by calling `transfer`. You need to complete the implementation of the `Vault.sol` contract. ## Task 1: Implement `totalValueInToken` ```solidity function totalValueInToken() public view returns (uint256) {} ``` Return the amount of WETH in this contract plus the amount of WETH managed by the strategy. > Hint: > > - `strategy.totalValueInToken()` returns the total amount of WETH managed by the strategy. ## Task 2: Implement `deposit` ```solidity function deposit(uint256 wethAmount) external guard returns (uint256 shares) {} ``` This function will deposit WETH from `msg.sender` and mint shares. - Call the function `strategy.claim` to claim funding fees - Calculate the shares to mint and then transfer `WETH` from `msg.sender`. > Hints > > - Call `_convertToShares` to calculate the amount of shares to mint ## Task 3: Implement `withdraw` ```solidity function withdraw(uint256 shares) external payable guard returns (uint256 wethSent, bytes32 withdrawOrderKey) {} ``` This function will withdraw WETH to `msg.sender`. If not enough WETH can be withdrawn this function will create a market decrease order. - Call the function `strategy.claim` to claim funding fees - Calculate the amount of WETH to withdraw by calling `_convertToWeth` - If there are enough WETH in vault or strategy - Send the appropriate amount of WETH to `msg.sender` - Burn `shares` - Refund ETH if `msg.value` is greater than 0. - If there are not enough WETH in vault and strategy - Withdraw all of WETH from the vault and the strategy to `msg.sender` - Calculate and burn the appropriate amount of shares - Lock the rest of the shares by calling `_lock` - Make sure that `withdrawCallback` is a contract. - Call `strategy.decrease` to create a withdraw order. Send `msg.value` as execution fee. - Store `WithdrawOrder` ## Task 4: Implement `cancelWithdrawOrder` ```solidity function cancelWithdrawOrder(bytes32 key) external guard {} ``` This function allows users to cancel their withdraw order. - Check that only the user that created the withdraw order can cancel their own order. - Make sure that `withdrawCallback` is a contract. - Call `strategy.cancel` to cancel the order. ## Task 5: Implement `removeWithdrawOrder` ```solidity function removeWithdrawOrder(bytes32 key, bool ok) external auth {} ``` This function is called by `WithdrawCallback` to remove the withdraw order in 2 situations. 1. Withdraw order executed successfully. `ok` will be `true`. 2. There was an error executing the order or the order was cancelled. `ok` will be `false`. - Get the withdraw order stored in the mapping `withdrawOrders`. - Unlock the shares of the user who created the withdraw order. - If `ok` is `true`, burn the remaining shares. - Delete the withdraw order. ## Test ```shell forge test --fork-url $FORK_URL --fork-block-number $FORK_BLOCK_NUM --match-path test/app/Vault.test.sol -vvv ``` ## Integration test Test all contracts (`Vault`, `Strategy` and `WithdrawCallback`). ```shell forge test --fork-url $FORK_URL --fork-block-number $FORK_BLOCK_NUM --match-path test/app/VaultAndStrategy.test.sol -vvv ```
Vault
ExercisesVault
is a contract that users deposit and withdraw WETH from.
Authorized accounts will be able to transfer WETH in this contract into the strategy by calling transfer
.
You need to complete the implementation of the Vault.sol
contract.
totalValueInToken
Return the amount of WETH in this contract plus the amount of WETH managed by the strategy.
Hint:
strategy.totalValueInToken()
returns the total amount of WETH managed by the strategy.
deposit
This function will deposit WETH from msg.sender
and mint shares.
Call the function strategy.claim
to claim funding fees
Calculate the shares to mint and then transfer WETH
from msg.sender
.
Hints
Call
_convertToShares
to calculate the amount of shares to mint
withdraw
This function will withdraw WETH to msg.sender
. If not enough WETH can be withdrawn this function will create a market decrease order.
Call the function strategy.claim
to claim funding fees
Calculate the amount of WETH to withdraw by calling _convertToWeth
If there are enough WETH in vault or strategy
Send the appropriate amount of WETH to msg.sender
Burn shares
Refund ETH if msg.value
is greater than 0.
If there are not enough WETH in vault and strategy
Withdraw all of WETH from the vault and the strategy to msg.sender
Calculate and burn the appropriate amount of shares
Lock the rest of the shares by calling _lock
Make sure that withdrawCallback
is a contract.
Call strategy.decrease
to create a withdraw order. Send msg.value
as execution fee.
Store WithdrawOrder
cancelWithdrawOrder
This function allows users to cancel their withdraw order.
Check that only the user that created the withdraw order can cancel their own order.
Make sure that withdrawCallback
is a contract.
Call strategy.cancel
to cancel the order.
removeWithdrawOrder
This function is called by WithdrawCallback
to remove the withdraw order in 2 situations.
Withdraw order executed successfully. ok
will be true
.
There was an error executing the order or the order was cancelled. ok
will be false
.
Get the withdraw order stored in the mapping withdrawOrders
.
Unlock the shares of the user who created the withdraw order.
If ok
is true
, burn the remaining shares.
Delete the withdraw order.
Test all contracts (Vault
, Strategy
and WithdrawCallback
).
A practical exercise to Vault Exercises - Implement Solidity functions for WETH deposit, withdrawal, and value calculation in a Vault contract. Learn to manage user shares, interact with a strategy contract, and handle withdrawal order logic.
Previous lesson
Previous
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