GovernorProposalGuardian.test.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. const { ethers } = require('hardhat');
  2. const { expect } = require('chai');
  3. const { loadFixture } = require('@nomicfoundation/hardhat-network-helpers');
  4. const { impersonate } = require('../../helpers/account');
  5. const { GovernorHelper } = require('../../helpers/governance');
  6. const { ProposalState } = require('../../helpers/enums');
  7. const TOKENS = [
  8. { Token: '$ERC20Votes', mode: 'blocknumber' },
  9. { Token: '$ERC20VotesTimestampMock', mode: 'timestamp' },
  10. ];
  11. const name = 'Proposal Guardian Governor';
  12. const version = '1';
  13. const tokenName = 'MockToken';
  14. const tokenSymbol = 'MTKN';
  15. const tokenSupply = ethers.parseEther('100');
  16. const votingDelay = 4n;
  17. const votingPeriod = 16n;
  18. const value = ethers.parseEther('1');
  19. describe('GovernorProposalGuardian', function () {
  20. for (const { Token, mode } of TOKENS) {
  21. const fixture = async () => {
  22. const [owner, proposer, guardian, voter1, voter2, voter3, voter4, other] = await ethers.getSigners();
  23. const receiver = await ethers.deployContract('CallReceiverMock');
  24. const token = await ethers.deployContract(Token, [tokenName, tokenSymbol, tokenName, version]);
  25. const mock = await ethers.deployContract('$GovernorProposalGuardianMock', [
  26. name, // name
  27. votingDelay, // initialVotingDelay
  28. votingPeriod, // initialVotingPeriod
  29. 0n, // initialProposalThreshold
  30. token, // tokenAddress
  31. 10n, // quorumNumeratorValue
  32. ]);
  33. await impersonate(mock.target);
  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, guardian, 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. await expect(this.mock.name()).to.eventually.equal(name);
  60. await expect(this.mock.token()).to.eventually.equal(this.token);
  61. await expect(this.mock.votingDelay()).to.eventually.equal(votingDelay);
  62. await expect(this.mock.votingPeriod()).to.eventually.equal(votingPeriod);
  63. });
  64. describe('set proposal guardian', function () {
  65. it('from governance', async function () {
  66. const governorSigner = await ethers.getSigner(this.mock.target);
  67. await expect(this.mock.connect(governorSigner).setProposalGuardian(this.guardian))
  68. .to.emit(this.mock, 'ProposalGuardianSet')
  69. .withArgs(ethers.ZeroAddress, this.guardian);
  70. await expect(this.mock.proposalGuardian()).to.eventually.equal(this.guardian);
  71. });
  72. it('from non-governance', async function () {
  73. await expect(this.mock.connect(this.other).setProposalGuardian(this.guardian))
  74. .to.be.revertedWithCustomError(this.mock, 'GovernorOnlyExecutor')
  75. .withArgs(this.other);
  76. });
  77. });
  78. it('cancel proposal during pending state from proposer when proposal guardian is non-zero', async function () {
  79. await this.mock.$_setProposalGuardian(this.guardian);
  80. await this.helper.connect(this.proposer).propose();
  81. await expect(this.helper.connect(this.proposer).cancel())
  82. .to.emit(this.mock, 'ProposalCanceled')
  83. .withArgs(this.proposal.id);
  84. });
  85. describe('cancel proposal during active state', function () {
  86. beforeEach(async function () {
  87. await this.helper.connect(this.proposer).propose();
  88. await this.helper.waitForSnapshot(1n);
  89. await expect(this.mock.state(this.proposal.id)).to.eventually.equal(ProposalState.Active);
  90. });
  91. it('from proposal guardian', async function () {
  92. await this.mock.$_setProposalGuardian(this.guardian);
  93. await expect(this.helper.connect(this.guardian).cancel())
  94. .to.emit(this.mock, 'ProposalCanceled')
  95. .withArgs(this.proposal.id);
  96. });
  97. it('from proposer when proposal guardian is non-zero', async function () {
  98. await this.mock.$_setProposalGuardian(this.guardian);
  99. await expect(this.helper.connect(this.proposer).cancel())
  100. .to.be.revertedWithCustomError(this.mock, 'GovernorUnableToCancel')
  101. .withArgs(this.proposal.id, this.proposer);
  102. });
  103. it('from proposer when proposal guardian is zero', async function () {
  104. await this.mock.$_setProposalGuardian(ethers.ZeroAddress);
  105. await expect(this.helper.connect(this.proposer).cancel())
  106. .to.emit(this.mock, 'ProposalCanceled')
  107. .withArgs(this.proposal.id);
  108. });
  109. });
  110. });
  111. }
  112. });