Votes.test.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. const { constants, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { expect } = require('chai');
  3. const { clockFromReceipt } = require('../../helpers/time');
  4. const { BNsum } = require('../../helpers/math');
  5. require('array.prototype.at/auto');
  6. const { shouldBehaveLikeVotes } = require('./Votes.behavior');
  7. const MODES = {
  8. blocknumber: artifacts.require('$VotesMock'),
  9. timestamp: artifacts.require('$VotesTimestampMock'),
  10. };
  11. contract('Votes', function (accounts) {
  12. const [account1, account2, account3] = accounts;
  13. const amounts = {
  14. [account1]: web3.utils.toBN('10000000000000000000000000'),
  15. [account2]: web3.utils.toBN('10'),
  16. [account3]: web3.utils.toBN('20'),
  17. };
  18. const name = 'My Vote';
  19. const version = '1';
  20. for (const [mode, artifact] of Object.entries(MODES)) {
  21. describe(`vote with ${mode}`, function () {
  22. beforeEach(async function () {
  23. this.votes = await artifact.new(name, version);
  24. });
  25. shouldBehaveLikeVotes(accounts, Object.values(amounts), { mode, fungible: true });
  26. it('starts with zero votes', async function () {
  27. expect(await this.votes.getTotalSupply()).to.be.bignumber.equal('0');
  28. });
  29. describe('performs voting operations', function () {
  30. beforeEach(async function () {
  31. this.txs = [];
  32. for (const [account, amount] of Object.entries(amounts)) {
  33. this.txs.push(await this.votes.$_mint(account, amount));
  34. }
  35. });
  36. it('reverts if block number >= current block', async function () {
  37. const lastTxTimepoint = await clockFromReceipt[mode](this.txs.at(-1).receipt);
  38. await expectRevert(this.votes.getPastTotalSupply(lastTxTimepoint + 1), 'Votes: future lookup');
  39. });
  40. it('delegates', async function () {
  41. expect(await this.votes.getVotes(account1)).to.be.bignumber.equal('0');
  42. expect(await this.votes.getVotes(account2)).to.be.bignumber.equal('0');
  43. expect(await this.votes.delegates(account1)).to.be.equal(constants.ZERO_ADDRESS);
  44. expect(await this.votes.delegates(account2)).to.be.equal(constants.ZERO_ADDRESS);
  45. await this.votes.delegate(account1, account1);
  46. expect(await this.votes.getVotes(account1)).to.be.bignumber.equal(amounts[account1]);
  47. expect(await this.votes.getVotes(account2)).to.be.bignumber.equal('0');
  48. expect(await this.votes.delegates(account1)).to.be.equal(account1);
  49. expect(await this.votes.delegates(account2)).to.be.equal(constants.ZERO_ADDRESS);
  50. await this.votes.delegate(account2, account1);
  51. expect(await this.votes.getVotes(account1)).to.be.bignumber.equal(amounts[account1].add(amounts[account2]));
  52. expect(await this.votes.getVotes(account2)).to.be.bignumber.equal('0');
  53. expect(await this.votes.delegates(account1)).to.be.equal(account1);
  54. expect(await this.votes.delegates(account2)).to.be.equal(account1);
  55. });
  56. it('cross delegates', async function () {
  57. await this.votes.delegate(account1, account2);
  58. await this.votes.delegate(account2, account1);
  59. expect(await this.votes.getVotes(account1)).to.be.bignumber.equal(amounts[account2]);
  60. expect(await this.votes.getVotes(account2)).to.be.bignumber.equal(amounts[account1]);
  61. });
  62. it('returns total amount of votes', async function () {
  63. const totalSupply = BNsum(...Object.values(amounts));
  64. expect(await this.votes.getTotalSupply()).to.be.bignumber.equal(totalSupply);
  65. });
  66. });
  67. });
  68. }
  69. });