GovernorERC721.test.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. const { expectEvent } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const Enums = require('../../helpers/enums');
  4. const { GovernorHelper } = require('../../helpers/governance');
  5. const Governor = artifacts.require('$GovernorVoteMocks');
  6. const CallReceiver = artifacts.require('CallReceiverMock');
  7. const TOKENS = [
  8. { Token: artifacts.require('$ERC721Votes'), mode: 'blocknumber' },
  9. { Token: artifacts.require('$ERC721VotesTimestampMock'), mode: 'timestamp' },
  10. ];
  11. contract('GovernorERC721', function (accounts) {
  12. const [owner, voter1, voter2, voter3, voter4] = accounts;
  13. const name = 'OZ-Governor';
  14. // const version = '1';
  15. const tokenName = 'MockNFToken';
  16. const tokenSymbol = 'MTKN';
  17. const NFT0 = web3.utils.toBN(0);
  18. const NFT1 = web3.utils.toBN(1);
  19. const NFT2 = web3.utils.toBN(2);
  20. const NFT3 = web3.utils.toBN(3);
  21. const NFT4 = web3.utils.toBN(4);
  22. const votingDelay = web3.utils.toBN(4);
  23. const votingPeriod = web3.utils.toBN(16);
  24. const value = web3.utils.toWei('1');
  25. for (const { mode, Token } of TOKENS) {
  26. describe(`using ${Token._json.contractName}`, function () {
  27. beforeEach(async function () {
  28. this.owner = owner;
  29. this.token = await Token.new(tokenName, tokenSymbol, tokenName, '1');
  30. this.mock = await Governor.new(name, this.token.address);
  31. this.receiver = await CallReceiver.new();
  32. this.helper = new GovernorHelper(this.mock, mode);
  33. await web3.eth.sendTransaction({ from: owner, to: this.mock.address, value });
  34. await Promise.all([NFT0, NFT1, NFT2, NFT3, NFT4].map(tokenId => this.token.$_mint(owner, tokenId)));
  35. await this.helper.delegate({ token: this.token, to: voter1, tokenId: NFT0 }, { from: owner });
  36. await this.helper.delegate({ token: this.token, to: voter2, tokenId: NFT1 }, { from: owner });
  37. await this.helper.delegate({ token: this.token, to: voter2, tokenId: NFT2 }, { from: owner });
  38. await this.helper.delegate({ token: this.token, to: voter3, tokenId: NFT3 }, { from: owner });
  39. await this.helper.delegate({ token: this.token, to: voter4, tokenId: NFT4 }, { from: owner });
  40. // default proposal
  41. this.proposal = this.helper.setProposal(
  42. [
  43. {
  44. target: this.receiver.address,
  45. value,
  46. data: this.receiver.contract.methods.mockFunction().encodeABI(),
  47. },
  48. ],
  49. '<proposal description>',
  50. );
  51. });
  52. it('deployment check', async function () {
  53. expect(await this.mock.name()).to.be.equal(name);
  54. expect(await this.mock.token()).to.be.equal(this.token.address);
  55. expect(await this.mock.votingDelay()).to.be.bignumber.equal(votingDelay);
  56. expect(await this.mock.votingPeriod()).to.be.bignumber.equal(votingPeriod);
  57. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  58. });
  59. it('voting with ERC721 token', async function () {
  60. await this.helper.propose();
  61. await this.helper.waitForSnapshot();
  62. expectEvent(await this.helper.vote({ support: Enums.VoteType.For }, { from: voter1 }), 'VoteCast', {
  63. voter: voter1,
  64. support: Enums.VoteType.For,
  65. weight: '1',
  66. });
  67. expectEvent(await this.helper.vote({ support: Enums.VoteType.For }, { from: voter2 }), 'VoteCast', {
  68. voter: voter2,
  69. support: Enums.VoteType.For,
  70. weight: '2',
  71. });
  72. expectEvent(await this.helper.vote({ support: Enums.VoteType.Against }, { from: voter3 }), 'VoteCast', {
  73. voter: voter3,
  74. support: Enums.VoteType.Against,
  75. weight: '1',
  76. });
  77. expectEvent(await this.helper.vote({ support: Enums.VoteType.Abstain }, { from: voter4 }), 'VoteCast', {
  78. voter: voter4,
  79. support: Enums.VoteType.Abstain,
  80. weight: '1',
  81. });
  82. await this.helper.waitForDeadline();
  83. await this.helper.execute();
  84. expect(await this.mock.hasVoted(this.proposal.id, owner)).to.be.equal(false);
  85. expect(await this.mock.hasVoted(this.proposal.id, voter1)).to.be.equal(true);
  86. expect(await this.mock.hasVoted(this.proposal.id, voter2)).to.be.equal(true);
  87. expect(await this.mock.hasVoted(this.proposal.id, voter3)).to.be.equal(true);
  88. expect(await this.mock.hasVoted(this.proposal.id, voter4)).to.be.equal(true);
  89. await this.mock.proposalVotes(this.proposal.id).then(results => {
  90. expect(results.forVotes).to.be.bignumber.equal('3');
  91. expect(results.againstVotes).to.be.bignumber.equal('1');
  92. expect(results.abstainVotes).to.be.bignumber.equal('1');
  93. });
  94. });
  95. });
  96. }
  97. });