ERC20Snapshot.sol [Code Snippet]
Captures token balances at specific moments.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../ERC20.sol";
abstract contract ERC20Snapshot is ERC20 {
mapping(uint256 => mapping(address => uint256)) private _snapshots;
uint256 private _currentSnapshotId;
event Snapshot(uint256 id);
// Create a new snapshot.
function _snapshot() internal returns (uint256) {
_currentSnapshotId++;
emit Snapshot(_currentSnapshotId);
return _currentSnapshotId;
}
// Retrieve balance at a given snapshot.
function balanceOfAt(address account, uint256 snapshotId) public view returns (uint256) {
return _snapshots[snapshotId][account];
}
// Logic to update snapshots on transfers is omitted for brevity.
}