Skip to content
Business logic Audit finding Intermediate

Logic error in fee subtraction

The fee calculation in LibBalances.applyTrade() incorrectly adds the fee to the long position instead of subtracting it from both positions. This causes the contract to believe it can withdraw collected fees, leaving users with an incorrect amount of quote tokens.

0xsanson Follow

Originally disclosed on Code4rena

Submitted by 0xsanson

In LibBalances.applyTrade(), we need to collect a fee from the trade. However, the current code subtracts a fee from the short position and adds it to the long. The correct implementation is to subtract a fee to both (see TracerPerpetualSwaps.sol L272). This issue causes withdrawals problems, since Tracer thinks it can withdraw the collect fees, leaving the users with an incorrect amount of quote tokens.

Recommend changing +fee to -fee in the highlighted line.

raymogg (Tracer) confirmed:

Valid issue 👍

The original report as published on Code4rena, by 0xsanson. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

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

Likely a sign error in the fee adjustment: the code used addition instead of subtraction for the long position's fee.

Impact

The contract may allow withdrawals of fees that were never actually collected, reducing users' quote token balances incorrectly.

What the attacker needed

None

The fix

The fee should be subtracted from both the long and short positions, as done in TracerPerpetualSwaps.sol.

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.

In a trading system, fees should be taken from both the buyer and the seller. Here, the code accidentally gave the fee to one side instead of taking it from both, so the system thought it had extra money to withdraw, which would leave users short.

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.

More by 0xsanson

View all

8 more disclosed reports, best first.

More from Tracer contest

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.