Unintended protocol behavior when doing swap more than two users
Summary
Unintended protocol behavior when doing swap lead to an unexpected revert. This revert comes from unordered transactions where this accident could happen at anytime, causes inconvenience for users when doing swap.
Because of the AmirX creates a transfer all of funds with telcoin.balanceOf(address(this)), it automatically affect to move all of funds into address defi.defiSafe. For single transaction let's say just Alice transaction, it acceptable because just Alice token that would be move to address defi.defiSafe. But in another case, it doesn't acceptable.
Assume first, we go through normal case. We have two users that is Alice and Bob. They would swap their assets from eUSD to eMXN with fee token telcoin directly . The scenario looks like:
Alice sends telcoin token to the AmirX as a referral fees and Alice creates a swap
Also Bob sends telcoin token to the AmirX as a referral fees and Bob creates a swap
The transactions will run normal
Assume second, we go through unnormal case. We have two users that is Alice and Bob. And also they would swap their assets from eUSD to eMXN with fee token with telcoin directly. This unnormal case can occur when:
Alice sends telcoin token to the AmirX as a referral fees. And then Bob also sends telcoin token to the AmirX as a referral fees
The AmirX protocol contains all of referral fees from Alice and Bob
Alice creates a swap and then theAmirX moves all of funds (both referral fees of Alice and Bob) into defi.defiSafe
Afterthat, Bob creates a swap and the Bob's transaction will revert because the referral fee of Bob already move first when Alice creates a swap.
Impact
When we see the unnormal case above, the impact will affect to Bob that the protocol will revert his transaction and losing his referral fee. To avoid it, Bob must retransfer again to the AmirX and make Bob pays the referral fees twice (with notes that his second transaction in normal case).
AmirX
Swaps
two steps
✔ Unnormal case when doing swap (51ms)
1 passing (1s)
Mitigation
Instead of using telcoin.balanceOf(address(this)) as a value, recommend to use defi.referralFee as a value exatcly to avoid moving all of funds into address defi.defiSafe.
hrmneffdii
High
Unintended protocol behavior when doing swap more than two users
Summary
Unintended protocol behavior when doing swap lead to an unexpected revert. This revert comes from unordered transactions where this accident could happen at anytime, causes inconvenience for users when doing swap.
Root Cause
Let's see code below
https://github.com/sherlock-audit/2024-11-telcoin/blob/main/telcoin-audit/contracts/swap/AmirX.sol#L209-L213
Because of the
AmirX
creates a transfer all of funds withtelcoin.balanceOf(address(this))
, it automatically affect to move all of funds into addressdefi.defiSafe
. For single transaction let's say justAlice
transaction, it acceptable because justAlice
token that would be move to addressdefi.defiSafe
. But in another case, it doesn't acceptable.Assume first, we go through normal case. We have two users that is
Alice
andBob
. They would swap their assets fromeUSD
toeMXN
with fee tokentelcoin
directly . The scenario looks like:Alice
sendstelcoin
token to theAmirX
as a referral fees andAlice
creates a swapBob
sendstelcoin
token to theAmirX
as a referral fees andBob
creates a swapAssume second, we go through unnormal case. We have two users that is
Alice
andBob
. And also they would swap their assets fromeUSD
toeMXN
with fee token withtelcoin
directly. This unnormal case can occur when:Alice
sendstelcoin
token to theAmirX
as a referral fees. And thenBob
also sendstelcoin
token to theAmirX
as a referral feesAmirX
protocol contains all of referral fees fromAlice
andBob
Alice
creates a swap and then theAmirX
moves all of funds (both referral fees ofAlice
andBob
) intodefi.defiSafe
Bob
creates a swap and theBob
's transaction will revert because the referral fee ofBob
already move first whenAlice
creates a swap.Impact
When we see the unnormal case above, the impact will affect to
Bob
that the protocol will revert his transaction and losing his referral fee. To avoid it,Bob
must retransfer again to theAmirX
and makeBob
pays the referral fees twice (with notes that his second transaction in normal case).PoC
Mitigation
Instead of using
telcoin.balanceOf(address(this))
as a value, recommend to usedefi.referralFee
as a value exatcly to avoid moving all of funds into addressdefi.defiSafe
.