Single token purchase restriction on curve creation enables sniping
The price calculation in the Curves contract reverts when a curve subject tries to buy more than one token during initialisation, because of the order of terms in the sum2 expression. This forces the subject to buy only a single token, letting automated sniping bots buy up the rest at a low price before the subject can acquire more.
Submitted by EV_om, also found by mrudenko, McToady, adamn000, M3azad (1, 2), burhan_khaja, bengyles, matejdb, jesjupyter, 13u9, wangxx2026, pipidu83, erosjohn, eeshenggoh, lukejohn, aslanbek (1, 2), twcctop, KingNFT, and Daniel526
The getPrice() function in the Curves contract is used to calculate the price of tokens when buying or selling. However, due to the order of arguments in the sum2 calculation within this function, subjects are restricted to buying only one token when initializing their curve.
The issue arises from the expression (supply - 1 + amount), which reverts when supply is greater than 1. However, if the expression was (supply + amount - 1), this issue would not occur. This restriction limits the functionality of the contract and makes it vulnerable to sniping on initialization and first-purchase frontrunning. This issue has spawned a small industry around friend.tech, with different companies offering sniping bots that can make instant automatic first purchases for accounts associated with a sizeable social media presence.
function getPrice(uint256 supply, uint256 amount) public pure returns (uint256) {
uint256 sum1 = supply == 0 ? 0 : ((supply - 1) * (supply) * (2 * (supply - 1) + 1)) / 6;
uint256 sum2 = supply == 0 && amount == 1
? 0
: ((supply - 1 + amount) * (supply + amount) * (2 * (supply - 1 + amount) + 1)) / 6;
...
}
Although, the issue can be mitigated in the Curves implementation by the curve subject opening a presale, it is still present for curves initialized without presale.
Proof of Concept
- A user with a large social media following wants to initialize a curve and buy the first 10 tokens for future use, possibly for an airdrop to her followers.
- She does not want to create a presale and instead initializes the curve directly via
buyCurvesToken(). - As soon as the curve is initialized, automated sniping bots start buying large numbers of tokens.
- She is now forced to purchase the tokens at a much higher price and may incur losses from bots realizing their profits.
Recommended Mitigation Steps
To resolve this issue, the order of arguments in the sum2 calculation within the getPrice() function should be adjusted. Instead of (supply - 1 + amount), the expression should be (supply + amount - 1). This change will allow subjects to buy more than one token when initializing their curve, enhancing the contract’s functionality and protecting the users.
andresaiello (Curves) disputed via duplicate Issue #44 and commented:
Reverts in the price calculation.
I’m not convinced that the purpose of the
presalesfunctionality is to allow the token subject to create a presale for himself just so that he can buy more than one token (and then not being able to have a presale for other users).If the token subject would choose to do a regular presale, using the regular community mechanisms from the NFT era, how would they ensure that they don’t get a sniping bot registered for the presale?
The team might have intended to solve the issues with frontrunners, but to me it doesn’t seem they succeeded.
Note: For full discussion, see here.
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
Likely the terms in the sum2 expression are ordered as (supply - 1 + amount), which underflows and reverts once supply is greater than 1, instead of (supply + amount - 1).
Impact
The subject cannot buy more than one token at initialisation, so sniping bots can front-run the first purchase and the subject may have to buy at a higher price and incur losses.
What the attacker needed
Curve initialised without a presale; automated sniping bots watching new curves
The fix
The order of terms in the sum2 calculation should be changed so the expression reads (supply + amount - 1) rather than (supply - 1 + amount).
How it was found
- A user with a large following initialises a curve and wants to buy the first ten tokens.
- She skips the presale and initialises the curve directly through the buy function.
- Sniping bots immediately buy large numbers of tokens once the curve exists.
- She must now buy at a much higher price and may lose money to the bots.
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.
A bonding curve is a smart contract that sets a token's price based on how many tokens have already been sold. Here, a maths expression in the price function fails (reverts) whenever the first buyer tries to buy more than one token at the very start, so they can only take one. Bots watching the blockchain can then grab the remaining cheap tokens before the real owner buys more.
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.