ERC20Burnable.behavior.js 3.5 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(this.token.burn(amount, { from: owner }),
  33. 'ERC20: burn amount exceeds balance'
  34. );
  35. });
  36. });
  37. });
  38. describe('burnFrom', function () {
  39. describe('on success', function () {
  40. context('for a zero amount', function () {
  41. shouldBurnFrom(new BN(0));
  42. });
  43. context('for a non-zero amount', function () {
  44. shouldBurnFrom(new BN(100));
  45. });
  46. function shouldBurnFrom (amount) {
  47. const originalAllowance = amount.muln(3);
  48. beforeEach(async function () {
  49. await this.token.approve(burner, originalAllowance, { from: owner });
  50. const { logs } = await this.token.burnFrom(owner, amount, { from: burner });
  51. this.logs = logs;
  52. });
  53. it('burns the requested amount', async function () {
  54. expect(await this.token.balanceOf(owner)).to.be.bignumber.equal(initialBalance.sub(amount));
  55. });
  56. it('decrements allowance', async function () {
  57. expect(await this.token.allowance(owner, burner)).to.be.bignumber.equal(originalAllowance.sub(amount));
  58. });
  59. it('emits a transfer event', async function () {
  60. expectEvent.inLogs(this.logs, 'Transfer', {
  61. from: owner,
  62. to: ZERO_ADDRESS,
  63. value: amount,
  64. });
  65. });
  66. }
  67. });
  68. describe('when the given amount is greater than the balance of the sender', function () {
  69. const amount = initialBalance.addn(1);
  70. it('reverts', async function () {
  71. await this.token.approve(burner, amount, { from: owner });
  72. await expectRevert(this.token.burnFrom(owner, amount, { from: burner }),
  73. 'ERC20: burn amount exceeds balance'
  74. );
  75. });
  76. });
  77. describe('when the given amount is greater than the allowance', function () {
  78. const allowance = new BN(100);
  79. it('reverts', async function () {
  80. await this.token.approve(burner, allowance, { from: owner });
  81. await expectRevert(this.token.burnFrom(owner, allowance.addn(1), { from: burner }),
  82. 'ERC20: burn amount exceeds allowance'
  83. );
  84. });
  85. });
  86. });
  87. }
  88. module.exports = {
  89. shouldBehaveLikeERC20Burnable,
  90. };