Skip to content

Commit 3902a41

Browse files
frangioqiweiiiAmxx
authored
Remove DOMAIN_SEPARATOR from Votes and update docs examples (OpenZeppelin#4297)
Co-authored-by: Qiwei Yang <yangqiwei97@gmail.com> Co-authored-by: Hadrien Croubois <hadrien.croubois@gmail.com>
1 parent 5cef83d commit 3902a41

File tree

12 files changed

+186
-224
lines changed

12 files changed

+186
-224
lines changed

.changeset/silly-bees-beam.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@
22
'openzeppelin-solidity': major
33
---
44

5-
`ERC20Votes`: Changed internal vote accounting to reusable `Votes` module previously used by `ERC721Votes`. Removed implicit `ERC20Permit` inheritance. [#3816](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/3816)
5+
`ERC20Votes`: Changed internal vote accounting to reusable `Votes` module previously used by `ERC721Votes`. Removed implicit `ERC20Permit` inheritance. Note that the `DOMAIN_SEPARATOR` getter was previously guaranteed to be available for `ERC20Votes` contracts, but is no longer available unless `ERC20Permit` is explicitly used; ERC-5267 support is included in `ERC20Votes` with `EIP712` and is recommended as an alternative.
6+
7+
pr: #3816

.github/workflows/checks.yml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ concurrency:
1313
group: checks-${{ github.ref }}
1414
cancel-in-progress: true
1515

16+
env:
17+
NODE_OPTIONS: --max_old_space_size=5120
18+
1619
jobs:
1720
lint:
1821
runs-on: ubuntu-latest
@@ -26,7 +29,6 @@ jobs:
2629
runs-on: ubuntu-latest
2730
env:
2831
FORCE_COLOR: 1
29-
NODE_OPTIONS: --max_old_space_size=4096
3032
GAS: true
3133
steps:
3234
- uses: actions/checkout@v3
@@ -57,8 +59,6 @@ jobs:
5759
run: bash scripts/upgradeable/transpile.sh
5860
- name: Run tests
5961
run: npm run test
60-
env:
61-
NODE_OPTIONS: --max_old_space_size=4096
6262
- name: Check linearisation of the inheritance graph
6363
run: npm run test:inheritance
6464
- name: Check storage layout
@@ -86,8 +86,6 @@ jobs:
8686
- name: Set up environment
8787
uses: ./.github/actions/setup
8888
- run: npm run coverage
89-
env:
90-
NODE_OPTIONS: --max_old_space_size=4096
9189
- uses: codecov/codecov-action@v3
9290
with:
9391
token: ${{ secrets.CODECOV_TOKEN }}

contracts/governance/utils/Votes.sol

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -225,14 +225,6 @@ abstract contract Votes is Context, EIP712, Nonces, IERC5805 {
225225
return a - b;
226226
}
227227

228-
/**
229-
* @dev Returns the contract's {EIP712} domain separator.
230-
*/
231-
// solhint-disable-next-line func-name-mixedcase
232-
function DOMAIN_SEPARATOR() external view returns (bytes32) {
233-
return _domainSeparatorV4();
234-
}
235-
236228
/**
237229
* @dev Must return the voting units held by an account.
238230
*/
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.19;
3+
4+
import "../../../governance/Governor.sol";
5+
import "../../../governance/compatibility/GovernorCompatibilityBravo.sol";
6+
import "../../../governance/extensions/GovernorVotes.sol";
7+
import "../../../governance/extensions/GovernorVotesQuorumFraction.sol";
8+
import "../../../governance/extensions/GovernorTimelockControl.sol";
9+
10+
contract MyGovernor is
11+
Governor,
12+
GovernorCompatibilityBravo,
13+
GovernorVotes,
14+
GovernorVotesQuorumFraction,
15+
GovernorTimelockControl
16+
{
17+
constructor(
18+
IVotes _token,
19+
TimelockController _timelock
20+
) Governor("MyGovernor") GovernorVotes(_token) GovernorVotesQuorumFraction(4) GovernorTimelockControl(_timelock) {}
21+
22+
function votingDelay() public pure override returns (uint256) {
23+
return 7200; // 1 day
24+
}
25+
26+
function votingPeriod() public pure override returns (uint256) {
27+
return 50400; // 1 week
28+
}
29+
30+
function proposalThreshold() public pure override returns (uint256) {
31+
return 0;
32+
}
33+
34+
// The functions below are overrides required by Solidity.
35+
36+
function state(
37+
uint256 proposalId
38+
) public view override(Governor, IGovernor, GovernorTimelockControl) returns (ProposalState) {
39+
return super.state(proposalId);
40+
}
41+
42+
function propose(
43+
address[] memory targets,
44+
uint256[] memory values,
45+
bytes[] memory calldatas,
46+
string memory description
47+
) public override(Governor, GovernorCompatibilityBravo, IGovernor) returns (uint256) {
48+
return super.propose(targets, values, calldatas, description);
49+
}
50+
51+
function cancel(
52+
address[] memory targets,
53+
uint256[] memory values,
54+
bytes[] memory calldatas,
55+
bytes32 descriptionHash
56+
) public override(Governor, GovernorCompatibilityBravo, IGovernor) returns (uint256) {
57+
return super.cancel(targets, values, calldatas, descriptionHash);
58+
}
59+
60+
function _execute(
61+
uint256 proposalId,
62+
address[] memory targets,
63+
uint256[] memory values,
64+
bytes[] memory calldatas,
65+
bytes32 descriptionHash
66+
) internal override(Governor, GovernorTimelockControl) {
67+
super._execute(proposalId, targets, values, calldatas, descriptionHash);
68+
}
69+
70+
function _cancel(
71+
address[] memory targets,
72+
uint256[] memory values,
73+
bytes[] memory calldatas,
74+
bytes32 descriptionHash
75+
) internal override(Governor, GovernorTimelockControl) returns (uint256) {
76+
return super._cancel(targets, values, calldatas, descriptionHash);
77+
}
78+
79+
function _executor() internal view override(Governor, GovernorTimelockControl) returns (address) {
80+
return super._executor();
81+
}
82+
83+
function supportsInterface(
84+
bytes4 interfaceId
85+
) public view override(Governor, IERC165, GovernorTimelockControl) returns (bool) {
86+
return super.supportsInterface(interfaceId);
87+
}
88+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.19;
3+
4+
import "../../../token/ERC20/ERC20.sol";
5+
import "../../../token/ERC20/extensions/ERC20Permit.sol";
6+
import "../../../token/ERC20/extensions/ERC20Votes.sol";
7+
8+
contract MyToken is ERC20, ERC20Permit, ERC20Votes {
9+
constructor() ERC20("MyToken", "MTK") ERC20Permit("MyToken") {}
10+
11+
// The functions below are overrides required by Solidity.
12+
13+
function _update(address from, address to, uint256 amount) internal override(ERC20, ERC20Votes) {
14+
super._update(from, to, amount);
15+
}
16+
17+
function nonces(address owner) public view virtual override(ERC20Permit, Nonces) returns (uint256) {
18+
return super.nonces(owner);
19+
}
20+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.19;
3+
4+
import "../../../token/ERC20/ERC20.sol";
5+
import "../../../token/ERC20/extensions/ERC20Permit.sol";
6+
import "../../../token/ERC20/extensions/ERC20Votes.sol";
7+
8+
contract MyTokenTimestampBased is ERC20, ERC20Permit, ERC20Votes {
9+
constructor() ERC20("MyTokenTimestampBased", "MTK") ERC20Permit("MyTokenTimestampBased") {}
10+
11+
// Overrides IERC6372 functions to make the token & governor timestamp-based
12+
13+
function clock() public view override returns (uint48) {
14+
return uint48(block.timestamp);
15+
}
16+
17+
// solhint-disable-next-line func-name-mixedcase
18+
function CLOCK_MODE() public pure override returns (string memory) {
19+
return "mode=timestamp";
20+
}
21+
22+
// The functions below are overrides required by Solidity.
23+
24+
function _update(address from, address to, uint256 amount) internal override(ERC20, ERC20Votes) {
25+
super._update(from, to, amount);
26+
}
27+
28+
function nonces(address owner) public view virtual override(ERC20Permit, Nonces) returns (uint256) {
29+
return super.nonces(owner);
30+
}
31+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.19;
3+
4+
import "../../../token/ERC20/ERC20.sol";
5+
import "../../../token/ERC20/extensions/ERC20Permit.sol";
6+
import "../../../token/ERC20/extensions/ERC20Votes.sol";
7+
import "../../../token/ERC20/extensions/ERC20Wrapper.sol";
8+
9+
contract MyTokenWrapped is ERC20, ERC20Permit, ERC20Votes, ERC20Wrapper {
10+
constructor(
11+
IERC20 wrappedToken
12+
) ERC20("MyTokenWrapped", "MTK") ERC20Permit("MyTokenWrapped") ERC20Wrapper(wrappedToken) {}
13+
14+
// The functions below are overrides required by Solidity.
15+
16+
function decimals() public view override(ERC20, ERC20Wrapper) returns (uint8) {
17+
return super.decimals();
18+
}
19+
20+
function _update(address from, address to, uint256 amount) internal override(ERC20, ERC20Votes) {
21+
super._update(from, to, amount);
22+
}
23+
24+
function nonces(address owner) public view virtual override(ERC20Permit, Nonces) returns (uint256) {
25+
return super.nonces(owner);
26+
}
27+
}

contracts/token/ERC20/extensions/ERC20Permit.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712, Nonces {
6666
* @dev See {IERC20Permit-DOMAIN_SEPARATOR}.
6767
*/
6868
// solhint-disable-next-line func-name-mixedcase
69-
function DOMAIN_SEPARATOR() external view override returns (bytes32) {
69+
function DOMAIN_SEPARATOR() external view virtual override returns (bytes32) {
7070
return _domainSeparatorV4();
7171
}
7272
}

0 commit comments

Comments
 (0)