mirror of openzeppelin-contracts
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
openzeppelin-contracts/contracts/drafts/ERC20Migrator.sol

104 lines
4.1 KiB

pragma solidity ^0.5.0;
import "../token/ERC20/IERC20.sol";
import "../token/ERC20/ERC20Mintable.sol";
import "../token/ERC20/SafeERC20.sol";
import "../math/Math.sol";
/**
* @title ERC20Migrator
* @dev This contract can be used to migrate an ERC20 token from one
* contract to another, where each token holder has to opt-in to the migration.
* To opt-in, users must approve for this contract the number of tokens they
* want to migrate. Once the allowance is set up, anyone can trigger the
* migration to the new token contract. In this way, token holders "turn in"
* their old balance and will be minted an equal amount in the new token.
* The new token contract must be mintable. For the precise interface refer to
migrate content to format for new docs site Squashed commit of the following: commit fcf35eb806100de300bd9803ce3150dde1ecc424 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 17 17:16:04 2019 -0300 remove all docsite dependency commit eeaee9a9d43d70704f6ab17b5126ddbd52b93a50 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 17 17:15:23 2019 -0300 update solidity-docgen commit f021ff951829ea0c155186749819403c6b76e803 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 17 17:05:06 2019 -0300 update docsite script for new setup commit ff887699d381cfbbe3acf1f1c0de8e22b58480f3 Merge: c938aa1d 84f85a41 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 17 16:46:46 2019 -0300 Merge branch 'master' into antora commit c938aa1d9ed05ac83a34e2cebd8353f8331ad6d6 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jul 16 18:24:29 2019 -0300 make component name shorter commit 5bbd6931e02cbbd8864c82655ad0f390ceead5f3 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 20:16:17 2019 -0300 add all info to docs templates commit 39682c4515d7cf0f0368ed557f50d2709174208a Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 20:13:49 2019 -0300 fix npm docsite script commit 7ae46bd4a0437abf66150d54d05adf46e3de2cab Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 18:48:05 2019 -0300 convert inline docs to asciidoc commit cfdfd3dee4b4bf582fde22c8cb6e17a603d6e0c8 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 17:34:52 2019 -0300 add missing contract names in readmes commit 15b6a2f9bfb546cf1d3bf4f104278b118bf1b3f4 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 17:16:47 2019 -0300 fix script path commit 80d82b909f9460d1450d401f00b3f309da506b29 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 17:13:53 2019 -0300 update version of solidity-docgen commit a870b6c607b9c2d0012f8a60a4ed1a1c8b7e8ebd Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 17:03:53 2019 -0300 add nav generation of api ref commit 069cff4a25b83752650b54b86d85608c2f547e5e Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 16:32:14 2019 -0300 initial migration to asciidoc and new docgen version commit 55216eed0a6551da913c8d1da4b2a0d0d3faa1a8 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 20:39:35 2019 -0300 add basic api doc example commit 0cbe50ce2173b6d1d9a698329d91220f58822a53 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 19:31:31 2019 -0300 add sidebars commit 256fc942845307258ac9dc25aace48117fa10f79 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 15:22:38 2019 -0300 add page titles commit f4d0effa70e1fc0662729863e8ee72a8821bc458 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 15:19:41 2019 -0300 add contracts index file commit b73b06359979f7d933df7f2b283c50cb1c31b2a0 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 15:14:52 2019 -0300 fix header levels commit fb57d9b820f09a1b7c04eed1a205be0e45866cac Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 15:11:47 2019 -0300 switch format to preferred asciidoctor format commit 032181d8804137332c71534753929d080a31a71f Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 15:05:38 2019 -0300 initialize antora component and convert docs to asciidoc
6 years ago
* OpenZeppelin's {ERC20Mintable}, but the only functions that are needed are
* {MinterRole-isMinter} and {ERC20Mintable-mint}. The migrator will check
* that it is a minter for the token.
Nonfunctional typos #1643 (#1652) * Add IntelliJ IDE config to .gitignore * Fix variable name in ERC20 function comments * Fix typos in Arrays function comment * Fix typos in ownership test names * Fix typo in Pausable test name * Fix grammar in Ownable function comment * Fix grammar in Crowdsale contract comment * Fix typo in Counters contract comment * Fix typo in ERC721Enumerable comment * Fix typo in ERC721PausedToken test name * Fix typo in Crowdsale function comment * Fix typo in IncreasingPriceCrowdsale function comment * Fix grammar in IncreasingPriceCrowdsale test name * Fix typo in AllowanceCrowdsale test name * Fix typo in RefundEscrow function comment * Fix typo in ERC20Migrator contract comment * Fix typos in SignatureBouncer comments * Fix typo in SignedSafeMath test name * Fix typo in TokenVesting contract comment * Move Ownable comment from @notice section to @dev The Ownable contract has a comment explaining that renouncing ownership will prevent execution of functions with the onlyOwner modifier. This commit moves that comment to the @dev section and replaces it with a description suitable for a generic user. * Clarify purpose of ERC20 transfer function * Clarify registration of ERC721Enumerable interface * Clarify purpose of AllowanceCrowdsale test * Increase specificity of inheritance comments FinalizableCrowdsale and RefundableCrowsale both have comments indicating that they are extensions of the Crowdsale contract. This commit refines those comments to the most immediate ancestor ( TimedCrowdsale and RefundableCrowdsale respectively ) * Remove unused parameter in PaymentSplitter test * Rename parameter in SignatureBouncer functions The SignatureBouncer contract has modifiers to validate the message sender is authorised to perform an action. They pass msg.sender to internal functions as the variable `account`, but the function comments refer to the variable as `sender` This commit changes the variable name to `sender` * Clarify comments in SignatureBouncer functions The SignatureBouncer has comments that use the description `sender` to refer to the variable `account`. This commit updates the comments for consistency. Maintainer Note: this reverts changes in the previous commit, which renamed the variable `account` instead.
6 years ago
* The balance from the legacy token will be transferred to the migrator, as it
* is migrated, and remain there forever.
* Although this contract can be used in many different scenarios, the main
* motivation was to provide a way to migrate ERC20 tokens into an upgradeable
* version of it using ZeppelinOS. To read more about how this can be done
* using this implementation, please follow the official documentation site of
* ZeppelinOS: https://docs.zeppelinos.org/docs/erc20_onboarding.html
migrate content to format for new docs site Squashed commit of the following: commit fcf35eb806100de300bd9803ce3150dde1ecc424 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 17 17:16:04 2019 -0300 remove all docsite dependency commit eeaee9a9d43d70704f6ab17b5126ddbd52b93a50 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 17 17:15:23 2019 -0300 update solidity-docgen commit f021ff951829ea0c155186749819403c6b76e803 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 17 17:05:06 2019 -0300 update docsite script for new setup commit ff887699d381cfbbe3acf1f1c0de8e22b58480f3 Merge: c938aa1d 84f85a41 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 17 16:46:46 2019 -0300 Merge branch 'master' into antora commit c938aa1d9ed05ac83a34e2cebd8353f8331ad6d6 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jul 16 18:24:29 2019 -0300 make component name shorter commit 5bbd6931e02cbbd8864c82655ad0f390ceead5f3 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 20:16:17 2019 -0300 add all info to docs templates commit 39682c4515d7cf0f0368ed557f50d2709174208a Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 20:13:49 2019 -0300 fix npm docsite script commit 7ae46bd4a0437abf66150d54d05adf46e3de2cab Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 18:48:05 2019 -0300 convert inline docs to asciidoc commit cfdfd3dee4b4bf582fde22c8cb6e17a603d6e0c8 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 17:34:52 2019 -0300 add missing contract names in readmes commit 15b6a2f9bfb546cf1d3bf4f104278b118bf1b3f4 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 17:16:47 2019 -0300 fix script path commit 80d82b909f9460d1450d401f00b3f309da506b29 Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 17:13:53 2019 -0300 update version of solidity-docgen commit a870b6c607b9c2d0012f8a60a4ed1a1c8b7e8ebd Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 17:03:53 2019 -0300 add nav generation of api ref commit 069cff4a25b83752650b54b86d85608c2f547e5e Author: Francisco Giordano <frangio.1@gmail.com> Date: Wed Jul 10 16:32:14 2019 -0300 initial migration to asciidoc and new docgen version commit 55216eed0a6551da913c8d1da4b2a0d0d3faa1a8 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 20:39:35 2019 -0300 add basic api doc example commit 0cbe50ce2173b6d1d9a698329d91220f58822a53 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 19:31:31 2019 -0300 add sidebars commit 256fc942845307258ac9dc25aace48117fa10f79 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 15:22:38 2019 -0300 add page titles commit f4d0effa70e1fc0662729863e8ee72a8821bc458 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 15:19:41 2019 -0300 add contracts index file commit b73b06359979f7d933df7f2b283c50cb1c31b2a0 Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 15:14:52 2019 -0300 fix header levels commit fb57d9b820f09a1b7c04eed1a205be0e45866cac Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 15:11:47 2019 -0300 switch format to preferred asciidoctor format commit 032181d8804137332c71534753929d080a31a71f Author: Francisco Giordano <frangio.1@gmail.com> Date: Tue Jun 25 15:05:38 2019 -0300 initialize antora component and convert docs to asciidoc
6 years ago
*
* Example of usage:
* ```
* const migrator = await ERC20Migrator.new(legacyToken.address);
* await newToken.addMinter(migrator.address);
* await migrator.beginMigration(newToken.address);
* ```
*/
contract ERC20Migrator {
using SafeERC20 for IERC20;
/// Address of the old token contract
IERC20 private _legacyToken;
/// Address of the new token contract
ERC20Mintable private _newToken;
/**
* @param legacyToken address of the old token contract
*/
constructor (IERC20 legacyToken) public {
require(address(legacyToken) != address(0), "ERC20Migrator: legacy token is the zero address");
_legacyToken = legacyToken;
}
/**
* @dev Returns the legacy token that is being migrated.
*/
function legacyToken() public view returns (IERC20) {
return _legacyToken;
}
/**
* @dev Returns the new token to which we are migrating.
*/
function newToken() public view returns (IERC20) {
return _newToken;
}
/**
* @dev Begins the migration by setting which is the new token that will be
* minted. This contract must be a minter for the new token.
* @param newToken_ the token that will be minted
*/
function beginMigration(ERC20Mintable newToken_) public {
require(address(_newToken) == address(0), "ERC20Migrator: migration already started");
require(address(newToken_) != address(0), "ERC20Migrator: new token is the zero address");
//solhint-disable-next-line max-line-length
require(newToken_.isMinter(address(this)), "ERC20Migrator: not a minter for new token");
_newToken = newToken_;
}
/**
* @dev Transfers part of an account's balance in the old token to this
* contract, and mints the same amount of new tokens for that account.
* @param account whose tokens will be migrated
* @param amount amount of tokens to be migrated
*/
function migrate(address account, uint256 amount) public {
require(address(_newToken) != address(0), "ERC20Migrator: migration not started");
_legacyToken.safeTransferFrom(account, address(this), amount);
_newToken.mint(account, amount);
}
/**
* @dev Transfers all of an account's allowed balance in the old token to
* this contract, and mints the same amount of new tokens for that account.
* @param account whose tokens will be migrated
*/
function migrateAll(address account) public {
uint256 balance = _legacyToken.balanceOf(account);
uint256 allowance = _legacyToken.allowance(account, address(this));
uint256 amount = Math.min(balance, allowance);
migrate(account, amount);
}
}