Root + Impact
Description
## Summary
The `amendSellOrder` function lacks a check to verify that the seller has sufficient tokens
before attempting transfers. This leads to failed transactions, gas loss, and poor user experience
when sellers try to increase order amounts without having enough tokens.
## Vulnerability Details
The `amendSellOrder` function attempts to transfer additional tokens without checking if the seller
has sufficient balance:
function amendSellOrder(
uint256 _orderId,
uint256 _newAmountToSell,
uint256 _newPriceInUSDC,
uint256 _newDeadlineDuration
) public {
if (_newAmountToSell > order.amountToSell) {
uint256 diff = _newAmountToSell - order.amountToSell;
token.safeTransferFrom(msg.sender, address(this), diff);
}
}
--Issues:
- No balance check before `safeTransferFrom`
- Transaction will revert if seller lacks sufficient tokens
- Gas wasted on failed transactions
- Poor user experience with unclear error messages
- Potential for race conditions if balance changes
## Impact
- Failed transactions: Orders cannot be amended when seller lacks tokens
- Gas loss: Users lose gas on failed transactions
- Poor UX: No clear error message about insufficient balance
- Race conditions: Balance could change between check and transfer
- User frustration: Unclear why amendment failed
## Affected Functions
- `amendSellOrder()` - Lines 137-175 (missing balance check)
Risk
Likelihood:
Impact:
Proof of Concept
# Proof of Concept
1. User creates order for 100 tokens**
2. User spends all their tokens elsewhere**
3. User tries to amend order to 150 tokens**
4. Transaction fails because user has 0 tokens
5. User loses gas and gets unclear error message
## The Problem
No check for sufficient balance before attempting transfer:
function amendSellOrder(uint256 _orderId, uint256 _newAmountToSell, ...) public {
if (_newAmountToSell > order.amountToSell) {
uint256 diff = _newAmountToSell - order.amountToSell;
token.safeTransferFrom(msg.sender, address(this), diff);
}
}
## What Happens
- User has 0 tokens*but tries to increase order by 50 tokens
- safeTransferFrom reverts with generic error
- Transaction fails and user loses gas
- No clear error message about insufficient balance
- User doesn't know why amendment failed
Recommended Mitigation
# Mitigation
## What's Wrong
The `amendSellOrder` function attempts to transfer tokens without checking
if the seller has sufficient balance, leading to failed transactions and poor user experience.
**Vulnerable Code:**
// Lines 137-175: amendSellOrder()
if (_newAmountToSell > order.amountToSell) {
uint256 diff = _newAmountToSell - order.amountToSell;
// No balance check before transfer
token.safeTransferFrom(msg.sender, address(this), diff);
}
--The Problem:
Transactions fail with unclear error messages when sellers lack sufficient
tokens, wasting gas and frustrating users.
## How to Fix
Add a balance check before attempting the transfer:
if (_newAmountToSell > order.amountToSell) {
uint256 diff = _newAmountToSell - order.amountToSell;
// Check if seller has sufficient tokens
if (token.balanceOf(msg.sender) < diff) revert InsufficientBalance();
token.safeTransferFrom(msg.sender, address(this), diff);
}
--The Fix:
Now users get a clear error message before wasting gas on failed transactions.
## Alternative: Add Custom Error
error InsufficientBalance(uint256 required, uint256 available);
// In function:
if (token.balanceOf(msg.sender) < diff) {
revert InsufficientBalance(diff, token.balanceOf(msg.sender));
}
--Recommendation:
Add the balance check to improve user experience and prevent gas waste.