GovernorCountingFractional.test.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { GovernorHelper } = require('../../helpers/governance');
  5. const { VoteType } = require('../../helpers/enums');
  6. const { zip } = require('../../helpers/iterate');
  7. const { sum } = require('../../helpers/math');
  8. const TOKENS = [
  9. { Token: '$ERC20Votes', mode: 'blocknumber' },
  10. { Token: '$ERC20VotesTimestampMock', mode: 'timestamp' },
  11. ];
  12. const name = 'OZ-Governor';
  13. const version = '1';
  14. const tokenName = 'MockToken';
  15. const tokenSymbol = 'MTKN';
  16. const tokenSupply = ethers.parseEther('100');
  17. const votingDelay = 4n;
  18. const votingPeriod = 16n;
  19. const value = ethers.parseEther('1');
  20. describe('GovernorCountingFractional', function () {
  21. for (const { Token, mode } of TOKENS) {
  22. const fixture = async () => {
  23. const [owner, proposer, voter1, voter2, voter3, voter4, other] = await ethers.getSigners();
  24. const receiver = await ethers.deployContract('CallReceiverMock');
  25. const token = await ethers.deployContract(Token, [tokenName, tokenSymbol, version]);
  26. const mock = await ethers.deployContract('$GovernorFractionalMock', [
  27. name, // name
  28. votingDelay, // initialVotingDelay
  29. votingPeriod, // initialVotingPeriod
  30. 0n, // initialProposalThreshold
  31. token, // tokenAddress
  32. 10n, // quorumNumeratorValue
  33. ]);
  34. await owner.sendTransaction({ to: mock, value });
  35. await token.$_mint(owner, tokenSupply);
  36. const helper = new GovernorHelper(mock, mode);
  37. await helper.connect(owner).delegate({ token, to: voter1, value: ethers.parseEther('10') });
  38. await helper.connect(owner).delegate({ token, to: voter2, value: ethers.parseEther('7') });
  39. await helper.connect(owner).delegate({ token, to: voter3, value: ethers.parseEther('5') });
  40. await helper.connect(owner).delegate({ token, to: voter4, value: ethers.parseEther('2') });
  41. return { owner, proposer, voter1, voter2, voter3, voter4, other, receiver, token, mock, helper };
  42. };
  43. describe(`using ${Token}`, function () {
  44. beforeEach(async function () {
  45. Object.assign(this, await loadFixture(fixture));
  46. // default proposal
  47. this.proposal = this.helper.setProposal(
  48. [
  49. {
  50. target: this.receiver.target,
  51. value,
  52. data: this.receiver.interface.encodeFunctionData('mockFunction'),
  53. },
  54. ],
  55. '<proposal description>',
  56. );
  57. });
  58. it('deployment check', async function () {
  59. expect(await this.mock.name()).to.equal(name);
  60. expect(await this.mock.token()).to.equal(this.token);
  61. expect(await this.mock.votingDelay()).to.equal(votingDelay);
  62. expect(await this.mock.votingPeriod()).to.equal(votingPeriod);
  63. expect(await this.mock.COUNTING_MODE()).to.equal(
  64. 'support=bravo,fractional&quorum=for,abstain&params=fractional',
  65. );
  66. });
  67. it('nominal is unaffected', async function () {
  68. await this.helper.connect(this.proposer).propose();
  69. await this.helper.waitForSnapshot();
  70. await this.helper.connect(this.voter1).vote({ support: VoteType.For, reason: 'This is nice' });
  71. await this.helper.connect(this.voter2).vote({ support: VoteType.For });
  72. await this.helper.connect(this.voter3).vote({ support: VoteType.Against });
  73. await this.helper.connect(this.voter4).vote({ support: VoteType.Abstain });
  74. await this.helper.waitForDeadline();
  75. await this.helper.execute();
  76. expect(await this.mock.hasVoted(this.proposal.id, this.owner)).to.be.false;
  77. expect(await this.mock.hasVoted(this.proposal.id, this.voter1)).to.be.true;
  78. expect(await this.mock.hasVoted(this.proposal.id, this.voter2)).to.be.true;
  79. expect(await ethers.provider.getBalance(this.mock)).to.equal(0n);
  80. expect(await ethers.provider.getBalance(this.receiver)).to.equal(value);
  81. });
  82. describe('voting with a fraction of the weight', function () {
  83. it('twice', async function () {
  84. await this.helper.connect(this.proposer).propose();
  85. await this.helper.waitForSnapshot();
  86. expect(await this.mock.proposalVotes(this.proposal.id)).to.deep.equal([0n, 0n, 0n]);
  87. expect(await this.mock.hasVoted(this.proposal.id, this.voter2)).to.equal(false);
  88. expect(await this.mock.usedVotes(this.proposal.id, this.voter2)).to.equal(0n);
  89. const steps = [
  90. ['0', '2', '1'],
  91. ['1', '0', '1'],
  92. ].map(votes => votes.map(vote => ethers.parseEther(vote)));
  93. for (const votes of steps) {
  94. const params = ethers.solidityPacked(['uint128', 'uint128', 'uint128'], votes);
  95. await expect(
  96. this.helper.connect(this.voter2).vote({
  97. support: VoteType.Parameters,
  98. reason: 'no particular reason',
  99. params,
  100. }),
  101. )
  102. .to.emit(this.mock, 'VoteCastWithParams')
  103. .withArgs(
  104. this.voter2,
  105. this.proposal.id,
  106. VoteType.Parameters,
  107. sum(...votes),
  108. 'no particular reason',
  109. params,
  110. );
  111. }
  112. expect(await this.mock.proposalVotes(this.proposal.id)).to.deep.equal(zip(...steps).map(v => sum(...v)));
  113. expect(await this.mock.hasVoted(this.proposal.id, this.voter2)).to.equal(true);
  114. expect(await this.mock.usedVotes(this.proposal.id, this.voter2)).to.equal(sum(...[].concat(...steps)));
  115. });
  116. it('fractional then nominal', async function () {
  117. await this.helper.connect(this.proposer).propose();
  118. await this.helper.waitForSnapshot();
  119. expect(await this.mock.proposalVotes(this.proposal.id)).to.deep.equal([0n, 0n, 0n]);
  120. expect(await this.mock.hasVoted(this.proposal.id, this.voter2)).to.equal(false);
  121. expect(await this.mock.usedVotes(this.proposal.id, this.voter2)).to.equal(0n);
  122. const weight = ethers.parseEther('7');
  123. const fractional = ['1', '2', '1'].map(ethers.parseEther);
  124. const params = ethers.solidityPacked(['uint128', 'uint128', 'uint128'], fractional);
  125. await expect(
  126. this.helper.connect(this.voter2).vote({
  127. support: VoteType.Parameters,
  128. reason: 'no particular reason',
  129. params,
  130. }),
  131. )
  132. .to.emit(this.mock, 'VoteCastWithParams')
  133. .withArgs(
  134. this.voter2,
  135. this.proposal.id,
  136. VoteType.Parameters,
  137. sum(...fractional),
  138. 'no particular reason',
  139. params,
  140. );
  141. await expect(this.helper.connect(this.voter2).vote({ support: VoteType.Against }))
  142. .to.emit(this.mock, 'VoteCast')
  143. .withArgs(this.voter2, this.proposal.id, VoteType.Against, weight - sum(...fractional), '');
  144. expect(await this.mock.proposalVotes(this.proposal.id)).to.deep.equal([
  145. weight - sum(...fractional.slice(1)),
  146. ...fractional.slice(1),
  147. ]);
  148. expect(await this.mock.hasVoted(this.proposal.id, this.voter2)).to.equal(true);
  149. expect(await this.mock.usedVotes(this.proposal.id, this.voter2)).to.equal(weight);
  150. });
  151. it('revert if params spend more than available', async function () {
  152. await this.helper.connect(this.proposer).propose();
  153. await this.helper.waitForSnapshot();
  154. const weight = ethers.parseEther('7');
  155. const fractional = ['0', '1000', '0'].map(ethers.parseEther);
  156. await expect(
  157. this.helper.connect(this.voter2).vote({
  158. support: VoteType.Parameters,
  159. reason: 'no particular reason',
  160. params: ethers.solidityPacked(['uint128', 'uint128', 'uint128'], fractional),
  161. }),
  162. )
  163. .to.be.revertedWithCustomError(this.mock, 'GovernorExceedRemainingWeight')
  164. .withArgs(this.voter2, sum(...fractional), weight);
  165. });
  166. it('revert if no weight remaining', async function () {
  167. await this.helper.connect(this.proposer).propose();
  168. await this.helper.waitForSnapshot();
  169. await this.helper.connect(this.voter2).vote({ support: VoteType.For });
  170. await expect(
  171. this.helper.connect(this.voter2).vote({
  172. support: VoteType.Parameters,
  173. reason: 'no particular reason',
  174. params: ethers.solidityPacked(['uint128', 'uint128', 'uint128'], [0n, 1n, 0n]),
  175. }),
  176. )
  177. .to.be.revertedWithCustomError(this.mock, 'GovernorAlreadyCastVote')
  178. .withArgs(this.voter2);
  179. });
  180. it('revert if params are not properly formatted #1', async function () {
  181. await this.helper.connect(this.proposer).propose();
  182. await this.helper.waitForSnapshot();
  183. await expect(
  184. this.helper.connect(this.voter2).vote({
  185. support: VoteType.Parameters,
  186. reason: 'no particular reason',
  187. params: ethers.solidityPacked(['uint128', 'uint128'], [0n, 1n]),
  188. }),
  189. ).to.be.revertedWithCustomError(this.mock, 'GovernorInvalidVoteParams');
  190. });
  191. it('revert if params are not properly formatted #2', async function () {
  192. await this.helper.connect(this.proposer).propose();
  193. await this.helper.waitForSnapshot();
  194. await expect(
  195. this.helper.connect(this.voter2).vote({
  196. support: VoteType.Against,
  197. reason: 'no particular reason',
  198. params: ethers.solidityPacked(['uint128', 'uint128', 'uint128'], [0n, 1n, 0n]),
  199. }),
  200. ).to.be.revertedWithCustomError(this.mock, 'GovernorInvalidVoteParams');
  201. });
  202. it('revert if vote type is invalid', async function () {
  203. await this.helper.connect(this.proposer).propose();
  204. await this.helper.waitForSnapshot();
  205. await expect(this.helper.connect(this.voter2).vote({ support: 128n })).to.be.revertedWithCustomError(
  206. this.mock,
  207. 'GovernorInvalidVoteType',
  208. );
  209. });
  210. });
  211. });
  212. }
  213. });