ERC20Burnable.behavior.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. (this.receipt = 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(this.receipt, '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. this.receipt = await this.token.burnFrom(owner, amount, { from: burner });
  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(this.receipt, '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(this.token.burnFrom(owner, amount, { from: burner }),
  72. 'ERC20: burn amount exceeds balance',
  73. );
  74. });
  75. });
  76. describe('when the given amount is greater than the allowance', function () {
  77. const allowance = new BN(100);
  78. it('reverts', async function () {
  79. await this.token.approve(burner, allowance, { from: owner });
  80. await expectRevert(this.token.burnFrom(owner, allowance.addn(1), { from: burner }),
  81. 'ERC20: insufficient allowance',
  82. );
  83. });
  84. });
  85. });
  86. }
  87. module.exports = {
  88. shouldBehaveLikeERC20Burnable,
  89. };