Skip to content

Commit

Permalink
Extend parameterized Governor customization to include vote weight ca…
Browse files Browse the repository at this point in the history
…lculation
  • Loading branch information
Amxx authored and apbendi committed Jan 17, 2022
1 parent f41a8b6 commit 7a33925
Show file tree
Hide file tree
Showing 12 changed files with 29 additions and 92 deletions.
18 changes: 17 additions & 1 deletion contracts/governance/Governor.sol
Expand Up @@ -179,6 +179,15 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
*/
function _voteSucceeded(uint256 proposalId) internal view virtual returns (bool);

/**
* @dev Check the voting weight of an account.
*/
function _getVotes(
address account,
uint256 blockNumber,
bytes memory params
) internal view virtual returns (uint256);

/**
* @dev Register a vote with a given support and voting weight.
*
Expand Down Expand Up @@ -314,6 +323,13 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
return proposalId;
}

/**
* @dev See {IGovernor-getVotes}.
*/
function getVotes(address account, uint256 blockNumber) public view virtual override returns (uint256) {
return _getVotes(account, blockNumber, _defaultParams());
}

/**
* @dev See {IGovernor-castVote}.
*/
Expand Down Expand Up @@ -381,7 +397,7 @@ abstract contract Governor is Context, ERC165, EIP712, IGovernor {
ProposalCore storage proposal = _proposals[proposalId];
require(state(proposalId) == ProposalState.Active, "Governor: vote not currently active");

uint256 weight = getVotes(account, proposal.voteStart.getDeadline());
uint256 weight = _getVotes(account, proposal.voteStart.getDeadline(), params);
_countVote(proposalId, account, support, weight, params);

emit VoteCast(account, proposalId, support, weight, reason);
Expand Down
8 changes: 6 additions & 2 deletions contracts/governance/extensions/GovernorVotes.sol
Expand Up @@ -19,9 +19,13 @@ abstract contract GovernorVotes is Governor {
}

/**
* Read the voting weight from the token's built in snapshot mechanism (see {IGovernor-getVotes}).
* Read the voting weight from the token's built in snapshot mechanism (see {Governor-_getVotes}).
*/
function getVotes(address account, uint256 blockNumber) public view virtual override returns (uint256) {
function _getVotes(
address account,
uint256 blockNumber,
bytes memory /*params*/
) internal view virtual override returns (uint256) {
return token.getPastVotes(account, blockNumber);
}
}
8 changes: 6 additions & 2 deletions contracts/governance/extensions/GovernorVotesComp.sol
Expand Up @@ -19,9 +19,13 @@ abstract contract GovernorVotesComp is Governor {
}

/**
* Read the voting weight from the token's built in snapshot mechanism (see {IGovernor-getVotes}).
* Read the voting weight from the token's built in snapshot mechanism (see {Governor-_getVotes}).
*/
function getVotes(address account, uint256 blockNumber) public view virtual override returns (uint256) {
function _getVotes(
address account,
uint256 blockNumber,
bytes memory /*params*/
) internal view virtual override returns (uint256) {
return token.getPriorVotes(account, blockNumber);
}
}
10 changes: 0 additions & 10 deletions contracts/mocks/GovernorCompMock.sol
Expand Up @@ -28,14 +28,4 @@ contract GovernorCompMock is GovernorVotesComp, GovernorCountingSimple {
) public returns (uint256 proposalId) {
return _cancel(targets, values, calldatas, salt);
}

function getVotes(address account, uint256 blockNumber)
public
view
virtual
override(IGovernor, GovernorVotesComp)
returns (uint256)
{
return super.getVotes(account, blockNumber);
}
}
10 changes: 0 additions & 10 deletions contracts/mocks/GovernorCompatibilityBravoMock.sol
Expand Up @@ -124,16 +124,6 @@ contract GovernorCompatibilityBravoMock is
return super._cancel(targets, values, calldatas, salt);
}

function getVotes(address account, uint256 blockNumber)
public
view
virtual
override(IGovernor, GovernorVotesComp)
returns (uint256)
{
return super.getVotes(account, blockNumber);
}

function _executor() internal view virtual override(Governor, GovernorTimelockCompound) returns (address) {
return super._executor();
}
Expand Down
10 changes: 0 additions & 10 deletions contracts/mocks/GovernorMock.sol
Expand Up @@ -35,16 +35,6 @@ contract GovernorMock is
return _cancel(targets, values, calldatas, salt);
}

function getVotes(address account, uint256 blockNumber)
public
view
virtual
override(IGovernor, GovernorVotes)
returns (uint256)
{
return super.getVotes(account, blockNumber);
}

function proposalThreshold() public view override(Governor, GovernorSettings) returns (uint256) {
return super.proposalThreshold();
}
Expand Down
10 changes: 0 additions & 10 deletions contracts/mocks/GovernorTimelockCompoundMock.sol
Expand Up @@ -92,16 +92,6 @@ contract GovernorTimelockCompoundMock is
return super._cancel(targets, values, calldatas, salt);
}

function getVotes(address account, uint256 blockNumber)
public
view
virtual
override(IGovernor, GovernorVotes)
returns (uint256)
{
return super.getVotes(account, blockNumber);
}

function _executor() internal view virtual override(Governor, GovernorTimelockCompound) returns (address) {
return super._executor();
}
Expand Down
10 changes: 0 additions & 10 deletions contracts/mocks/GovernorTimelockControlMock.sol
Expand Up @@ -92,16 +92,6 @@ contract GovernorTimelockControlMock is
return super._cancel(targets, values, calldatas, descriptionHash);
}

function getVotes(address account, uint256 blockNumber)
public
view
virtual
override(IGovernor, GovernorVotes)
returns (uint256)
{
return super.getVotes(account, blockNumber);
}

function _executor() internal view virtual override(Governor, GovernorTimelockControl) returns (address) {
return super._executor();
}
Expand Down
10 changes: 0 additions & 10 deletions contracts/mocks/GovernorVoteMock.sol
Expand Up @@ -28,14 +28,4 @@ contract GovernorVoteMocks is GovernorVotes, GovernorCountingSimple {
) public returns (uint256 proposalId) {
return _cancel(targets, values, calldatas, salt);
}

function getVotes(address account, uint256 blockNumber)
public
view
virtual
override(IGovernor, GovernorVotes)
returns (uint256)
{
return super.getVotes(account, blockNumber);
}
}
9 changes: 0 additions & 9 deletions contracts/mocks/wizard/MyGovernor1.sol
Expand Up @@ -40,15 +40,6 @@ contract MyGovernor1 is
return super.quorum(blockNumber);
}

function getVotes(address account, uint256 blockNumber)
public
view
override(IGovernor, GovernorVotes)
returns (uint256)
{
return super.getVotes(account, blockNumber);
}

function state(uint256 proposalId) public view override(Governor, GovernorTimelockControl) returns (ProposalState) {
return super.state(proposalId);
}
Expand Down
9 changes: 0 additions & 9 deletions contracts/mocks/wizard/MyGovernor2.sol
Expand Up @@ -46,15 +46,6 @@ contract MyGovernor2 is
return super.quorum(blockNumber);
}

function getVotes(address account, uint256 blockNumber)
public
view
override(IGovernor, GovernorVotes)
returns (uint256)
{
return super.getVotes(account, blockNumber);
}

function state(uint256 proposalId) public view override(Governor, GovernorTimelockControl) returns (ProposalState) {
return super.state(proposalId);
}
Expand Down
9 changes: 0 additions & 9 deletions contracts/mocks/wizard/MyGovernor3.sol
Expand Up @@ -44,15 +44,6 @@ contract MyGovernor is
return super.quorum(blockNumber);
}

function getVotes(address account, uint256 blockNumber)
public
view
override(IGovernor, GovernorVotes)
returns (uint256)
{
return super.getVotes(account, blockNumber);
}

function state(uint256 proposalId)
public
view
Expand Down

0 comments on commit 7a33925

Please sign in to comment.