ERC20Burnable.behavior.js 3.3 KB

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