Votes.test.js 3.2 KB

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