BurnableToken.behaviour.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. const { assertRevert } = require('../../helpers/assertRevert');
  2. const { inLogs } = 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. const amount = 100;
  12. beforeEach(async function () {
  13. ({ logs: this.logs } = await this.token.burn(amount, { from: owner }));
  14. });
  15. it('burns the requested amount', async function () {
  16. const balance = await this.token.balanceOf(owner);
  17. balance.should.be.bignumber.equal(initialBalance - amount);
  18. });
  19. it('emits a burn event', async function () {
  20. const event = await inLogs(this.logs, 'Burn');
  21. event.args.burner.should.eq(owner);
  22. event.args.value.should.be.bignumber.equal(amount);
  23. });
  24. it('emits a transfer event', async function () {
  25. const event = await inLogs(this.logs, 'Transfer');
  26. event.args.from.should.eq(owner);
  27. event.args.to.should.eq(ZERO_ADDRESS);
  28. event.args.value.should.be.bignumber.equal(amount);
  29. });
  30. });
  31. describe('when the given amount is greater than the balance of the sender', function () {
  32. const amount = initialBalance + 1;
  33. it('reverts', async function () {
  34. await assertRevert(this.token.burn(amount, { from: owner }));
  35. });
  36. });
  37. });
  38. describe('burnFrom', function () {
  39. describe('on success', function () {
  40. const amount = 100;
  41. beforeEach(async function () {
  42. await this.token.approve(burner, 300, { from: owner });
  43. const { logs } = await this.token.burnFrom(owner, amount, { from: burner });
  44. this.logs = logs;
  45. });
  46. it('burns the requested amount', async function () {
  47. const balance = await this.token.balanceOf(owner);
  48. balance.should.be.bignumber.equal(initialBalance - amount);
  49. });
  50. it('decrements allowance', async function () {
  51. const allowance = await this.token.allowance(owner, burner);
  52. allowance.should.be.bignumber.equal(200);
  53. });
  54. it('emits a burn event', async function () {
  55. const event = await inLogs(this.logs, 'Burn');
  56. event.args.burner.should.eq(owner);
  57. event.args.value.should.be.bignumber.equal(amount);
  58. });
  59. it('emits a transfer event', async function () {
  60. const event = await inLogs(this.logs, 'Transfer');
  61. event.args.from.should.eq(owner);
  62. event.args.to.should.eq(ZERO_ADDRESS);
  63. event.args.value.should.be.bignumber.equal(amount);
  64. });
  65. });
  66. describe('when the given amount is greater than the balance of the sender', function () {
  67. const amount = initialBalance + 1;
  68. it('reverts', async function () {
  69. await this.token.approve(burner, amount, { from: owner });
  70. await assertRevert(this.token.burnFrom(owner, amount, { from: burner }));
  71. });
  72. });
  73. describe('when the given amount is greater than the allowance', function () {
  74. const amount = 100;
  75. it('reverts', async function () {
  76. await this.token.approve(burner, amount - 1, { from: owner });
  77. await assertRevert(this.token.burnFrom(owner, amount, { from: burner }));
  78. });
  79. });
  80. });
  81. }
  82. module.exports = {
  83. shouldBehaveLikeBurnableToken,
  84. };