Skip to content

Commit

Permalink
feat: Migrate for another account that has approved
Browse files Browse the repository at this point in the history
  • Loading branch information
deluca-mike committed Mar 6, 2022
1 parent b0160eb commit 8a16d5f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
8 changes: 6 additions & 2 deletions contracts/Migrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,14 @@ contract Migrator {
}

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

function migrate(address owner, uint256 amount_) public {
require(amount_ > 0, "M:M:ZERO_AMOUNT");

require(ERC20Helper.transferFrom(oldToken, msg.sender, address(this), amount_), "M:M:TRANSFER_FROM_FAILED");
require(ERC20Helper.transfer(newToken, msg.sender, amount_), "M:M:TRANSFER_FAILED");
require(ERC20Helper.transferFrom(oldToken, owner, address(this), amount_), "M:M:TRANSFER_FROM_FAILED");
require(ERC20Helper.transfer(newToken, owner, amount_), "M:M:TRANSFER_FAILED");
}

}
37 changes: 36 additions & 1 deletion contracts/test/Migrator.t.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,21 @@
// SPDX-License-Identifier: AGPL-3.0-or-later
pragma solidity 0.8.7;

import { TestUtils } from "../../modules/contract-test-utils/contracts/test.sol";
import { IERC20 } from "../../modules/erc20/contracts/interfaces/IERC20.sol";

import { TestUtils } from "../../modules/contract-test-utils/contracts/test.sol";
import { MockERC20 } from "../../modules/erc20/contracts/test/mocks/MockERC20.sol";

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

contract SomeAccount {

function approve(address token_, address spender_, uint256 amount_) external {
IERC20(token_).approve(spender_, amount_);
}

}

contract MigratorTest is TestUtils {

uint256 public constant OLD_SUPPLY = 10_000_000 ether;
Expand Down Expand Up @@ -49,6 +58,32 @@ contract MigratorTest is TestUtils {
assertEq(newToken.balanceOf(address(migrator)), OLD_SUPPLY - amount_);
}

function test_migrationForSpecifiedOwner(uint256 amount_) external {
amount_ = constrictToRange(amount_, 1, OLD_SUPPLY);

SomeAccount someAccount = new SomeAccount();

// Mint amount of old token
oldToken.mint(address(someAccount), amount_);

// Approve
someAccount.approve(address(oldToken), address(migrator), amount_);

assertEq(oldToken.balanceOf(address(someAccount)), amount_);
assertEq(oldToken.balanceOf(address(migrator)), 0);
assertEq(oldToken.allowance(address(someAccount), address(migrator)), amount_);
assertEq(newToken.balanceOf(address(someAccount)), 0);
assertEq(newToken.balanceOf(address(migrator)), OLD_SUPPLY);

migrator.migrate(address(someAccount), amount_);

assertEq(oldToken.balanceOf(address(someAccount)), 0);
assertEq(oldToken.balanceOf(address(migrator)), amount_);
assertEq(oldToken.allowance(address(someAccount), address(migrator)), 0);
assertEq(newToken.balanceOf(address(someAccount)), amount_);
assertEq(newToken.balanceOf(address(migrator)), OLD_SUPPLY - amount_);
}

function test_partialMigration(uint256 amount_, uint256 partialAmount_) external {
amount_ = constrictToRange(amount_, 2, OLD_SUPPLY);
partialAmount_ = constrictToRange(partialAmount_, 1, amount_ - 1);
Expand Down
2 changes: 1 addition & 1 deletion modules/erc20

0 comments on commit 8a16d5f

Please sign in to comment.