buyOrder
function allows users to purchase ERC20 tokens from a sell order by sending Ether equivalent to the order's price. However, the function only checks if msg.value
(the Ether sent) is less than the order price and reverts in that case. It does not handle scenarios where the buyer overpays (sends more Ether than required). The excess Ether remains in the contract, leading to a potential loss for the buyer since there is no mechanism to refund the overpayment.The buyOrder
function found in line 270 validates the Ether sent by the buyer (msg.value
) to ensure it is at least equal to the price of the order being purchased:
if (msg.value < order.price) {
revert TokenDivider__IncorrectEtherAmount();
}
However, the function does not validate against overpayment (i.e., msg.value > order.price
). If the buyer sends excess Ether, the surplus is retained in the contract without any mechanism to refund the overpayment.
If a buyer accidentally sends more Ether (msg.value > order.price
), the excess Ether remains locked in the contract.
The buyer cannot reclaim the surplus without an explicit refund mechanism, leading to potential financial loss.
Overpaid Ether accumulates in the contract. Unless an admin or withdrawal mechanism exists, these funds may become permanently inaccessible.
This creates inefficiencies and risks for the platform's financial management.
Users expect fair handling of their funds. If overpayments are retained without clear communication or a refund, users may view the platform as untrustworthy or careless.
Retaining overpaid funds can lead to negative publicity, especially if the issue affects many users. This might discourage potential users from engaging with the platform.
In some jurisdictions, withholding overpaid funds without a refund mechanism might violate regulations or contractual obligations, exposing the contract owner to potential legal actions.
Manuel Review
To ensure proper handling of payments and prevent unintended overpayment, the following changes are recommended:
Update the buyOrder
Function to Validate Exact Payment: Replace the current conditional check for payment with a stricter validation. Modify to this:
if(msg.value != order.price) {
revert TokenDivider__IncorrectEtherAmount();
}
Implement Overpayment Refund Logic (Optional): If allowing overpayments is intentional (e.g., for user convenience), include logic to refund any excess Ether.
if(msg.value > order.price) {
uint256 overpayment = msg.value - order.price;
(bool refundSuccess, ) = payable(msg.sender).call{value: overpayment}("");
if (!refundSuccess) {
revert TokenDivider__TransferFailed();
}
}
By enforcing strict validation or handling overpayments explicitly, the vulnerability is resolved, and the contract aligns with user expectations and best practices.
The extra eth sent by the user in the buy order will be locked in the contract forever
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.