ERC20Migrator.test.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. const { assertRevert } = require('../helpers/assertRevert');
  2. const ERC20Mock = artifacts.require('ERC20Mock');
  3. const ERC20Mintable = artifacts.require('ERC20Mintable');
  4. const ERC20Migrator = artifacts.require('ERC20Migrator');
  5. const BigNumber = web3.eth.BigNumber;
  6. require('chai')
  7. .use(require('chai-bignumber')(BigNumber))
  8. .should();
  9. contract('ERC20Migrator', function ([_, owner, recipient, anotherAccount]) {
  10. const ZERO_ADDRESS = '0x0000000000000000000000000000000000000000';
  11. const totalSupply = 200;
  12. it('reverts with a null legacy token address', async function () {
  13. await assertRevert(ERC20Migrator.new(ZERO_ADDRESS));
  14. });
  15. describe('with tokens and migrator', function () {
  16. beforeEach('deploying tokens and migrator', async function () {
  17. this.legacyToken = await ERC20Mock.new(owner, totalSupply);
  18. this.migrator = await ERC20Migrator.new(this.legacyToken.address);
  19. this.newToken = await ERC20Mintable.new();
  20. });
  21. it('returns legacy token', async function () {
  22. (await this.migrator.legacyToken()).should.be.equal(this.legacyToken.address);
  23. });
  24. describe('beginMigration', function () {
  25. it('reverts with a null new token address', async function () {
  26. await assertRevert(this.migrator.beginMigration(ZERO_ADDRESS));
  27. });
  28. it('reverts if not a minter of the token', async function () {
  29. await assertRevert(this.migrator.beginMigration(this.newToken.address));
  30. });
  31. it('succeeds if it is a minter of the token', async function () {
  32. await this.newToken.addMinter(this.migrator.address);
  33. await this.migrator.beginMigration(this.newToken.address);
  34. });
  35. it('reverts the second time it is called', async function () {
  36. await this.newToken.addMinter(this.migrator.address);
  37. await this.migrator.beginMigration(this.newToken.address);
  38. await assertRevert(this.migrator.beginMigration(this.newToken.address));
  39. });
  40. });
  41. describe('once migration began', function () {
  42. beforeEach('beginning migration', async function () {
  43. await this.newToken.addMinter(this.migrator.address);
  44. await this.migrator.beginMigration(this.newToken.address);
  45. });
  46. it('returns new token', async function () {
  47. (await this.migrator.newToken()).should.be.equal(this.newToken.address);
  48. });
  49. describe('migrateAll', function () {
  50. const baseAmount = totalSupply;
  51. describe('when the approved balance is equal to the owned balance', function () {
  52. const amount = baseAmount;
  53. beforeEach('approving the whole balance to the new contract', async function () {
  54. await this.legacyToken.approve(this.migrator.address, amount, { from: owner });
  55. });
  56. beforeEach('migrating token', async function () {
  57. const tx = await this.migrator.migrateAll(owner);
  58. this.logs = tx.receipt.logs;
  59. });
  60. it('mints the same balance of the new token', async function () {
  61. const currentBalance = await this.newToken.balanceOf(owner);
  62. currentBalance.should.be.bignumber.equal(amount);
  63. });
  64. it('burns a given amount of old tokens', async function () {
  65. const currentBurnedBalance = await this.legacyToken.balanceOf(this.migrator.address);
  66. currentBurnedBalance.should.be.bignumber.equal(amount);
  67. const currentLegacyTokenBalance = await this.legacyToken.balanceOf(owner);
  68. currentLegacyTokenBalance.should.be.bignumber.equal(0);
  69. });
  70. it('updates the total supply', async function () {
  71. const currentSupply = await this.newToken.totalSupply();
  72. currentSupply.should.be.bignumber.equal(amount);
  73. });
  74. });
  75. describe('when the approved balance is lower than the owned balance', function () {
  76. const amount = baseAmount - 1;
  77. beforeEach('approving part of the balance to the new contract', async function () {
  78. await this.legacyToken.approve(this.migrator.address, amount, { from: owner });
  79. await this.migrator.migrateAll(owner);
  80. });
  81. it('migrates only approved amount', async function () {
  82. const currentBalance = await this.newToken.balanceOf(owner);
  83. currentBalance.should.be.bignumber.equal(amount);
  84. });
  85. });
  86. });
  87. describe('migrate', function () {
  88. const baseAmount = 50;
  89. beforeEach('approving tokens to the new contract', async function () {
  90. await this.legacyToken.approve(this.migrator.address, baseAmount, { from: owner });
  91. });
  92. describe('when the amount is equal to the one approved', function () {
  93. const amount = baseAmount;
  94. beforeEach('migrate token', async function () {
  95. ({ logs: this.logs } = await this.migrator.migrate(owner, amount));
  96. });
  97. it('mints that amount of the new token', async function () {
  98. const currentBalance = await this.newToken.balanceOf(owner);
  99. currentBalance.should.be.bignumber.equal(amount);
  100. });
  101. it('burns a given amount of old tokens', async function () {
  102. const currentBurnedBalance = await this.legacyToken.balanceOf(this.migrator.address);
  103. currentBurnedBalance.should.be.bignumber.equal(amount);
  104. const currentLegacyTokenBalance = await this.legacyToken.balanceOf(owner);
  105. currentLegacyTokenBalance.should.be.bignumber.equal(totalSupply - amount);
  106. });
  107. it('updates the total supply', async function () {
  108. const currentSupply = await this.newToken.totalSupply();
  109. currentSupply.should.be.bignumber.equal(amount);
  110. });
  111. });
  112. describe('when the given amount is higher than the one approved', function () {
  113. const amount = baseAmount + 1;
  114. it('reverts', async function () {
  115. await assertRevert(this.migrator.migrate(owner, amount));
  116. });
  117. });
  118. });
  119. });
  120. });
  121. });