Votes.test.js 3.2 KB

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