Security review · October 2024
Buyback Updates, Part 2 for GMX
Guardian's review of Buyback Updates, Part 2 for GMX, published October 2024. The report records 15 findings across 2 review rounds, including 3 critical and 1 high.
See how Guardian runs a perp DEX smart contract audit.
- Published
- Review window
- October 1 to 22, 2024
- Rounds
- Main Review, Remediation Review
- Language
- Solidity
- Chains
- Arbitrum, Avalanche
- Sector
- Perpetuals
- 3 Critical
- 1 High
- 1 Medium
- 10 Low
- 0 Informational
Scope
Findings 15
Main Review
13 findings · October 1 to 7, 2024-
C-01 Critical Expected Fee Calculation Skews Output Logical Error Resolved
Description
The buyback mechanism aims to calculate how many fee tokens must be provided to the user for the batch amount of buyback tokens based on current oracle prices:
uint256 expectedFeeTokenAmount = Precision.mulDiv(batchSize, feeTokenPrice, buybackTokenPrice);The issue is that
batchSizerepresents a buyback token amount, and should be multiplied with thebuybackTokenPriceto get the USD value of the batch, and then divide by thefeeTokenPriceto calculate the worth in fee tokens. However, the current logic multiplies byfeeTokenPriceand divides bybuybackTokenPrice, leading to drastically different outputs of the swap.Consider the following scenario: Batch size: 1 GMX feeToken WBTC: $100,000 buybackToken GMX: $10
The current logic would return
1 GMX * $100,000 / $10 = 10,000BTC to provide to the user for 1 GMX, draining the contract’s available fees. This scenario also does not account for the varying decimal precision in the prices for each token, which also drastically modifies the expected output.Recommendation
Modify the calculation to
uint256 expectedFeeTokenAmount = Precision.mulDiv(batchSize, buybackTokenPrice, feeTokenPrice); -
C-02 Critical WNT Fees Cannot Be Used For GMX Buyback Validation Resolved
Description
Function
claimFeesvalidates that the fee token batch size is zero, which prevents GMX and WNT from being the providedfeeToken. This will prevent accumulated WNT fees from being used to buyback GMX tokens in the FeeHandler, since the available fee amounts will not be incremented for either buyback token.** **Recommendation
Clearly document this behavior.
-
C-03 Critical
BUYBACK_MAX_PRICE_AGECannot Be Set DoS ResolvedDescription
BUYBACK_MAX_PRICE_AGEis not included in theallowedBaseKeysof the Config contract, which prevents it from being set, even by the keeper, as the_validateKeyfunction will revert. As a result,maxPriceAgewill be 0 in the_getMaxFeeTokenAmountfunction, causing all buybacks to revert unless they are executed at the exact same timestamp as the oracle update.Recommendation
Set
allowedBaseKeys[Keys.BUYBACK_MAX_PRICE_AGE]to true in the Config file. -
H-01 High V1 Fees Cannot Be Claimed DoS Resolved
Description
When withdrawing fees from V1,
FeeHandlercalls thewithdrawFeesfunction of thevaultGov, which is a timelock. However, this will always revert since thevaultGov.withdrawFeeshas anonlyAdminmodifier, and theFeeHandleris not admin of the timelock. Since there can be only one admin, otheronlyAdminfunctions likesetLiquidator,addExcludedToken,setInPrivateTransferModeetc. cannot be called if theFeeHandleris set as admin.Recommendation
Consider removing the V1 fee logic from the
claimFeesfunction. Instead, V1 fees should be regularly withdrawn and transferred to the FeeHandler by the V1 admin. Additionally, consider adding a separate admin function that updates the available fee amounts after each V1 fee transfer. -
M-01 Medium Lack of Interval Caps Validation Acknowledged
Description
Each buyback token has a particular batch amount to limit how much can be sold by the user at a particular time. However, nothing prevents a user from calling the
buybackfunction multiples times and swapping multiples batches within a small timeframe. This can be used to take advantage of any price discrepancies between the Chainlink oracles and market prices.Recommendation
Consider limiting how much the protocol can buy back within a specific time interval.
-
L-01 Low No Access Control on Claim Fees Access Control Acknowledged
Description
Currently any user can call function
claimFeesto withdraw fees from the GMX vault or claim accumulated GMXV2 fees. This can lead to a user continuously claiming fees while buybacks are active to prevent the protocol from claiming fees for itself when necessary.Recommendation
Consider added
onlyFeeKeeperaccess control. -
L-02 Low Output Amount Can Be Inaccurate Logical Error Resolved
Description
The current
getOutputAmountfunction does not validate thatmarketsare unique. Hence, if duplicates of a market were passed into the view function, theavailableFeeAmountwould be larger due to double-counting of available fees. Ultimately, the function would return an inaccurate output amount and this can affect the behavior of the UI.Recommendation
Consider checking for duplicates in the
marketsparameter in the frontend and document this behavior. -
L-03 Low SafeTransferFrom Should Be First Best Practices Acknowledged
Description
In function
buyback, thesafeTransferFromoccurs after the fee token is transferred. However it is a best practice to transfer funds in before performing other state updates.Recommendation
Consider performing
safeTransferFromright after the initial validations and before the fee token is transferred to the user. -
L-04 Low Discounted GMX Buys Not Supported Warning Acknowledged
Description
The existing buyback mechanism aims to apply a premium on GMX purchases:
Precision.applyFactor(expectedFeeTokenAmount, maxPriceImpactFactor + Precision.FLOAT_PRECISION);This will increase how many fee tokens the user may receive per buyback token. For example, if the price of GMX is $100 and batch size is 10, max price impact is 0.3%, and fees for AAVE accumulate, the max worth of AAVE that would be swapped would be $1003.However, it may be prudent to also have a mechanism to discount GMX purchases so that users cannot extract value when the oracle price has yet to be updated to reflect latest market prices, or even to future-proof the contract as buyback incentive mechanisms may change with future proposals.
Recommendation
Clearly document the premium behavior to users and consider adding the ability to discount the GMX buyback price such as
Precision.applyFactor(expectedFeeTokenAmount, Precision.FLOAT_PRECISION - maxDiscountFactor); -
L-05 Low
buybackUses Wrong Oracle Modifier Informational AcknowledgedDescription
buyback()allows any user to call it and does not use the typical keeper system seen throughout the GMX protocol. This allows a user to create an atomic swap. Although an atomic action is taking place, the modifierwithOraclePrices()is used. This allows users to pass in price data that has a larger deadline than if the modifierwithOraclePricesForAtomicAction()was used. AlthoughmaxPriceAgeis validated, if it is set to too large of a value, then this can open up users to perform swaps with prices that deviate from the current market price. Additionally, if it is intended to still use Chainlink Price Feed, then usingwithOraclePrices()may cause unexpected reverts for a provider that is expected to be a Chainlink Data Stream.Recommendation
If using
withOraclePrices()is intended, verify thatBUYBACK_MAX_PRICE_AGEis not set to too large of a value. Otherwise, usewithOraclePricesForAtomicAction()instead. -
L-06 Low Consider Adding Withdraw Function For Fee Tokens Informational Acknowledged
Description
Fee tokens are transferred to the
FeeHandlercontract, and users can purchase these fee tokens by providing buyback tokens. The keeper has the ability to withdraw buyback tokens but does not have the ability to withdraw fee tokens. If users do not utilize the buyback functionality, claimed fee tokens may have to remain in theFeeHandlercontract for a long period of time and cannot be used for other purposes.Recommendation
Consider adding a privileged withdraw function for fee tokens.
-
L-07 Low Memory Variable Declared In For Loop Gas Optimization Resolved
Description
getOutputAmount()declaresfeeAmounts, which is a memory variable, inside of a for loop. In Solidity, declaring a memory variable inside of a for loop stores a new variable to memory on each iteration. The first 22 words of memory are priced linearly, but the pricing is quadratic after that. This can lead to users having to pay substantially more in gas costs.Recommendation
Declare
feeAmountsoutside of the for loop, and reassign in each iteration. -
L-08 Low Save
WNTAs Immutable Gas Optimization AcknowledgedDescription
_incrementAvailableFeeAmounts()gets the wrapped native token from storage. Since the wrapped native token will remain the same for each chain, it is safe to store it as an immutable variable. This will cut down on gas costs, since it will avoid reading from storage.Recommendation
Store
WNTas an immutable variable in the constructor similar togmx.
Remediation Review
2 findings · October 22, 2024-
L-01 Low Fee Claims Can Continue When Buybacks Disabled Warning Acknowledged
Description
Function
claimFeescan still be called by arbitrary users even if the batch sizes have been set to zero, effectively disabling buybacks. In such a scenario, fees could accrue in the contract but not be withdrawn.Recommendation
Consider if some pause functionality may be necessary for fee claims.
-
L-02 Low Events Emitted With Zero Transfers Warning Acknowledged
Description
If zero tokens are claimed through
FeeUtils.claimFees, a"FeesClaimed"event is still emitted. This can be done by callingclaimFeesbefore fees have accumulated, or passing an invalid fee token address.Recommendation
Be aware and take this into consideration for offchain services.
No findings match.
More from GMX
All 60 reports-
Morpho Liquidation Module
3 findings 3 findings: 3 informational -
RewardRouterV3 & RewardRouterUtils
20 findings1 high 20 findings: 1 high, 8 low, 11 informational -
V2.2.1 Review
102 findings8 high 102 findings: 8 high, 37 medium, 49 low, 8 informational -
Incentives Contracts
17 findings 17 findings: 3 medium, 3 low, 11 informational
Put your code through the same review.
This review started with a conversation about scope. Tell us what you are building and we will plan yours with you.
