ERC20Burnable.behavior.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const { BN, constants, expectEvent, shouldFail } = 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 shouldFail.reverting(this.token.burn(amount, { from: owner }));
  32. });
  33. });
  34. });
  35. describe('burnFrom', function () {
  36. describe('on success', function () {
  37. context('for a zero amount', function () {
  38. shouldBurnFrom(new BN(0));
  39. });
  40. context('for a non-zero amount', function () {
  41. shouldBurnFrom(new BN(100));
  42. });
  43. function shouldBurnFrom (amount) {
  44. const originalAllowance = amount.muln(3);
  45. beforeEach(async function () {
  46. await this.token.approve(burner, originalAllowance, { from: owner });
  47. const { logs } = await this.token.burnFrom(owner, amount, { from: burner });
  48. this.logs = logs;
  49. });
  50. it('burns the requested amount', async function () {
  51. (await this.token.balanceOf(owner)).should.be.bignumber.equal(initialBalance.sub(amount));
  52. });
  53. it('decrements allowance', async function () {
  54. (await this.token.allowance(owner, burner)).should.be.bignumber.equal(originalAllowance.sub(amount));
  55. });
  56. it('emits a transfer event', async function () {
  57. expectEvent.inLogs(this.logs, '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 shouldFail.reverting(this.token.burnFrom(owner, amount, { from: burner }));
  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 shouldFail.reverting(this.token.burnFrom(owner, allowance.addn(1), { from: burner }));
  77. });
  78. });
  79. });
  80. }
  81. module.exports = {
  82. shouldBehaveLikeERC20Burnable,
  83. };