Submitted by 0x3b, also found by dimulski, KupiaSec, t0x1c, ZdravkoHr, degensec, and REKCAH
getPrice in salesOption 2 can round down to the lower barrier, effectively skipping the last time period. In simpler terms, if the price is scheduled to decrease over 4 hours, it will decrease for 2.999 hours and then jump to the price at the end of the 4th hour.
getPrice uses this else-if calculation to determine whether it should return the price as an equation or just the final price.
The if statements method of division by rate is incorrect, as it results in rounding down and incorrect price movements. This can cause the price to jump over the last time period, effectively decreasing faster than intended.
Example:
Hour 0 to 1 - 0.49 ETH.
Hour 1 to 2 - 0.39 ETH - as it crosses the 1st hour, the price becomes 0.39 ETH.
Hour 2 to 3 - 0.29 ETH - same here.
It should go from 3 to 4 to 0.19 ETH, but that step is missed, and it goes directly to 0.1 ETH - the final price.
Hour 3 to 4 - 0.10 ETH - the final price.
The equation for calculating tDiff using the provided variables is:
Note: see original submission for math breakdown provided.
We want the crossover when hour 2 switches to 3, which is when (block.timestamp - collectionPhases[_collectionId].allowlistStartTime) = 3 h or 10800 sec.
Note: see original submission for math breakdown provided.
We plug in tDiff and the other variables into the if.
Note: see original submission for math breakdown provided.
We obtain 3 as the answer, which is due to rounding down, as we divide 0.39 by 0.1. Solidity only works with whole numbers, causing the if to fail as 3 > 3 is false. This leads to entering the else statement where the last price of 0.1 ETH is returned, effectively missing one step of the 4-hour decrease.
Gist here.
Add contracts/=smart-contracts/ to mappings and run it with forge test --match-test test_changeProof --lib-paths ../smart-contracts -vv.
Logs:
A simple yet effective suggestion is to add = in the if.
Error
a2rocket (NextGen) confirmed via duplicate issue #393
0xsomeone (judge) commented:
