Use beforeTokenTransfer hook in ERC20Snapshot (#2312)

This commit is contained in:
Julian M. Rodriguez
2020-08-11 16:51:58 -03:00
committed by GitHub
parent 722879b32d
commit 9700e6b4bd

View File

@ -104,28 +104,21 @@ abstract contract ERC20Snapshot is ERC20 {
return snapshotted ? value : totalSupply(); return snapshotted ? value : totalSupply();
} }
// _transfer, _mint and _burn are the only functions where the balances are modified, so it is there that the
// snapshots are updated. Note that the update happens _before_ the balance change, with the pre-modified value. // Update balance and/or total supply snapshots before the values are modified. This is implemented
// The same is true for the total supply and _mint and _burn. // in the _beforeTokenTransfer hook, which is executed for _mint, _burn, and _transfer operations.
function _transfer(address from, address to, uint256 value) internal virtual override { function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override {
super._beforeTokenTransfer(from, to, amount);
if (from == address(0) || to == address(0)) {
// mint or burn
_updateAccountSnapshot(from);
_updateTotalSupplySnapshot();
} else {
// transfer
_updateAccountSnapshot(from); _updateAccountSnapshot(from);
_updateAccountSnapshot(to); _updateAccountSnapshot(to);
super._transfer(from, to, value);
} }
function _mint(address account, uint256 value) internal virtual override {
_updateAccountSnapshot(account);
_updateTotalSupplySnapshot();
super._mint(account, value);
}
function _burn(address account, uint256 value) internal virtual override {
_updateAccountSnapshot(account);
_updateTotalSupplySnapshot();
super._burn(account, value);
} }
function _valueAt(uint256 snapshotId, Snapshots storage snapshots) function _valueAt(uint256 snapshotId, Snapshots storage snapshots)