GovernorERC721.test.js 4.1 KB

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