Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
lint
  • Loading branch information
RenanSouza2 committed Apr 16, 2023
commit 8b9029b31d183a0c72d991db00024f97059c858b
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ abstract contract GovernorVotesQuorumFraction is GovernorVotes {
}

// Otherwise, do the binary search
uint32 key = SafeCast.toUint32(blockNumber);
uint32 key = SafeCast.toUint32(blockNumber);
return _quorumNumeratorHistory.upperLookup(key);
}

Expand Down Expand Up @@ -111,10 +111,7 @@ abstract contract GovernorVotesQuorumFraction is GovernorVotes {
}

// Set new quorum for future proposals
_quorumNumeratorHistory.push(
SafeCast.toUint32(block.number),
SafeCast.toUint224(newQuorumNumerator)
);
_quorumNumeratorHistory.push(SafeCast.toUint32(block.number), SafeCast.toUint224(newQuorumNumerator));

emit QuorumNumeratorUpdated(oldQuorumNumerator, newQuorumNumerator);
}
Expand Down
18 changes: 6 additions & 12 deletions contracts/governance/utils/Votes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ abstract contract Votes is IVotes, Context, EIP712, Nonces {
* - `blockNumber` must have been already mined
*/
function getPastVotes(address account, uint256 blockNumber) public view virtual override returns (uint256) {
require(blockNumber < block.number, 'Votes: block not yet mined');
require(blockNumber < block.number, "Votes: block not yet mined");
uint32 key = SafeCast.toUint32(blockNumber);
return _delegateCheckpoints[account].upperLookup(key);
}
Expand All @@ -71,7 +71,7 @@ abstract contract Votes is IVotes, Context, EIP712, Nonces {
* - `blockNumber` must have been already mined
*/
function getPastTotalSupply(uint256 blockNumber) public view virtual override returns (uint256) {
require(blockNumber < block.number, 'Votes: block not yet mined');
require(blockNumber < block.number, "Votes: block not yet mined");
uint32 key = SafeCast.toUint32(blockNumber);
return _totalCheckpoints.upperLookup(key);
}
Expand Down Expand Up @@ -140,17 +140,11 @@ abstract contract Votes is IVotes, Context, EIP712, Nonces {
function _transferVotingUnits(address from, address to, uint256 amount) internal virtual {
if (from == address(0)) {
uint224 latest = _totalCheckpoints.latest();
_totalCheckpoints.push(
SafeCast.toUint32(block.timestamp),
latest + SafeCast.toUint32(amount)
);
_totalCheckpoints.push(SafeCast.toUint32(block.timestamp), latest + SafeCast.toUint32(amount));
}
if (to == address(0)) {
uint224 latest = _totalCheckpoints.latest();
_totalCheckpoints.push(
SafeCast.toUint32(block.timestamp),
latest - SafeCast.toUint32(amount)
);
_totalCheckpoints.push(SafeCast.toUint32(block.timestamp), latest - SafeCast.toUint32(amount));
}
_moveDelegateVotes(delegates(from), delegates(to), amount);
}
Expand All @@ -163,15 +157,15 @@ abstract contract Votes is IVotes, Context, EIP712, Nonces {
if (from != address(0)) {
uint224 latest = _totalCheckpoints.latest();
(uint256 oldValue, uint256 newValue) = _delegateCheckpoints[from].push(
SafeCast.toUint32(block.timestamp),
SafeCast.toUint32(block.timestamp),
latest - SafeCast.toUint32(amount)
);
emit DelegateVotesChanged(from, oldValue, newValue);
}
if (to != address(0)) {
uint224 latest = _totalCheckpoints.latest();
(uint256 oldValue, uint256 newValue) = _delegateCheckpoints[to].push(
SafeCast.toUint32(block.timestamp),
SafeCast.toUint32(block.timestamp),
latest + SafeCast.toUint32(amount)
);
emit DelegateVotesChanged(to, oldValue, newValue);
Expand Down
38 changes: 1 addition & 37 deletions scripts/generate/templates/Checkpoints.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,43 +62,7 @@ function upperLookup(${opts.historyTypeName} storage self, ${opts.keyTypeName} k
uint256 pos = _upperBinaryLookup(self.${opts.checkpointFieldName}, key, 0, len);
return pos == 0 ? 0 : _unsafeAccess(self.${opts.checkpointFieldName}, pos - 1).${opts.valueFieldName};
}
`;

const legacyOperations = opts => `\
/**
* @dev Returns checkpoint at given position.
*/
function getAtPosition(${opts.historyTypeName} storage self, uint32 pos) internal view returns (${opts.checkpointTypeName} memory) {
return self._checkpoints[pos];
}

/**
* @dev Pushes a value onto a History so that it is stored as the checkpoint for the current block.
*
* Returns previous value and new value.
*/
function push(${opts.historyTypeName} storage self, uint256 value) internal returns (uint256, uint256) {
return _insert(self.${opts.checkpointFieldName}, SafeCast.toUint32(block.number), SafeCast.toUint224(value));
}

/**
* @dev Pushes a value onto a History, by updating the latest value using binary operation \`op\`. The new value will
* be set to \`op(latest, delta)\`.
*
* Returns previous value and new value.
*/
function push(
${opts.historyTypeName} storage self,
function(uint256, uint256) view returns (uint256) op,
uint256 delta
) internal returns (uint256, uint256) {
uint32 key = SafeCast.toUint32(block.number);
${opts.valueTypeName} value = SafeCast.to${opts.valueTypeNameCap}(op(latest(self), delta));
return push(self, key, value);
}
`;

const common = opts => `\
/**
* @dev Returns the value in the most recent checkpoint, or zero if there are no checkpoints.
*/
Expand Down Expand Up @@ -249,7 +213,7 @@ module.exports = format(
'library Checkpoints {',
[
// New flavors
...OPTS.flatMap(opts => [types(opts), operations(opts), common(opts)]),
...OPTS.flatMap(opts => [types(opts), operations(opts)]),
],
'}',
);