ERC20Burnable.behavior.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. const { assertRevert } = require('../../../helpers/assertRevert');
  2. const expectEvent = require('../../../helpers/expectEvent');
  3. const BigNumber = web3.BigNumber;
  4. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  5. require('chai')
  6. .use(require('chai-bignumber')(BigNumber))
  7. .should();
  8. function shouldBehaveLikeERC20Burnable (owner, initialBalance, [burner]) {
  9. describe('burn', function () {
  10. describe('when the given amount is not greater than balance of the sender', function () {
  11. context('for a zero amount', function () {
  12. shouldBurn(0);
  13. });
  14. context('for a non-zero amount', function () {
  15. shouldBurn(100);
  16. });
  17. function shouldBurn (amount) {
  18. beforeEach(async function () {
  19. ({ logs: this.logs } = await this.token.burn(amount, { from: owner }));
  20. });
  21. it('burns the requested amount', async function () {
  22. (await this.token.balanceOf(owner)).should.be.bignumber.equal(initialBalance - amount);
  23. });
  24. it('emits a transfer event', async function () {
  25. const event = expectEvent.inLogs(this.logs, 'Transfer');
  26. event.args.from.should.equal(owner);
  27. event.args.to.should.equal(ZERO_ADDRESS);
  28. event.args.value.should.be.bignumber.equal(amount);
  29. });
  30. }
  31. });
  32. describe('when the given amount is greater than the balance of the sender', function () {
  33. const amount = initialBalance + 1;
  34. it('reverts', async function () {
  35. await assertRevert(this.token.burn(amount, { from: owner }));
  36. });
  37. });
  38. });
  39. describe('burnFrom', function () {
  40. describe('on success', function () {
  41. context('for a zero amount', function () {
  42. shouldBurnFrom(0);
  43. });
  44. context('for a non-zero amount', function () {
  45. shouldBurnFrom(100);
  46. });
  47. function shouldBurnFrom (amount) {
  48. const originalAllowance = amount * 3;
  49. beforeEach(async function () {
  50. await this.token.approve(burner, originalAllowance, { from: owner });
  51. const { logs } = await this.token.burnFrom(owner, amount, { from: burner });
  52. this.logs = logs;
  53. });
  54. it('burns the requested amount', async function () {
  55. (await this.token.balanceOf(owner)).should.be.bignumber.equal(initialBalance - amount);
  56. });
  57. it('decrements allowance', async function () {
  58. (await this.token.allowance(owner, burner)).should.be.bignumber.equal(originalAllowance - amount);
  59. });
  60. it('emits a transfer event', async function () {
  61. const event = expectEvent.inLogs(this.logs, 'Transfer');
  62. event.args.from.should.equal(owner);
  63. event.args.to.should.equal(ZERO_ADDRESS);
  64. event.args.value.should.be.bignumber.equal(amount);
  65. });
  66. }
  67. });
  68. describe('when the given amount is greater than the balance of the sender', function () {
  69. const amount = initialBalance + 1;
  70. it('reverts', async function () {
  71. await this.token.approve(burner, amount, { from: owner });
  72. await assertRevert(this.token.burnFrom(owner, amount, { from: burner }));
  73. });
  74. });
  75. describe('when the given amount is greater than the allowance', function () {
  76. const amount = 100;
  77. it('reverts', async function () {
  78. await this.token.approve(burner, amount - 1, { from: owner });
  79. await assertRevert(this.token.burnFrom(owner, amount, { from: burner }));
  80. });
  81. });
  82. });
  83. }
  84. module.exports = {
  85. shouldBehaveLikeERC20Burnable,
  86. };