Skip to content

Commit

Permalink
fix: PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
JGcarv committed Jun 5, 2024
1 parent a529d9d commit 52d2107
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 39 deletions.
24 changes: 13 additions & 11 deletions contracts/Migrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,33 @@ import { ERC20Helper } from "../modules/erc20-helper/src/ERC20Helper.sol";

import { IERC20Like } from "./interfaces/Interfaces.sol";

contract Migrator {
import { IMigrator } from "./interfaces/IMigrator.sol";

uint256 public constant SCALER = 10;
contract Migrator is IMigrator {

address public immutable newToken;
address public immutable oldToken;
uint256 public immutable override TOKEN_SPLIT_SCALAR;

constructor(address oldToken_, address newToken_) {
address public immutable override newToken;
address public immutable override oldToken;

constructor(address oldToken_, address newToken_, uint256 scalar_) {
require(IERC20Like(newToken_).decimals() == IERC20Like(oldToken_).decimals(), "M:C:DECIMAL_MISMATCH");

oldToken = oldToken_;
newToken = newToken_;

TOKEN_SPLIT_SCALAR = scalar_;
}

function migrate(uint256 amount_) external {
function migrate(uint256 amount_) external override {
migrate(msg.sender, amount_);
}

function migrate(address owner_, uint256 amount_) public {
function migrate(address owner_, uint256 amount_) public override {
require(amount_ != uint256(0), "M:M:ZERO_AMOUNT");

uint256 outputAmount = amount_ * SCALER;

require(ERC20Helper.transferFrom(oldToken, owner_, address(this), amount_), "M:M:TRANSFER_FROM_FAILED");
require(ERC20Helper.transfer(newToken, owner_, outputAmount), "M:M:TRANSFER_FAILED");
require(ERC20Helper.transferFrom(oldToken, owner_, address(this), amount_), "M:M:TRANSFER_FROM_FAILED");
require(ERC20Helper.transfer(newToken, owner_, amount_ * TOKEN_SPLIT_SCALAR), "M:M:TRANSFER_FAILED");
}

}
6 changes: 6 additions & 0 deletions contracts/interfaces/IMigrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,10 @@ interface IMigrator {
*/
function migrate(address owner_, uint256 amount_) external;

/**
* @dev Get the scalar value for token split.
* @return TOKEN_SPLIT_SCALAR_ The scalar value for token split.
*/
function TOKEN_SPLIT_SCALAR() external view returns (uint256 TOKEN_SPLIT_SCALAR_);

}
55 changes: 27 additions & 28 deletions contracts/test/Migrator.t.sol → tests/Migrator.t.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity 0.8.7;

import { Test } from "../../modules/forge-std/src/Test.sol";
import { Test } from "../modules/forge-std/src/Test.sol";

import { IERC20 } from "../../modules/erc20/contracts/interfaces/IERC20.sol";
import { MockERC20 } from "../../modules/erc20/contracts/test/mocks/MockERC20.sol";
import { MockERC20 } from "../modules/erc20/contracts/test/mocks/MockERC20.sol";

import { Migrator } from "../Migrator.sol";
import { Migrator } from "../contracts/Migrator.sol";

contract MigratorConstructorTest is Test {

Expand All @@ -15,14 +14,14 @@ contract MigratorConstructorTest is Test {
MockERC20 newToken = new MockERC20("New Token", "NT", 17);

vm.expectRevert("M:C:DECIMAL_MISMATCH");
new Migrator(address(oldToken), address(newToken));
new Migrator(address(oldToken), address(newToken), 1);
}

function test_constructor() external {
MockERC20 oldToken = new MockERC20("Old Token", "OT", 18);
MockERC20 newToken = new MockERC20("New Token", "NT", 18);

Migrator migrator = new Migrator(address(oldToken), address(newToken));
Migrator migrator = new Migrator(address(oldToken), address(newToken), 1);

assertEq(migrator.oldToken(), address(oldToken));
assertEq(migrator.newToken(), address(newToken));
Expand All @@ -32,7 +31,7 @@ contract MigratorConstructorTest is Test {

contract MigratorTest is Test {

uint256 internal constant SCALER = 10;
uint256 internal constant SCALAR = 10;
uint256 internal constant OLD_SUPPLY = 10_000_000 ether;

address account = makeAddr("account");
Expand All @@ -45,10 +44,10 @@ contract MigratorTest is Test {
_oldToken = new MockERC20("Old Token", "OT", 18);
_newToken = new MockERC20("New Token", "NT", 18);

_migrator = new Migrator(address(_oldToken), address(_newToken));
_migrator = new Migrator(address(_oldToken), address(_newToken), SCALAR);

// Mint new token to migrator
_newToken.mint(address(_migrator), OLD_SUPPLY * SCALER);
_newToken.mint(address(_migrator), OLD_SUPPLY * SCALAR);
}

function test_migrate_zeroAmount() external {
Expand All @@ -60,20 +59,20 @@ contract MigratorTest is Test {

_migrator.migrate(1);

uint256 newAmount = 1 * SCALER;
uint256 newAmount = 1 * SCALAR;

assertEq(_oldToken.allowance(address(this), address(_migrator)), 0);

assertEq(_oldToken.balanceOf(address(this)), 0);
assertEq(_oldToken.balanceOf(address(_migrator)), 1);
assertEq(_newToken.balanceOf(address(this)), newAmount);
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALER)- newAmount);
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALAR)- newAmount);
}

function testFuzz_migrate_insufficientApproval(uint256 amount_) external {
amount_ = bound(amount_, 1, OLD_SUPPLY);

uint256 newTokenAmount = amount_ * SCALER;
uint256 newTokenAmount = amount_ * SCALAR;

// Mint amount of old token
_oldToken.mint(address(this), amount_);
Expand All @@ -93,7 +92,7 @@ contract MigratorTest is Test {
assertEq(_oldToken.balanceOf(address(this)), 0);
assertEq(_oldToken.balanceOf(address(_migrator)), amount_);
assertEq(_newToken.balanceOf(address(this)), newTokenAmount);
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALER) - newTokenAmount);
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALAR) - newTokenAmount);
}

function testFuzz_migrate_insufficientBalance(uint256 amount_) external {
Expand All @@ -111,23 +110,23 @@ contract MigratorTest is Test {

_migrator.migrate(amount_);

uint256 newAmount = amount_ * SCALER;
uint256 newAmount = amount_ * SCALAR;

assertEq(_oldToken.allowance(address(this), address(_migrator)), 0);

assertEq(_oldToken.balanceOf(address(this)), 0);
assertEq(_oldToken.balanceOf(address(_migrator)), amount_);
assertEq(_newToken.balanceOf(address(this)), newAmount);
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALER) - newAmount);
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALAR) - newAmount);
}

function testFuzz_migrate_newTokenInsufficientBalance(uint256 amount_) external {
amount_ = bound(amount_, 1, OLD_SUPPLY);

uint256 newAmount = amount_ * SCALER;
uint256 newAmount = amount_ * SCALAR;

// Burn new supply that was added in setUp
_newToken.burn(address(_migrator), (OLD_SUPPLY * SCALER) - newAmount + 1);
_newToken.burn(address(_migrator), (OLD_SUPPLY * SCALAR) - newAmount + 1);

// Mint amount of old token
_oldToken.mint(address(this), amount_);
Expand All @@ -153,7 +152,7 @@ contract MigratorTest is Test {
function testFuzz_migrate_success(uint256 amount_) external {
amount_ = bound(amount_, 1, OLD_SUPPLY);

uint256 newAmount = amount_ * SCALER;
uint256 newAmount = amount_ * SCALAR;

// Mint amount of old token
_oldToken.mint(address(this), amount_);
Expand All @@ -166,7 +165,7 @@ contract MigratorTest is Test {
assertEq(_oldToken.balanceOf(address(this)), amount_);
assertEq(_oldToken.balanceOf(address(_migrator)), 0);
assertEq(_newToken.balanceOf(address(this)), 0);
assertEq(_newToken.balanceOf(address(_migrator)), OLD_SUPPLY * SCALER);
assertEq(_newToken.balanceOf(address(_migrator)), OLD_SUPPLY * SCALAR);

_migrator.migrate(amount_);

Expand All @@ -175,13 +174,13 @@ contract MigratorTest is Test {
assertEq(_oldToken.balanceOf(address(this)), 0);
assertEq(_oldToken.balanceOf(address(_migrator)), amount_);
assertEq(_newToken.balanceOf(address(this)), newAmount);
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALER) - newAmount);
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALAR) - newAmount);
}

function testFuzz_migration_specifiedOwner(uint256 amount_) external {
amount_ = bound(amount_, 1, OLD_SUPPLY);

uint256 newAmount = amount_ * SCALER;
uint256 newAmount = amount_ * SCALAR;

// Mint amount of old token
_oldToken.mint(address(account), amount_);
Expand All @@ -195,7 +194,7 @@ contract MigratorTest is Test {
assertEq(_oldToken.balanceOf(address(account)), amount_);
assertEq(_oldToken.balanceOf(address(_migrator)), 0);
assertEq(_newToken.balanceOf(address(account)), 0);
assertEq(_newToken.balanceOf(address(_migrator)), OLD_SUPPLY * SCALER);
assertEq(_newToken.balanceOf(address(_migrator)), OLD_SUPPLY * SCALAR);

_migrator.migrate(address(account), amount_);

Expand All @@ -204,15 +203,15 @@ contract MigratorTest is Test {
assertEq(_oldToken.balanceOf(address(account)), 0);
assertEq(_oldToken.balanceOf(address(_migrator)), amount_);
assertEq(_newToken.balanceOf(address(account)), newAmount);
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALER) - newAmount);
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALAR) - newAmount);
}

function testFuzz_migrate_partialMigration(uint256 amount_, uint256 partialAmount_) external {
amount_ = bound(amount_, 2, OLD_SUPPLY);
partialAmount_ = bound(partialAmount_, 1, amount_ - 1);

uint256 newAmount = amount_ * SCALER;
uint256 newPartialAmount = partialAmount_ * SCALER;
uint256 newAmount = amount_ * SCALAR;
uint256 newPartialAmount = partialAmount_ * SCALAR;

// Mint amount of old token
_oldToken.mint(address(this), amount_);
Expand All @@ -225,7 +224,7 @@ contract MigratorTest is Test {
assertEq(_oldToken.balanceOf(address(this)), amount_);
assertEq(_oldToken.balanceOf(address(_migrator)), 0);
assertEq(_newToken.balanceOf(address(this)), 0);
assertEq(_newToken.balanceOf(address(_migrator)), OLD_SUPPLY * SCALER);
assertEq(_newToken.balanceOf(address(_migrator)), OLD_SUPPLY * SCALAR);

_migrator.migrate(partialAmount_);

Expand All @@ -234,7 +233,7 @@ contract MigratorTest is Test {
assertEq(_oldToken.balanceOf(address(this)), amount_ - partialAmount_);
assertEq(_oldToken.balanceOf(address(_migrator)), partialAmount_);
assertEq(_newToken.balanceOf(address(this)), newPartialAmount);
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALER) - newPartialAmount);
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALAR) - newPartialAmount);

uint256 remaining = amount_ - partialAmount_;

Expand All @@ -247,7 +246,7 @@ contract MigratorTest is Test {
assertEq(_oldToken.balanceOf(address(this)), 0);
assertEq(_oldToken.balanceOf(address(_migrator)), amount_);
assertEq(_newToken.balanceOf(address(this)), newAmount);
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALER)- newAmount);
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALAR)- newAmount);
}

}

0 comments on commit 52d2107

Please sign in to comment.