Fee on transfer tokens will not behave as expected
The contract checks token balances against expected targets after transfers, but fee-on-transfer tokens cause the received amount to be less than the target, making mint() and swap() revert. This prevents the protocol from supporting such tokens, contrary to the whitepaper's claim of working with any ERC20 pair.
RaymondFam Follow with rbserver, nadin and 5 others
Originally disclosed on Code4rena
Submitted by RaymondFam, also found by rbserver, nadin, kaden, pavankv, mert_eren, SaeedAlipoor01988, and Rolezn
Impact
According to Whitepaper 1.1 Permissionless:
“In Timeswap, liquidity providers can create pools for any ERC20 pair, without permission. It is designed to be generalized and works for any pair of tokens, at any time frame, and at any market state …
If fee on transfer token(s) is/are entailed, it will specifically make mint() and swap() revert in TimeswapV2Option.sol when checking if the token0 or token1 balance target is achieved.
Proof of Concept
File: TimeswapV2Option.sol#L144-L148
// check if the token0 balance target is achieved.
if (token0AndLong0Amount != 0) Error.checkEnough(IERC20(token0).balanceOf(address(this)), currentProcess.balance0Target);
// check if the token1 balance target is achieved.
if (token1AndLong1Amount != 0) Error.checkEnough(IERC20(token1).balanceOf(address(this)), currentProcess.balance1Target);
File: TimeswapV2Option.sol#L234-L235
// check if the token0 or token1 balance target is achieved.
Error.checkEnough(IERC20(param.isLong0ToLong1 ? token1 : token0).balanceOf(address(this)), param.isLong0ToLong1 ? currentProcess.balance1Target : currentProcess.balance0Target);
/// @dev Reverts when token amount not received.
/// @param balance The balance amount being subtracted.
/// @param balanceTarget The amount target.
function checkEnough(uint256 balance, uint256 balanceTarget) internal pure {
if (balance < balanceTarget) revert NotEnoughReceived(balance, balanceTarget);
}
As can be seen from the code blocks above, checkEnough() is meant to be reverting when token amount has not been received. But in the case of deflationary tokens, the error is going to be thrown even though the token amount has been received due to the fee factor making balance < balanceTarget, i.e the contract balance of token0 and/or token1 always less than currentProcess.balance0Target or currentProcess.balance1Target.
Recommended Mitigation Steps
Consider:
- Whitelisting token0 and token1 ensuring no fee-on-transfer token is allowed when deploying a new Timeswap V2 Option pair contract, or
- Calculating the balance before and after the transfer to the recipient during the process, and use the difference between those two balances as the amount received rather than using the input amount (
token0AndLong0Amountortoken1AndLong1Amount) if deflationary token is going to be allowed in the protocol.
vhawk19 (Timeswap) acknowledged and commented:
Not supported by design.
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 contract compares its token balance to a target amount that assumes the full transfer amount is received, without accounting for transfer fees.
What the attacker needed
Fee-on-transfer token used in pair
The fix
The team acknowledged the issue but stated it is not supported by design, so no fix was implemented.
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 transferred, so the receiver gets less than the sender sent. The contract expects to receive the full amount and checks its balance, so it fails and cancels the transaction. This means the protocol cannot be used with those tokens.
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.