ERC20Migrator.test.js 5.9 KB

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