The Swan smart contract lacks validation checks on the success of ERC20 token transfers (transfer and transferFrom). This omission can lead to unexpected behavior if token transfers fail, resulting in incomplete or erroneous asset sales and royalty distributions.
In the Swan contract, various functions, including transferRoyalties and purchase, perform transfers using the transfer and transferFrom functions of an ERC20 token. However, these functions do not check for success, which is crucial as ERC20 transfers may fail or return false. If these transfers fail silently, funds may not move as expected, causing inconsistent states within the contract and potentially harming users.
Here is an example of the vulnerable code in the transferRoyalties function:
Similarly, in the purchase function:
In these examples, the code assumes that all transfers are successful. Without checking the return value, the contract does not handle possible transfer failures, which may arise if the token has insufficient balance, allowance, or any restrictions in its implementation.
To demonstrate this vulnerability, deploy the Swan contract and simulate a failed token transfer. In this example, I use a custom ERC20 token that always fails on transfer or transferFrom.
Create a Custom ERC20 Token that fails on transfer:
Hardhat test:
The test indicate that the purchase function does not handle the failed token transfer, resulting in the buyer's balance remaining the same. The asset's status may be erroneously marked as sold despite the failed payment.
Without transfer success checks, failed transfers can lead to:
Inconsistent states, such as assets marked as "sold" without payment being received.
Buyers or sellers potentially being denied their expected funds or assets.
Incorrect royalty distributions if funds are not transferred to all intended recipients.
These issues can damage trust and lead to potential financial losses for contract users.
Manual review.
Add require statements to check the success of all transfer and transferFrom calls, as shown below:
By validating each transfer’s success, the contract will revert if a transfer fails, preventing further execution and maintaining consistency.
The contest is live. Earn rewards by submitting a finding.
This is your time to appeal against judgements on your submissions.
Appeals are being carefully reviewed by our judges.