Skip to content

Commit c3cb7a0

Browse files
Amxxarr00
andauthored
Deduplicate logic in Votes.sol (#5314)
Co-authored-by: Arr00 <13561405+arr00@users.noreply.github.com>
1 parent 2562c11 commit c3cb7a0

File tree

2 files changed

+15
-23
lines changed

2 files changed

+15
-23
lines changed

contracts/governance/utils/Votes.sol

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ abstract contract Votes is Context, EIP712, Nonces, IERC5805 {
7171
return "mode=blocknumber&from=default";
7272
}
7373

74+
/**
75+
* @dev Validate that a timepoint is in the past, and return it as a uint48.
76+
*/
77+
function _validateTimepoint(uint256 timepoint) internal view returns (uint48) {
78+
uint48 currentTimepoint = clock();
79+
if (timepoint >= currentTimepoint) revert ERC5805FutureLookup(timepoint, currentTimepoint);
80+
return SafeCast.toUint48(timepoint);
81+
}
82+
7483
/**
7584
* @dev Returns the current amount of votes that `account` has.
7685
*/
@@ -87,11 +96,7 @@ abstract contract Votes is Context, EIP712, Nonces, IERC5805 {
8796
* - `timepoint` must be in the past. If operating using block numbers, the block must be already mined.
8897
*/
8998
function getPastVotes(address account, uint256 timepoint) public view virtual returns (uint256) {
90-
uint48 currentTimepoint = clock();
91-
if (timepoint >= currentTimepoint) {
92-
revert ERC5805FutureLookup(timepoint, currentTimepoint);
93-
}
94-
return _delegateCheckpoints[account].upperLookupRecent(SafeCast.toUint48(timepoint));
99+
return _delegateCheckpoints[account].upperLookupRecent(_validateTimepoint(timepoint));
95100
}
96101

97102
/**
@@ -107,11 +112,7 @@ abstract contract Votes is Context, EIP712, Nonces, IERC5805 {
107112
* - `timepoint` must be in the past. If operating using block numbers, the block must be already mined.
108113
*/
109114
function getPastTotalSupply(uint256 timepoint) public view virtual returns (uint256) {
110-
uint48 currentTimepoint = clock();
111-
if (timepoint >= currentTimepoint) {
112-
revert ERC5805FutureLookup(timepoint, currentTimepoint);
113-
}
114-
return _totalCheckpoints.upperLookupRecent(SafeCast.toUint48(timepoint));
115+
return _totalCheckpoints.upperLookupRecent(_validateTimepoint(timepoint));
115116
}
116117

117118
/**

contracts/governance/utils/VotesExtended.sol

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import {SafeCast} from "../../utils/math/SafeCast.sol";
3131
* {ERC20Votes} and {ERC721Votes} follow this pattern and are thus safe to use with {VotesExtended}.
3232
*/
3333
abstract contract VotesExtended is Votes {
34-
using SafeCast for uint256;
3534
using Checkpoints for Checkpoints.Trace160;
3635
using Checkpoints for Checkpoints.Trace208;
3736

@@ -47,11 +46,7 @@ abstract contract VotesExtended is Votes {
4746
* - `timepoint` must be in the past. If operating using block numbers, the block must be already mined.
4847
*/
4948
function getPastDelegate(address account, uint256 timepoint) public view virtual returns (address) {
50-
uint48 currentTimepoint = clock();
51-
if (timepoint >= currentTimepoint) {
52-
revert ERC5805FutureLookup(timepoint, currentTimepoint);
53-
}
54-
return address(_delegateCheckpoints[account].upperLookupRecent(timepoint.toUint48()));
49+
return address(_delegateCheckpoints[account].upperLookupRecent(_validateTimepoint(timepoint)));
5550
}
5651

5752
/**
@@ -63,11 +58,7 @@ abstract contract VotesExtended is Votes {
6358
* - `timepoint` must be in the past. If operating using block numbers, the block must be already mined.
6459
*/
6560
function getPastBalanceOf(address account, uint256 timepoint) public view virtual returns (uint256) {
66-
uint48 currentTimepoint = clock();
67-
if (timepoint >= currentTimepoint) {
68-
revert ERC5805FutureLookup(timepoint, currentTimepoint);
69-
}
70-
return _balanceOfCheckpoints[account].upperLookupRecent(timepoint.toUint48());
61+
return _balanceOfCheckpoints[account].upperLookupRecent(_validateTimepoint(timepoint));
7162
}
7263

7364
/// @inheritdoc Votes
@@ -82,10 +73,10 @@ abstract contract VotesExtended is Votes {
8273
super._transferVotingUnits(from, to, amount);
8374
if (from != to) {
8475
if (from != address(0)) {
85-
_balanceOfCheckpoints[from].push(clock(), _getVotingUnits(from).toUint208());
76+
_balanceOfCheckpoints[from].push(clock(), SafeCast.toUint208(_getVotingUnits(from)));
8677
}
8778
if (to != address(0)) {
88-
_balanceOfCheckpoints[to].push(clock(), _getVotingUnits(to).toUint208());
79+
_balanceOfCheckpoints[to].push(clock(), SafeCast.toUint208(_getVotingUnits(to)));
8980
}
9081
}
9182
}

0 commit comments

Comments
 (0)