BurnableToken.behavior.js 3.4 KB

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