ERC20Migrator.test.js 5.9 KB

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