ERC20Burnable.behavior.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. const shouldFail = require('../../../helpers/shouldFail');
  2. const expectEvent = require('../../../helpers/expectEvent');
  3. const { ZERO_ADDRESS } = require('../../../helpers/constants');
  4. const BigNumber = web3.BigNumber;
  5. require('chai')
  6. .use(require('chai-bignumber')(BigNumber))
  7. .should();
  8. function shouldBehaveLikeERC20Burnable (owner, initialBalance, [burner]) {
  9. describe('burn', function () {
  10. describe('when the given amount is not greater than balance of the sender', function () {
  11. context('for a zero amount', function () {
  12. shouldBurn(0);
  13. });
  14. context('for a non-zero amount', function () {
  15. shouldBurn(100);
  16. });
  17. function shouldBurn (amount) {
  18. beforeEach(async function () {
  19. ({ logs: this.logs } = await this.token.burn(amount, { from: owner }));
  20. });
  21. it('burns the requested amount', async function () {
  22. (await this.token.balanceOf(owner)).should.be.bignumber.equal(initialBalance - amount);
  23. });
  24. it('emits a transfer event', async function () {
  25. expectEvent.inLogs(this.logs, 'Transfer', {
  26. from: owner,
  27. to: ZERO_ADDRESS,
  28. value: amount,
  29. });
  30. });
  31. }
  32. });
  33. describe('when the given amount is greater than the balance of the sender', function () {
  34. const amount = initialBalance + 1;
  35. it('reverts', async function () {
  36. await shouldFail.reverting(this.token.burn(amount, { from: owner }));
  37. });
  38. });
  39. });
  40. describe('burnFrom', function () {
  41. describe('on success', function () {
  42. context('for a zero amount', function () {
  43. shouldBurnFrom(0);
  44. });
  45. context('for a non-zero amount', function () {
  46. shouldBurnFrom(100);
  47. });
  48. function shouldBurnFrom (amount) {
  49. const originalAllowance = amount * 3;
  50. beforeEach(async function () {
  51. await this.token.approve(burner, originalAllowance, { from: owner });
  52. const { logs } = await this.token.burnFrom(owner, amount, { from: burner });
  53. this.logs = logs;
  54. });
  55. it('burns the requested amount', async function () {
  56. (await this.token.balanceOf(owner)).should.be.bignumber.equal(initialBalance - amount);
  57. });
  58. it('decrements allowance', async function () {
  59. (await this.token.allowance(owner, burner)).should.be.bignumber.equal(originalAllowance - amount);
  60. });
  61. it('emits a transfer event', async function () {
  62. expectEvent.inLogs(this.logs, 'Transfer', {
  63. from: owner,
  64. to: ZERO_ADDRESS,
  65. value: amount,
  66. });
  67. });
  68. }
  69. });
  70. describe('when the given amount is greater than the balance of the sender', function () {
  71. const amount = initialBalance + 1;
  72. it('reverts', async function () {
  73. await this.token.approve(burner, amount, { from: owner });
  74. await shouldFail.reverting(this.token.burnFrom(owner, amount, { from: burner }));
  75. });
  76. });
  77. describe('when the given amount is greater than the allowance', function () {
  78. const amount = 100;
  79. it('reverts', async function () {
  80. await this.token.approve(burner, amount - 1, { from: owner });
  81. await shouldFail.reverting(this.token.burnFrom(owner, amount, { from: burner }));
  82. });
  83. });
  84. });
  85. }
  86. module.exports = {
  87. shouldBehaveLikeERC20Burnable,
  88. };