Open nidhhoggr opened 1 year ago
@nidhhoggr Not sure if this is true, but I think this can overflow in your implementation.
uint256 end = nextTokenId + quantity;
As mentioned previously, this code was grabbed from the latest commit to ERC721A where the same code is used. I've taken a look at the code and I don't think overflow is possible, here is why.
In the same function the following line is executed before the unchecked block.
_currentIndex += quantity;
At this point _currentIndex
and nextTokenId
are the same value. If quantity exceeded 2**256 the transaction would automatically revert before the unchecked block is reached. Looking more closely at the code in this commit, we could also get rid of end
and just use _currentIndex
because they are the same value.
When investigating how to come up with a way to reduce log4 calls to only once per _mint call, I came up with the following code. The only problem here is that is winded up using ~16 more gas than before. Basically it emulates a do-while loop which is not supported un Yul yet.
I went back and checked ERC721As implementation and sure enough, they have also remedied this using a do while loop with native solidity. Implementing it this way result in aroun ~100 gas savings while providing more readability and still eliminating the constant declarations. https://github.com/chiru-labs/ERC721A/blob/main/contracts/ERC721A.sol#L773
At the end of the day constants are useful for code readability especially when the variable is used in multiple places. In our scenario, these variables are only used once in the code. Further, usage of constants in upgradable contracts introduce storage collision issues (as would any declared state variables) so the tradeoff of simply removing them are more advantageous. In scenarios where the constant would occur in multiple areas of the code, a library could be used with helper methods that contain the constants. e.g. a helper method for masking 160 bits. The commit below address the constants declared in our base contracts but there are still some declared in the Random extensions. It's up to you if you'd like to remedy those.