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

CMTAT 2.6.0 #283

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
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
Loading