Solidity

為什麼在最新開放的 zeppelin erc20 轉賬中,餘額和要求檢查不在未檢查的功能範圍內?

  • October 20, 2022

如果由於檢查 mint 功能而無法在此傳輸中上溢/下溢,那麼為什麼這 2 行 (236/237) 不在未檢查欄位內?

https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol#L236

這兩行不執行算術運算,因此將它們移動到unchecked塊內不會帶來任何好處。也許它會使程式碼更難閱讀:

   uint256 fromBalance = _balances[from];
   require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
   unchecked {
       _balances[from] = fromBalance - amount;
       // Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
       // decrementing then incrementing.
       _balances[to] += amount;
   }

引用自:https://ethereum.stackexchange.com/questions/137800