Skip to content
Business logic Audit finding Intermediate

Spot dex cant handle fee-on-transfer tokens

The spot DEX contracts assume that the full amount of tokens sent by a user will be received, which is incorrect for fee-on-transfer tokens. This could cause swaps to fail midway and lock funds.

0x4non Follow with 0xStalin, kutugu and 1 other

Originally disclosed on Sherlock

Source: https://github.com/sherlock-audit/2023-06-unstoppable-judging/issues/40

Found by

0x4non, 0xStalin, kutugu, twicek

Summary

Dca.vy, LimitOrder.vy and TrailingStopDex.vy does not properly support tokens that implement a fee on transfer. These types of tokens deduct a fee from any transfer of tokens, resulting in the recipient receiving less than the original amount sent.

Vulnerability Detail

The specific issue lies in the ERC20 token transfer.

Lests analyze the method execute_limit_order focus on lines LimitOrders.vy#L160-L161, this code assumes that the full order.amount_in will be transferred to self. This assumption is incorrect when the token in question implements a fee on transfer. When a fee is deducted, self will receive less tokens than order.amount_in. The subsequent call to approve and ExactInputSingleParams could therefore potentially fail as they rely on self having a balance of order.amount_in.

Impact

This vulnerability could potentially halt token swaps midway if the token involved deducts a transfer fee. This can result in an unsuccessful token swap, which in turn could lock funds and possibly lead to financial loss for the user.

Code Snippet

Tool used

Manual Review

Recommendation

To rectify this vulnerability, it's recommended to replace order.amount_in with the account of balance of ERC20(order.token_in) after the swap minus before the transferFrom call. This ensures that the correct balance (after any potential fees) is used for the approve call and the ExactInputSingleParams. It's also advised to add error handling for unsuccessful transfers and approvals.

Example:

VY
    balance_before: uint256 = ERC20(order.token_in).balanceOf(self)
    
    # transfer token_in from user to self
    ERC20(order.token_in).transferFrom(order.account, self, order.amount_in)

    correct_amount: uint256 = ERC20(order.token_in).balanceOf(self) - balance_before

    # approve UNISWAP_ROUTER to spend token_in
    ERC20(order.token_in).approve(UNISWAP_ROUTER, correct_amount)

    # Use correct_amount instead of order.amount_in

Discussion

dot-pengun

Escalate

The contest page states that only tokens supported by uniswap v3 can be used, as shown below.

The Spot contracts need to be able to interact with any pool/token, we don’t want to have a centralized whitelist, we want it to work with any Uniswap v3 pool (and later others but this is probably out of scope).

According to the uniswap V3 docs, uniswap do not support fee-on-transfer tokens, so I believe this issue is invalid.

sherlock-admin2

Escalate

The contest page states that only tokens supported by uniswap v3 can be used, as shown below.

The Spot contracts need to be able to interact with any pool/token, we don’t want to have a centralized whitelist, we want it to work with any Uniswap v3 pool (and later others but this is probably out of scope).

According to the uniswap V3 docs, uniswap do not support fee-on-transfer tokens, so I believe this issue is invalid.

You've created a valid escalation!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

twicek

Escalate for 10 USDC This sentence implies that any token could be interacted with in the spot contracts:

The Spot contracts need to be able to interact with any pool/token, we don’t want to have a centralized whitelist, we want it to work with any Uniswap v3 pool (and later others but this is probably out of scope).

The sponsor didn't know that fee-on-transfer tokens couldn't be used in UniswapV3 when writing the README in which fee-on-transfer token related issues are explicitly in scope for the spot dex:

Q: Are there any FEE-ON-TRANSFER tokens interacting with the smart contracts? Margin: no Spot: possibly

Information acquired after the beginning of the contest and not explicitly mentioned in the README shouldn't supersede the README information.

sherlock-admin2

Escalate for 10 USDC This sentence implies that any token could be interacted with in the spot contracts:

The Spot contracts need to be able to interact with any pool/token, we don’t want to have a centralized whitelist, we want it to work with any Uniswap v3 pool (and later others but this is probably out of scope).

The sponsor didn't know that fee-on-transfer tokens couldn't be used in UniswapV3 when writing the README in which fee-on-transfer token related issues are explicitly in scope for the spot dex:

Q: Are there any FEE-ON-TRANSFER tokens interacting with the smart contracts? Margin: no Spot: possibly

Information acquired after the beginning of the contest and not explicitly mentioned in the README shouldn't supersede the README information.

You've created a valid escalation!

To remove the escalation from consideration: Delete your comment.

You may delete or edit your escalation comment anytime before the 48-hour escalation window closes. After that, the escalation becomes final.

141345

Recommendation: keep the original judging.

Escalate

The contest page states that only tokens supported by uniswap v3 can be used, as shown below.

The Spot contracts need to be able to interact with any pool/token, we don’t want to have a centralized whitelist, we want it to work with any Uniswap v3 pool (and later others but this is probably out of scope).

According to the uniswap V3 docs, uniswap do not support fee-on-transfer tokens, so I believe this issue is invalid.

The README is a little ambiguous.

The designed behavior seems to be compatible with fee on transfer token. And the POC demonstrates that the current codebase needs improvement for this purpose.

141345

The README is a little ambiguous.

The designed behavior seems to be compatible with fee on transfer token. And the POC demonstrates that the current codebase needs improvement for this purpose.

same as above

Unstoppable-DeFi

https://github.com/Unstoppable-DeFi/unstoppable-dex-audit/pull/10

hrishibhat

Result: Medium Has duplicates Agree with the Lead judge's comment below:

The README is a little ambiguous. The designed behavior seems to be compatible with fee on transfer token. And the POC demonstrates that the current codebase needs improvement for this purpose.

Accepting the escalation because it is understandable that it could be confusing, but will keep the issue valid medium.

sherlock-admin2

Escalations have been resolved successfully!

Escalation status:

maarcweiss

Fixed by using the common pattern of checking balances before and after the transfers and calculating the real amount in spot dex

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

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 code uses the original transfer amount instead of the actual received balance when approving and swapping, so it overestimates the available tokens for fee-on-transfer tokens.

Impact

This could halt token swaps midway, potentially locking funds and causing financial loss for the user.

What the attacker needed

Fee-on-transfer token; user initiates a swap

The fix

The issue was fixed by checking balances before and after transfers to calculate the actual amount received.

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.

Some tokens take a small fee when they are transferred, so the receiver gets less than the sender sent. The DEX code assumed the full amount would arrive, so later steps that needed the full amount could fail. This could leave a user's swap stuck and their funds locked.

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 0x4non

View all

6 more disclosed reports, best first.

More from Unstoppable

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.