ERC20Burnable.behavior.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. const { BN, constants, expectEvent, expectRevert } = require('@openzeppelin/test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. const { expect } = require('chai');
  4. function shouldBehaveLikeERC20Burnable (owner, initialBalance, [burner]) {
  5. describe('burn', function () {
  6. describe('when the given amount is not greater than balance of the sender', function () {
  7. context('for a zero amount', function () {
  8. shouldBurn(new BN(0));
  9. });
  10. context('for a non-zero amount', function () {
  11. shouldBurn(new BN(100));
  12. });
  13. function shouldBurn (amount) {
  14. beforeEach(async function () {
  15. ({ logs: this.logs } = await this.token.burn(amount, { from: owner }));
  16. });
  17. it('burns the requested amount', async function () {
  18. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal(initialBalance.sub(amount));
  19. });
  20. it('emits a transfer event', async function () {
  21. expectEvent.inLogs(this.logs, 'Transfer', {
  22. from: owner,
  23. to: ZERO_ADDRESS,
  24. value: amount,
  25. });
  26. });
  27. }
  28. });
  29. describe('when the given amount is greater than the balance of the sender', function () {
  30. const amount = initialBalance.addn(1);
  31. it('reverts', async function () {
  32. await expectRevert.unspecified(this.token.burn(amount, { from: owner }),
  33. );
  34. });
  35. });
  36. });
  37. describe('burnFrom', function () {
  38. describe('on success', function () {
  39. context('for a zero amount', function () {
  40. shouldBurnFrom(new BN(0));
  41. });
  42. context('for a non-zero amount', function () {
  43. shouldBurnFrom(new BN(100));
  44. });
  45. function shouldBurnFrom (amount) {
  46. const originalAllowance = amount.muln(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. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal(initialBalance.sub(amount));
  54. });
  55. it('decrements allowance', async function () {
  56. expect(await this.token.allowance(owner, burner)).to.be.bignumber.equal(originalAllowance.sub(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.addn(1);
  69. it('reverts', async function () {
  70. await this.token.approve(burner, amount, { from: owner });
  71. await expectRevert.unspecified(this.token.burnFrom(owner, amount, { from: burner }),
  72. );
  73. });
  74. });
  75. describe('when the given amount is greater than the allowance', function () {
  76. const allowance = new BN(100);
  77. it('reverts', async function () {
  78. await this.token.approve(burner, allowance, { from: owner });
  79. await expectRevert.unspecified(this.token.burnFrom(owner, allowance.addn(1), { from: burner }),
  80. );
  81. });
  82. });
  83. });
  84. }
  85. module.exports = {
  86. shouldBehaveLikeERC20Burnable,
  87. };