Votes.test.js 3.7 KB

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