ERC20Migrator.sol 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. pragma solidity ^0.5.0;
  2. import "../token/ERC20/IERC20.sol";
  3. import "../token/ERC20/ERC20Mintable.sol";
  4. import "../token/ERC20/SafeERC20.sol";
  5. import "../math/Math.sol";
  6. /**
  7. * @title ERC20Migrator
  8. * @dev This contract can be used to migrate an ERC20 token from one
  9. * contract to another, where each token holder has to opt-in to the migration.
  10. * To opt-in, users must approve for this contract the number of tokens they
  11. * want to migrate. Once the allowance is set up, anyone can trigger the
  12. * migration to the new token contract. In this way, token holders "turn in"
  13. * their old balance and will be minted an equal amount in the new token.
  14. * The new token contract must be mintable. For the precise interface refer to
  15. * OpenZeppelin's {ERC20Mintable}, but the only functions that are needed are
  16. * {MinterRole-isMinter} and {ERC20Mintable-mint}. The migrator will check
  17. * that it is a minter for the token.
  18. * The balance from the legacy token will be transferred to the migrator, as it
  19. * is migrated, and remain there forever.
  20. * Although this contract can be used in many different scenarios, the main
  21. * motivation was to provide a way to migrate ERC20 tokens into an upgradeable
  22. * version of it using ZeppelinOS. To read more about how this can be done
  23. * using this implementation, please follow the official documentation site of
  24. * ZeppelinOS: https://docs.zeppelinos.org/docs/erc20_onboarding.html
  25. *
  26. * Example of usage:
  27. * ```
  28. * const migrator = await ERC20Migrator.new(legacyToken.address);
  29. * await newToken.addMinter(migrator.address);
  30. * await migrator.beginMigration(newToken.address);
  31. * ```
  32. */
  33. contract ERC20Migrator {
  34. using SafeERC20 for IERC20;
  35. /// Address of the old token contract
  36. IERC20 private _legacyToken;
  37. /// Address of the new token contract
  38. ERC20Mintable private _newToken;
  39. /**
  40. * @param legacyToken address of the old token contract
  41. */
  42. constructor (IERC20 legacyToken) public {
  43. require(address(legacyToken) != address(0), "ERC20Migrator: legacy token is the zero address");
  44. _legacyToken = legacyToken;
  45. }
  46. /**
  47. * @dev Returns the legacy token that is being migrated.
  48. */
  49. function legacyToken() public view returns (IERC20) {
  50. return _legacyToken;
  51. }
  52. /**
  53. * @dev Returns the new token to which we are migrating.
  54. */
  55. function newToken() public view returns (IERC20) {
  56. return _newToken;
  57. }
  58. /**
  59. * @dev Begins the migration by setting which is the new token that will be
  60. * minted. This contract must be a minter for the new token.
  61. * @param newToken_ the token that will be minted
  62. */
  63. function beginMigration(ERC20Mintable newToken_) public {
  64. require(address(_newToken) == address(0), "ERC20Migrator: migration already started");
  65. require(address(newToken_) != address(0), "ERC20Migrator: new token is the zero address");
  66. //solhint-disable-next-line max-line-length
  67. require(newToken_.isMinter(address(this)), "ERC20Migrator: not a minter for new token");
  68. _newToken = newToken_;
  69. }
  70. /**
  71. * @dev Transfers part of an account's balance in the old token to this
  72. * contract, and mints the same amount of new tokens for that account.
  73. * @param account whose tokens will be migrated
  74. * @param amount amount of tokens to be migrated
  75. */
  76. function migrate(address account, uint256 amount) public {
  77. require(address(_newToken) != address(0), "ERC20Migrator: migration not started");
  78. _legacyToken.safeTransferFrom(account, address(this), amount);
  79. _newToken.mint(account, amount);
  80. }
  81. /**
  82. * @dev Transfers all of an account's allowed balance in the old token to
  83. * this contract, and mints the same amount of new tokens for that account.
  84. * @param account whose tokens will be migrated
  85. */
  86. function migrateAll(address account) public {
  87. uint256 balance = _legacyToken.balanceOf(account);
  88. uint256 allowance = _legacyToken.allowance(account, address(this));
  89. uint256 amount = Math.min(balance, allowance);
  90. migrate(account, amount);
  91. }
  92. }