Imprecise calculations in launchFor() lead to less liquidity be added to the pair via the router
The launchFor() function in Bonding.sol computes the liquidity amount with a division performed before a multiplication, so integer rounding discards precision. This causes less liquidity than intended to be added to the pair via the router, and in some cases the computed liquidity can be zero.
Matin Follow with codexNature
Originally disclosed on Code4rena
Submitted by Matin, also found by codexNature
Finding description and impact
Solidity rounds down the result of an integer division, and because of that, it is always recommended to multiply before dividing to avoid that precision loss. The problem arises in the Bonding.sol’s launchFor() function where the liquidity is calculated:
function launchFor(
string memory _name,
string memory _ticker,
uint8[] memory cores,
string memory desc,
string memory img,
string[4] memory urls,
uint256 purchaseAmount,
address creator
) public nonReentrant returns (address, address, uint) {
// code
uint256 k = ((K * 10000) / assetRate);
uint256 liquidity = (((k * 10000 ether) / supply) * 1 ether) / 10000;
Although this model is implemented to calculate the k, we can see there is a hidden division before a multiplication that makes round down the whole expression. This is bad as the precision loss can be significant, which leads to the pool calculating less liquidity than actual.
Recommended mitigation steps
Consider changing the liquidity calculation in the way that prioritize the multiplication over division or use the high precision fixed point math libraries (e.g. PRB math lib).
Proof of Concept
uint256 public constant K = 3_000_000_000_000;
function testPrecisionLoss(
uint256 assetRate,
uint supply
) public pure returns (uint256 act, uint256 acc) {
uint256 k = ((K * 10000) / assetRate);
act = (((k * 10000 ether) / supply) * 1 ether) / 10000;
acc = (K * 10000 * 10000 ether * 1 ether) / (assetRate * supply * 10000);
}
For the assetRate and supply equal to (7.98e15, 1.9283e18):
The result would be:
Current Implementation 1555700000000000000
Actual Implementation 1949592125831354822
This is equal to ~25% relative error. Also, it is worth to mention that in some cases even the liquidity becomes zero. For the assetRate and supply equal to (1e18, 2e18), the result would be:
Current Implementation 0
Actual Implementation 15000000000000000
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 liquidity expression divides by supply before multiplying by 1 ether, so integer truncation is amplified instead of being deferred to the end.
Impact
The pair receives less liquidity than the model intends, with roughly a 25% relative error in the example given, and in some cases the calculated liquidity is zero.
What the attacker needed
Calling launchFor() with certain assetRate and supply
The fix
The calculation should be reordered to multiply before dividing, or a high-precision fixed-point maths library should be used.
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.
Smart contracts store whole numbers only, so when a division does not divide evenly the leftover fraction is thrown away. If you divide early and multiply afterwards, that thrown-away amount gets multiplied too, so the final number can be noticeably too small. Here the contract worked out how much liquidity to add to a trading pool in that order, so the pool received less than it should.
What is Smart contracts?
Flaws in on-chain code that let an attacker drain funds, freeze them or break the protocol's accounting.