Swap fees in the handler() function are only taken for the first swap but not for the second swap which is not an expected behavior.
Vulnerability Detail
The current functionality of the handler() function that performs the swap takes fees the following way:
Let's say we have SOL / USDT and USDC (or ETH but it's not supported yet) / USDT pools and we perform a swap from SOL to USDC. The first swap is when we convert SOL to the quote token (USDT) and the second is when we swap USDT on USDC obtaining the final amount. Currently the fees are only taken on the first swap:
let swap_fee = checked_mul_div_round_up(quote_amount, fee_rate as u128, ONE_E5_U128)?;
0xeix
Medium
Swap fees are only taken for the first swap
Summary
Swap fees in the
handler()
function are only taken for the first swap but not for the second swap which is not an expected behavior.Vulnerability Detail
The current functionality of the
handler()
function that performs the swap takes fees the following way:Let's say we have SOL / USDT and USDC (or ETH but it's not supported yet) / USDT pools and we perform a swap from SOL to USDC. The first swap is when we convert SOL to the quote token (USDT) and the second is when we swap USDT on USDC obtaining the final amount. Currently the fees are only taken on the first swap:
But they're not taken on the second swap:
https://github.com/sherlock-audit/2024-08-woofi-solana-deployment/blob/main/WOOFi_Solana/programs/woofi/src/instructions/swap.rs#L168-180
In comparison, EVM implementation of the same functionality takes swap on both operations - when selling quote and when selling base:
https://github.com/woonetwork/WooPoolV2/blob/main/contracts/WooPPV2.sol#L453
https://github.com/woonetwork/WooPoolV2/blob/main/contracts/WooPPV2.sol#L492
Impact
The swap fees are not taken on the swap from the quote token to the
woopool_to.token_mint
(selling quote operation):Code Snippet
Provided above.
Tool used
Manual Review
Recommendation
Take the fee on the
_to_amount
after selling quote token.