BurnableToken.behavior.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 shouldBehaveLikeBurnableToken (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 burn event', async function () {
  25. const event = expectEvent.inLogs(this.logs, 'TokensBurned');
  26. event.args.burner.should.equal(owner);
  27. event.args.value.should.be.bignumber.equal(amount);
  28. });
  29. it('emits a transfer event', async function () {
  30. const event = expectEvent.inLogs(this.logs, 'Transfer');
  31. event.args.from.should.equal(owner);
  32. event.args.to.should.equal(ZERO_ADDRESS);
  33. event.args.value.should.be.bignumber.equal(amount);
  34. });
  35. }
  36. });
  37. describe('when the given amount is greater than the balance of the sender', function () {
  38. const amount = initialBalance + 1;
  39. it('reverts', async function () {
  40. await assertRevert(this.token.burn(amount, { from: owner }));
  41. });
  42. });
  43. });
  44. describe('burnFrom', function () {
  45. describe('on success', function () {
  46. context('for a zero amount', function () {
  47. shouldBurnFrom(0);
  48. });
  49. context('for a non-zero amount', function () {
  50. shouldBurnFrom(100);
  51. });
  52. function shouldBurnFrom (amount) {
  53. const originalAllowance = amount * 3;
  54. beforeEach(async function () {
  55. await this.token.approve(burner, originalAllowance, { from: owner });
  56. const { logs } = await this.token.burnFrom(owner, amount, { from: burner });
  57. this.logs = logs;
  58. });
  59. it('burns the requested amount', async function () {
  60. (await this.token.balanceOf(owner)).should.be.bignumber.equal(initialBalance - amount);
  61. });
  62. it('decrements allowance', async function () {
  63. (await this.token.allowance(owner, burner)).should.be.bignumber.equal(originalAllowance - amount);
  64. });
  65. it('emits a burn event', async function () {
  66. const event = expectEvent.inLogs(this.logs, 'TokensBurned');
  67. event.args.burner.should.equal(owner);
  68. event.args.value.should.be.bignumber.equal(amount);
  69. });
  70. it('emits a transfer event', async function () {
  71. const event = expectEvent.inLogs(this.logs, 'Transfer');
  72. event.args.from.should.equal(owner);
  73. event.args.to.should.equal(ZERO_ADDRESS);
  74. event.args.value.should.be.bignumber.equal(amount);
  75. });
  76. }
  77. });
  78. describe('when the given amount is greater than the balance of the sender', function () {
  79. const amount = initialBalance + 1;
  80. it('reverts', async function () {
  81. await this.token.approve(burner, amount, { from: owner });
  82. await assertRevert(this.token.burnFrom(owner, amount, { from: burner }));
  83. });
  84. });
  85. describe('when the given amount is greater than the allowance', function () {
  86. const amount = 100;
  87. it('reverts', async function () {
  88. await this.token.approve(burner, amount - 1, { from: owner });
  89. await assertRevert(this.token.burnFrom(owner, amount, { from: burner }));
  90. });
  91. });
  92. });
  93. }
  94. module.exports = {
  95. shouldBehaveLikeBurnableToken,
  96. };