GovernorComp.test.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const { BN, expectEvent } = require('@openzeppelin/test-helpers');
  2. const Enums = require('../../helpers/enums');
  3. const {
  4. runGovernorWorkflow,
  5. } = require('./../GovernorWorkflow.behavior');
  6. const Token = artifacts.require('ERC20VotesCompMock');
  7. const Governor = artifacts.require('GovernorCompMock');
  8. const CallReceiver = artifacts.require('CallReceiverMock');
  9. contract('GovernorComp', function (accounts) {
  10. const [ owner, voter1, voter2, voter3, voter4 ] = accounts;
  11. const name = 'OZ-Governor';
  12. // const version = '1';
  13. const tokenName = 'MockToken';
  14. const tokenSymbol = 'MTKN';
  15. const tokenSupply = web3.utils.toWei('100');
  16. beforeEach(async function () {
  17. this.owner = owner;
  18. this.token = await Token.new(tokenName, tokenSymbol);
  19. this.mock = await Governor.new(name, this.token.address, 4, 16);
  20. this.receiver = await CallReceiver.new();
  21. await this.token.mint(owner, tokenSupply);
  22. await this.token.delegate(voter1, { from: voter1 });
  23. await this.token.delegate(voter2, { from: voter2 });
  24. await this.token.delegate(voter3, { from: voter3 });
  25. await this.token.delegate(voter4, { from: voter4 });
  26. });
  27. it('deployment check', async function () {
  28. expect(await this.mock.name()).to.be.equal(name);
  29. expect(await this.mock.token()).to.be.equal(this.token.address);
  30. expect(await this.mock.votingDelay()).to.be.bignumber.equal('4');
  31. expect(await this.mock.votingPeriod()).to.be.bignumber.equal('16');
  32. expect(await this.mock.quorum(0)).to.be.bignumber.equal('0');
  33. });
  34. describe('voting with comp token', function () {
  35. beforeEach(async function () {
  36. this.settings = {
  37. proposal: [
  38. [ this.receiver.address ],
  39. [ web3.utils.toWei('0') ],
  40. [ this.receiver.contract.methods.mockFunction().encodeABI() ],
  41. '<proposal description>',
  42. ],
  43. tokenHolder: owner,
  44. voters: [
  45. { voter: voter1, weight: web3.utils.toWei('1'), support: Enums.VoteType.For },
  46. { voter: voter2, weight: web3.utils.toWei('10'), support: Enums.VoteType.For },
  47. { voter: voter3, weight: web3.utils.toWei('5'), support: Enums.VoteType.Against },
  48. { voter: voter4, weight: web3.utils.toWei('2'), support: Enums.VoteType.Abstain },
  49. ],
  50. };
  51. });
  52. afterEach(async function () {
  53. expect(await this.mock.hasVoted(this.id, owner)).to.be.equal(false);
  54. expect(await this.mock.hasVoted(this.id, voter1)).to.be.equal(true);
  55. expect(await this.mock.hasVoted(this.id, voter2)).to.be.equal(true);
  56. expect(await this.mock.hasVoted(this.id, voter3)).to.be.equal(true);
  57. expect(await this.mock.hasVoted(this.id, voter4)).to.be.equal(true);
  58. this.receipts.castVote.filter(Boolean).forEach(vote => {
  59. const { voter } = vote.logs.find(Boolean).args;
  60. expectEvent(
  61. vote,
  62. 'VoteCast',
  63. this.settings.voters.find(({ address }) => address === voter),
  64. );
  65. });
  66. await this.mock.proposalVotes(this.id).then(result => {
  67. for (const [key, value] of Object.entries(Enums.VoteType)) {
  68. expect(result[`${key.toLowerCase()}Votes`]).to.be.bignumber.equal(
  69. Object.values(this.settings.voters).filter(({ support }) => support === value).reduce(
  70. (acc, { weight }) => acc.add(new BN(weight)),
  71. new BN('0'),
  72. ),
  73. );
  74. }
  75. });
  76. });
  77. runGovernorWorkflow();
  78. });
  79. });