Skip to content

Commit

Permalink
Separate ERC20 calls inside snapshotModule
Browse files Browse the repository at this point in the history
  • Loading branch information
rya-sge committed Nov 12, 2024
1 parent c887529 commit 75dd083
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 34 deletions.
41 changes: 7 additions & 34 deletions contracts/modules/internal/ERC20SnapshotModuleInternal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,13 +68,7 @@ abstract contract ERC20SnapshotModuleInternal is ICMTATSnapshot, SnapshotModuleB
uint256 time,
address owner
) public view returns (uint256) {
SnapshotModuleBaseStorage storage $ = _getSnapshotModuleBaseStorage();
(bool snapshotted, uint256 value) = _valueAt(
time,
$._accountBalanceSnapshots[owner]
);

return snapshotted ? value : balanceOf(owner);
return _snapshotBalanceOf(time, owner, balanceOf(owner));
}

/**
Expand All @@ -83,12 +77,7 @@ abstract contract ERC20SnapshotModuleInternal is ICMTATSnapshot, SnapshotModuleB
* @return value stored in the snapshot, or the actual totalSupply if no snapshot
*/
function snapshotTotalSupply(uint256 time) public view returns (uint256) {
SnapshotModuleBaseStorage storage $ = _getSnapshotModuleBaseStorage();
(bool snapshotted, uint256 value) = _valueAt(
time,
$._totalSupplySnapshots
);
return snapshotted ? value : totalSupply();
return _snapshotTotalSupply(time, totalSupply());
}

/*//////////////////////////////////////////////////////////////
Expand All @@ -106,34 +95,18 @@ abstract contract ERC20SnapshotModuleInternal is ICMTATSnapshot, SnapshotModuleB
_setCurrentSnapshot();
if (from != address(0)) {
// for both burn and transfer
_updateAccountSnapshot(from);
_updateAccountSnapshot(from, balanceOf(from));
if (to != address(0)) {
// transfer
_updateAccountSnapshot(to);
_updateAccountSnapshot(to, balanceOf(to));
} else {
// burn
_updateTotalSupplySnapshot();
_updateTotalSupplySnapshot(totalSupply());
}
} else {
// mint
_updateAccountSnapshot(to);
_updateTotalSupplySnapshot();
_updateAccountSnapshot(to, balanceOf(to));
_updateTotalSupplySnapshot(totalSupply());
}
}

/**
* @dev See {OpenZeppelin - ERC20Snapshot}
*/
function _updateAccountSnapshot(address account) private {
SnapshotModuleBaseStorage storage $ = _getSnapshotModuleBaseStorage();
_updateSnapshot($._accountBalanceSnapshots[account], balanceOf(account));
}

/**
* @dev See {OpenZeppelin - ERC20Snapshot}
*/
function _updateTotalSupplySnapshot() private {
SnapshotModuleBaseStorage storage $ = _getSnapshotModuleBaseStorage();
_updateSnapshot($._totalSupplySnapshots, totalSupply());
}
}
48 changes: 48 additions & 0 deletions contracts/modules/internal/base/SnapshotModuleBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -397,6 +397,54 @@ abstract contract SnapshotModuleBase is Initializable {
return (mostRecent, index);
}

/* ============ Require balance and total supply ============ */
/**
* @dev See {OpenZeppelin - ERC20Snapshot}
*/
function _updateAccountSnapshot(address account, uint256 accountBalance) internal {
SnapshotModuleBaseStorage storage $ = _getSnapshotModuleBaseStorage();
_updateSnapshot($._accountBalanceSnapshots[account], accountBalance);
}

/**
* @dev See {OpenZeppelin - ERC20Snapshot}
*/
function _updateTotalSupplySnapshot(uint256 totalSupply) internal {
SnapshotModuleBaseStorage storage $ = _getSnapshotModuleBaseStorage();
_updateSnapshot($._totalSupplySnapshots, totalSupply);
}

/**
* @notice Return the number of tokens owned by the given owner at the time when the snapshot with the given time was created.
* @return value stored in the snapshot, or the actual balance if no snapshot
*/
function _snapshotBalanceOf(
uint256 time,
address owner,
uint256 ownerBalance
) internal view returns (uint256) {
SnapshotModuleBaseStorage storage $ = _getSnapshotModuleBaseStorage();
(bool snapshotted, uint256 value) = _valueAt(
time,
$._accountBalanceSnapshots[owner]
);
return snapshotted ? value : ownerBalance;
}

/**
* @dev See {OpenZeppelin - ERC20Snapshot}
* Retrieves the total supply at the specified time.
* @return value stored in the snapshot, or the actual totalSupply if no snapshot
*/
function _snapshotTotalSupply(uint256 time, uint256 totalSupply) internal view returns (uint256) {
SnapshotModuleBaseStorage storage $ = _getSnapshotModuleBaseStorage();
(bool snapshotted, uint256 value) = _valueAt(
time,
$._totalSupplySnapshots
);
return snapshotted ? value : totalSupply;
}

/* ============ Utility functions ============ */


Expand Down

0 comments on commit 75dd083

Please sign in to comment.