ERC20Burnable.behavior.js 3.4 KB

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