GovernorERC721.test.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 TOKENS = [
  7. { Token: '$ERC721Votes', mode: 'blocknumber' },
  8. { Token: '$ERC721VotesTimestampMock', mode: 'timestamp' },
  9. ];
  10. const name = 'OZ-Governor';
  11. const version = '1';
  12. const tokenName = 'MockNFToken';
  13. const tokenSymbol = 'MTKN';
  14. const NFT0 = 0n;
  15. const NFT1 = 1n;
  16. const NFT2 = 2n;
  17. const NFT3 = 3n;
  18. const NFT4 = 4n;
  19. const votingDelay = 4n;
  20. const votingPeriod = 16n;
  21. const value = ethers.parseEther('1');
  22. describe('GovernorERC721', function () {
  23. for (const { Token, mode } of TOKENS) {
  24. const fixture = async () => {
  25. const [owner, voter1, voter2, voter3, voter4] = await ethers.getSigners();
  26. const receiver = await ethers.deployContract('CallReceiverMock');
  27. const token = await ethers.deployContract(Token, [tokenName, tokenSymbol, tokenName, version]);
  28. const mock = await ethers.deployContract('$GovernorMock', [
  29. name, // name
  30. votingDelay, // initialVotingDelay
  31. votingPeriod, // initialVotingPeriod
  32. 0n, // initialProposalThreshold
  33. token, // tokenAddress
  34. 10n, // quorumNumeratorValue
  35. ]);
  36. await owner.sendTransaction({ to: mock, value });
  37. await Promise.all([NFT0, NFT1, NFT2, NFT3, NFT4].map(tokenId => token.$_mint(owner, tokenId)));
  38. const helper = new GovernorHelper(mock, mode);
  39. await helper.connect(owner).delegate({ token, to: voter1, tokenId: NFT0 });
  40. await helper.connect(owner).delegate({ token, to: voter2, tokenId: NFT1 });
  41. await helper.connect(owner).delegate({ token, to: voter2, tokenId: NFT2 });
  42. await helper.connect(owner).delegate({ token, to: voter3, tokenId: NFT3 });
  43. await helper.connect(owner).delegate({ token, to: voter4, tokenId: NFT4 });
  44. return {
  45. owner,
  46. voter1,
  47. voter2,
  48. voter3,
  49. voter4,
  50. receiver,
  51. token,
  52. mock,
  53. helper,
  54. };
  55. };
  56. describe(`using ${Token}`, function () {
  57. beforeEach(async function () {
  58. Object.assign(this, await loadFixture(fixture));
  59. // initiate fresh proposal
  60. this.proposal = this.helper.setProposal(
  61. [
  62. {
  63. target: this.receiver.target,
  64. data: this.receiver.interface.encodeFunctionData('mockFunction'),
  65. value,
  66. },
  67. ],
  68. '<proposal description>',
  69. );
  70. });
  71. it('deployment check', async function () {
  72. expect(await this.mock.name()).to.equal(name);
  73. expect(await this.mock.token()).to.equal(this.token);
  74. expect(await this.mock.votingDelay()).to.equal(votingDelay);
  75. expect(await this.mock.votingPeriod()).to.equal(votingPeriod);
  76. expect(await this.mock.quorum(0n)).to.equal(0n);
  77. expect(await this.token.getVotes(this.voter1)).to.equal(1n); // NFT0
  78. expect(await this.token.getVotes(this.voter2)).to.equal(2n); // NFT1 & NFT2
  79. expect(await this.token.getVotes(this.voter3)).to.equal(1n); // NFT3
  80. expect(await this.token.getVotes(this.voter4)).to.equal(1n); // NFT4
  81. });
  82. it('voting with ERC721 token', async function () {
  83. await this.helper.propose();
  84. await this.helper.waitForSnapshot();
  85. await expect(this.helper.connect(this.voter1).vote({ support: VoteType.For }))
  86. .to.emit(this.mock, 'VoteCast')
  87. .withArgs(this.voter1, this.proposal.id, VoteType.For, 1n, '');
  88. await expect(this.helper.connect(this.voter2).vote({ support: VoteType.For }))
  89. .to.emit(this.mock, 'VoteCast')
  90. .withArgs(this.voter2, this.proposal.id, VoteType.For, 2n, '');
  91. await expect(this.helper.connect(this.voter3).vote({ support: VoteType.Against }))
  92. .to.emit(this.mock, 'VoteCast')
  93. .withArgs(this.voter3, this.proposal.id, VoteType.Against, 1n, '');
  94. await expect(this.helper.connect(this.voter4).vote({ support: VoteType.Abstain }))
  95. .to.emit(this.mock, 'VoteCast')
  96. .withArgs(this.voter4, this.proposal.id, VoteType.Abstain, 1n, '');
  97. await this.helper.waitForDeadline();
  98. await this.helper.execute();
  99. expect(await this.mock.hasVoted(this.proposal.id, this.owner)).to.be.false;
  100. expect(await this.mock.hasVoted(this.proposal.id, this.voter1)).to.be.true;
  101. expect(await this.mock.hasVoted(this.proposal.id, this.voter2)).to.be.true;
  102. expect(await this.mock.hasVoted(this.proposal.id, this.voter3)).to.be.true;
  103. expect(await this.mock.hasVoted(this.proposal.id, this.voter4)).to.be.true;
  104. expect(await this.mock.proposalVotes(this.proposal.id)).to.deep.equal([
  105. 1n, // againstVotes
  106. 3n, // forVotes
  107. 1n, // abstainVotes
  108. ]);
  109. });
  110. });
  111. }
  112. });