Skip to content

Instantly share code, notes, and snippets.

@graemecode
Created October 1, 2021 16:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save graemecode/6a801d6568002bd666c86ea6db4216d6 to your computer and use it in GitHub Desktop.
Save graemecode/6a801d6568002bd666c86ea6db4216d6 to your computer and use it in GitHub Desktop.
ERC20Upgrader.sol
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity 0.8.6;
import {IERC20} from "../../external/interface/IERC20.sol";
/**
* @title ERC20Upgrader
* @author MirrorXYZ
* @notice This contract allows swapping a legacy ERC20 token for an upgraded one.
*/
contract ERC20Upgrader {
// ============ Immutable Storage ============
address public immutable legacyAddress;
address public immutable targetAddress;
uint256 public immutable exchangeRate;
/**
* If true, amount to exchange is divided by exchange rate, otherwise multiplied.
* For a 1:1 transfer, make useDivide = false and exchangeRate = 1.
*/
bool public immutable useDivide;
// ============ Mutable Storage ============
uint256 public totalMigrated;
// ============ Events ============
event Upgraded(address owner, uint256 amount);
// ============ Constructor ============
constructor(
address legacyAddress_,
address targetAddress_,
uint256 exchangeRate_,
bool useDivide_
) {
legacyAddress = legacyAddress_;
targetAddress = targetAddress_;
exchangeRate = exchangeRate_;
useDivide = useDivide_;
}
// ============ Public Functions ============
// Burns the legacy token, and distributes the new one.
function upgrade(address upgrader, uint256 amount) public {
// Burn the legacy token, for the given amount.
IERC20(legacyAddress).transferFrom(upgrader, address(0), amount);
// Conditional for whether exchange rate should divide the amount.
if (useDivide) {
// Send the upgrader the amount, divided by the exchange rate.
IERC20(targetAddress).transfer(upgrader, amount / exchangeRate);
} else {
// The the upgrader the amount, multiplied by the exchange rate.
IERC20(targetAddress).transfer(upgrader, amount * exchangeRate);
}
// Keep track of the amount that has been migrated.
totalMigrated += amount;
// Emit an event broadcasting that a holder has upgraded.
emit Upgraded(upgrader, amount);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment