ERC20Burnable.behavior.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const { BN, constants, expectEvent, expectRevert } = require('openzeppelin-test-helpers');
  2. const { ZERO_ADDRESS } = constants;
  3. function shouldBehaveLikeERC20Burnable (owner, initialBalance, [burner]) {
  4. describe('burn', function () {
  5. describe('when the given amount is not greater than balance of the sender', function () {
  6. context('for a zero amount', function () {
  7. shouldBurn(new BN(0));
  8. });
  9. context('for a non-zero amount', function () {
  10. shouldBurn(new BN(100));
  11. });
  12. function shouldBurn (amount) {
  13. beforeEach(async function () {
  14. ({ logs: this.logs } = await this.token.burn(amount, { from: owner }));
  15. });
  16. it('burns the requested amount', async function () {
  17. (await this.token.balanceOf(owner)).should.be.bignumber.equal(initialBalance.sub(amount));
  18. });
  19. it('emits a transfer event', async function () {
  20. expectEvent.inLogs(this.logs, 'Transfer', {
  21. from: owner,
  22. to: ZERO_ADDRESS,
  23. value: amount,
  24. });
  25. });
  26. }
  27. });
  28. describe('when the given amount is greater than the balance of the sender', function () {
  29. const amount = initialBalance.addn(1);
  30. it('reverts', async function () {
  31. await expectRevert(this.token.burn(amount, { from: owner }),
  32. 'SafeMath: subtraction overflow'
  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. (await this.token.balanceOf(owner)).should.be.bignumber.equal(initialBalance.sub(amount));
  54. });
  55. it('decrements allowance', async function () {
  56. (await this.token.allowance(owner, burner)).should.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(this.token.burnFrom(owner, amount, { from: burner }),
  72. 'SafeMath: subtraction overflow'
  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. 'SafeMath: subtraction overflow'
  82. );
  83. });
  84. });
  85. });
  86. }
  87. module.exports = {
  88. shouldBehaveLikeERC20Burnable,
  89. };