`View::queryAssetBalances` does not account for JIT penalties
The view function queryAssetBalances returns the asset balances for a maker position without applying the JIT penalty that is applied when the position is actually removed. An attacker could rely on the incorrect value, leading to a mismatch between the displayed withdrawal amount and the actual amount received.
Source: https://github.com/sherlock-audit/2025-09-ammplify-judging/issues/349
Found by
tedox
Summary
View::queryAssetBalances is used to query how much assets an user would receive if they were to withdraw their funds at the moment. However, the value returned will be wrong as it does not account for potential JIT penalties that the position might incur.
Root Cause
When a user removes their maker position it calculates the amount of assets to return and then applies a JIT penalty if needed afterwards.
/// @inheritdoc IMaker
function removeMaker(
address recipient,
uint256 assetId,
uint160 minSqrtPriceX96,
uint160 maxSqrtPriceX96,
bytes calldata rftData
) external nonReentrant returns (address token0, address token1, uint256 removedX, uint256 removedY) {
Asset storage asset = AssetLib.getAsset(assetId);
require(asset.owner == msg.sender, NotMakerOwner(asset.owner, msg.sender));
require(asset.liqType == LiqType.MAKER || asset.liqType == LiqType.MAKER_NC, NotMaker(assetId));
PoolInfo memory pInfo = PoolLib.getPoolInfo(asset.poolAddr);
Data memory data = DataImpl.make(pInfo, asset, minSqrtPriceX96, maxSqrtPriceX96, 0);
WalkerLib.modify(pInfo, asset.lowTick, asset.highTick, data);
// Settle balances.
PoolWalker.settle(pInfo, asset.lowTick, asset.highTick, data);
removedX = uint256(-data.xBalance); // These are definitely negative.
removedY = uint256(-data.yBalance);
-> (removedX, removedY) = FeeLib.applyJITPenalties(asset, removedX, removedY); //@audit JIT penalty applied to the full amount returned
AssetLib.removeAsset(assetId);
address[] memory tokens = pInfo.tokens();
int256[] memory balances = new int256[](2);
balances[0] = -int256(removedX); // We know they fit since they can only be less (in magnitude) than before.
balances[1] = -int256(removedY);
RFTLib.settle(recipient, tokens, balances, rftData);
// Return values
token0 = tokens[0];
token1 = tokens[1];
}
However, when the view function is used it does not properly account for the JIT penalty.
/// Compute the token balances owned/owed by the position.
/// @dev We separate the fee and liq balance so we can use the same method for fee earnings and total value.
/// @return netBalance0 The amount of token0 owed to the position owner sans fees (Negative is owed by the owner).
/// @return netBalance1 The amount of token1 owed to the position owner sans fees (Negative is owed by the owner).
/// @return fees0 The amount of fees in token0 owed to a maker or owed by a taker depending on the liq type.
/// @return fees1 The amount of fees in token1 owed to a maker or owed by a taker depending on the liq type.
function queryAssetBalances(
uint256 assetId
) external view returns (int256 netBalance0, int256 netBalance1, uint256 fees0, uint256 fees1) {
Asset storage asset = AssetLib.getAsset(assetId);
PoolInfo memory pInfo = PoolLib.getPoolInfo(asset.poolAddr);
ViewData memory data = ViewDataImpl.make(pInfo, asset);
ViewWalkerLib.viewAsset(pInfo, asset.lowTick, asset.highTick, data);
if (asset.liqType == LiqType.TAKER) {
uint256 vaultX = VaultLib.balanceOf(pInfo.token0, asset.xVaultIndex, assetId, false);
uint256 vaultY = VaultLib.balanceOf(pInfo.token1, asset.yVaultIndex, assetId, false);
// Balance and fees are owed, and vault balance is owned.
netBalance0 = int256(vaultX) - int256(data.liqBalanceX);
netBalance1 = int256(vaultY) - int256(data.liqBalanceY);
fees0 = data.earningsX;
fees1 = data.earningsY;
} else { //@audit missing JIT penalty
netBalance0 = int256(data.liqBalanceX);
netBalance1 = int256(data.liqBalanceY);
fees0 = data.earningsX;
fees1 = data.earningsY;
}
}
This would cause a mismatch between the 2 values of more than 0.01% as long as the JIT penalty is more than 0.01% (which is likely the case)
Internal Pre-conditions
- The position is within the JIT penalty time
External Pre-conditions
Attack Path
- User creates a maker position
- Frontend queries
View::queryAssetBalancesin order to show the user how much they would be able to withdraw - User calls
removeMakerthrough the frontend but receives less funds due to the JIT penalty
Impact
Medium impact:
Issues that lead to getting incorrect return values (i.e. deviates from the withdrawal value of the asset by more than 0.01%) from the queryAssetBalance function (even if the appropriate input is used), which will lead to issues when executing other functions, may be considered valid with Medium severity at max.
PoC
No response
Mitigation
Apply the JIT penalty to the view function for more realistic results
Discussion
sherlock-admin2
The protocol team fixed this issue in the following PRs/commits: https://github.com/itos-finance/Ammplify/pull/26
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
How it worked
Root cause
The view function queryAssetBalances does not call the JIT penalty calculation that is applied in the actual removal function removeMaker.
Impact
The view function returns an incorrect withdrawal value that deviates by more than 0.01% when a JIT penalty applies, which could lead to issues when executing other functions.
What the attacker needed
The position is within the JIT penalty time
The fix
The protocol team fixed this issue by applying the JIT penalty to the view function.
How it was found
- User creates a maker position
- Frontend queries View::queryAssetBalances to show the user how much they would be able to withdraw
- User calls removeMaker through the frontend but receives less funds due to the JIT penalty
Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.
Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.
A smart contract has a function that tells users how much they would get if they withdrew their funds. But when they actually withdraw, a penalty is subtracted. The view function forgets to subtract that penalty, so it shows a higher amount than they really get.
What is Business logic?
The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.