From 899cf35a5d121aaa489d462c5ede60b9775221dd Mon Sep 17 00:00:00 2001 From: Ryan Sauge Date: Mon, 11 Dec 2023 15:29:06 +0100 Subject: [PATCH] Adapt snapshotModule for debt payment --- .../internal/ERC20SnapshotModuleInternal.sol | 33 ++++++++++++------- ...RC20SnapshotModuleCommonGetNextSnapshot.js | 7 ++-- .../ERC20SnapshotModuleUtils.js | 6 +++- 3 files changed, 30 insertions(+), 16 deletions(-) diff --git a/contracts/modules/internal/ERC20SnapshotModuleInternal.sol b/contracts/modules/internal/ERC20SnapshotModuleInternal.sol index f80ec992..8537987a 100644 --- a/contracts/modules/internal/ERC20SnapshotModuleInternal.sol +++ b/contracts/modules/internal/ERC20SnapshotModuleInternal.sol @@ -23,9 +23,9 @@ abstract contract ERC20SnapshotModuleInternal is SnapshotModuleBase, ERC20Upgrad /** - @dev - list of scheduled snapshot (time) - This list is sorted in ascending order + * @dev + * list of scheduled snapshot (time) + * This list is sorted in ascending order */ uint256[] private _scheduledSnapshots; @@ -36,8 +36,8 @@ abstract contract ERC20SnapshotModuleInternal is SnapshotModuleBase, ERC20Upgrad /** - @dev 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. + * @dev 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 _snapshotUpdate( address from, @@ -63,23 +63,32 @@ abstract contract ERC20SnapshotModuleInternal is SnapshotModuleBase, ERC20Upgrad /** - @dev See {OpenZeppelin - ERC20Snapshot} + * @dev See {OpenZeppelin - ERC20Snapshot} */ function _updateAccountSnapshot(address account) private { _updateSnapshot(_accountBalanceSnapshots[account], balanceOf(account)); } /** - @dev See {OpenZeppelin - ERC20Snapshot} + * @dev See {OpenZeppelin - ERC20Snapshot} */ function _updateTotalSupplySnapshot() private { _updateSnapshot(_totalSupplySnapshots, totalSupply()); } + /** + * @notice Return snapshotBalanceOf and snapshotTotalSupply to avoid multiple calls + * @return ownerBalance , totalSupply - see snapshotBalanceOf and snapshotTotalSupply + */ + function getSnapshotInfoBatch(uint256 time, address owner) public view returns (uint256 ownerBalance, uint256 totalSupply) { + ownerBalance = snapshotBalanceOf(time, owner); + totalSupply = snapshotTotalSupply(time); + } + /** - @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 + * @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, @@ -94,9 +103,9 @@ abstract contract ERC20SnapshotModuleInternal is SnapshotModuleBase, ERC20Upgrad } /** - @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 + * @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) public view returns (uint256) { (bool snapshotted, uint256 value) = _valueAt( diff --git a/test/common/ERC20SnapshotModuleCommon/ERC20SnapshotModuleCommonGetNextSnapshot.js b/test/common/ERC20SnapshotModuleCommon/ERC20SnapshotModuleCommonGetNextSnapshot.js index b1c538b4..8b2ff251 100644 --- a/test/common/ERC20SnapshotModuleCommon/ERC20SnapshotModuleCommonGetNextSnapshot.js +++ b/test/common/ERC20SnapshotModuleCommon/ERC20SnapshotModuleCommonGetNextSnapshot.js @@ -14,7 +14,7 @@ function ERC20SnapshotModuleCommonGetNextSnapshot ( beforeEach(async function () { this.currentTime = await time.latest() }) - it('can get all next snapshots', async function () { + it('testCanGetAllNextSnapshots', async function () { // Arrange this.snapshotTime1 = this.currentTime.add(time.duration.seconds(10)) this.snapshotTime2 = this.currentTime.add(time.duration.seconds(15)) @@ -49,7 +49,8 @@ function ERC20SnapshotModuleCommonGetNextSnapshot ( ]) }) - it('return empty array if all snapshots are in the past', async function () { + // + it('testCanReturnEmptyArrayIfAllSnapshotsAreInThePast', async function () { // Arrange this.snapshotTime1 = this.currentTime.add(time.duration.seconds(2)) this.snapshotTime2 = this.currentTime.add(time.duration.seconds(3)) @@ -71,7 +72,7 @@ function ERC20SnapshotModuleCommonGetNextSnapshot ( snapshots.length.should.equal(0) }) - it('return only future snapshots if some snapshots are in the past', async function () { + it('testCanReturnOnlyFutureSnapshotsIfSomeSnapshotsAreInThePast', async function () { // Arrange this.snapshotTime1 = this.currentTime.add(time.duration.seconds(2)) this.snapshotTime2 = this.currentTime.add(time.duration.seconds(20)) diff --git a/test/common/ERC20SnapshotModuleCommon/ERC20SnapshotModuleUtils/ERC20SnapshotModuleUtils.js b/test/common/ERC20SnapshotModuleCommon/ERC20SnapshotModuleUtils/ERC20SnapshotModuleUtils.js index 276e267e..094c035a 100644 --- a/test/common/ERC20SnapshotModuleCommon/ERC20SnapshotModuleUtils/ERC20SnapshotModuleUtils.js +++ b/test/common/ERC20SnapshotModuleCommon/ERC20SnapshotModuleUtils/ERC20SnapshotModuleUtils.js @@ -11,11 +11,15 @@ async function checkSnapshot (time, totalSupply, addresses, balances) { (await this.cmtat.snapshotTotalSupply(time)).should.be.bignumber.equal( totalSupply ) - for (let i = 0; i < balances.length; ++i) { ( await this.cmtat.snapshotBalanceOf(time, addresses[i]) ).should.be.bignumber.equal(balances[i]) + await this.cmtat.getSnapshotInfoBatch(time, addresses[i]) + const { 0: ownerBalance, 1: totalSupplyGet } = await this.cmtat.getSnapshotInfoBatch(time, addresses[i]) + //const [ownerBalance, totalSupplyGet] + ownerBalance.should.be.bignumber.equal(balances[i]) + totalSupplyGet.should.be.bignumber.equal(totalSupply) } }