Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use beforeTokenTransfer hook in ERC20Snapshot #2312

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 12 additions & 19 deletions contracts/token/ERC20/ERC20Snapshot.sol
Original file line number Diff line number Diff line change
Expand Up @@ -104,28 +104,21 @@ abstract contract ERC20Snapshot is ERC20 {
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.
// The same is true for the total supply and _mint and _burn.
function _transfer(address from, address to, uint256 value) internal virtual override {
_updateAccountSnapshot(from);
_updateAccountSnapshot(to);

super._transfer(from, to, value);
}

function _mint(address account, uint256 value) internal virtual override {
_updateAccountSnapshot(account);
_updateTotalSupplySnapshot();
// Update balance and/or total supply snapshots before the values are modified. This is implemented
// in the _beforeTokenTransfer hook, which is executed for _mint, _burn, and _transfer operations.
function _beforeTokenTransfer(address from, address to, uint256 amount) internal virtual override {
super._beforeTokenTransfer(from, to, amount);

super._mint(account, value);
}

function _burn(address account, uint256 value) internal virtual override {
_updateAccountSnapshot(account);
if (from == address(0) || to == address(0)) {
// mint or burn
_updateAccountSnapshot(from);
_updateTotalSupplySnapshot();

super._burn(account, value);
} else {
// transfer
_updateAccountSnapshot(from);
_updateAccountSnapshot(to);
}
}

function _valueAt(uint256 snapshotId, Snapshots storage snapshots)
Expand Down