Skip to content

Commit

Permalink
feat: replace owner with recipient when migrating
Browse files Browse the repository at this point in the history
  • Loading branch information
vbidin committed Jul 3, 2024
1 parent 74e1d2b commit d5de4d8
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
6 changes: 3 additions & 3 deletions contracts/Migrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ contract Migrator is IMigrator {
newTokenAmount_ = migrate(msg.sender, oldTokenAmount_);
}

function migrate(address owner_, uint256 oldTokenAmount_) public override returns (uint256 newTokenAmount_) {
function migrate(address recipient_, uint256 oldTokenAmount_) public override returns (uint256 newTokenAmount_) {
require(active, "M:M:INACTIVE");
require(oldTokenAmount_ != uint256(0), "M:M:ZERO_AMOUNT");

newTokenAmount_ = oldTokenAmount_ * tokenSplitScalar;

require(ERC20Helper.transferFrom(oldToken, owner_, address(this), oldTokenAmount_), "M:M:TRANSFER_FROM_FAILED");
require(ERC20Helper.transfer(newToken, owner_, newTokenAmount_), "M:M:TRANSFER_FAILED");
require(ERC20Helper.transferFrom(oldToken, msg.sender, address(this), oldTokenAmount_), "M:M:TRANSFER_FROM_FAILED");
require(ERC20Helper.transfer(newToken, recipient_, newTokenAmount_), "M:M:TRANSFER_FAILED");
}

function setActive(bool active_) external override {
Expand Down
8 changes: 4 additions & 4 deletions contracts/interfaces/IMigrator.sol
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@ interface IMigrator {
function oldToken() external view returns (address oldToken_);

/**
* @dev Exchange the oldToken for the same amount of newToken.
* @dev Exchange the oldToken for the same amount of newToken.
* @param oldTokenAmount_ The amount of oldToken to swap for newToken.
* @return newTokenAmount_ The amount of newToken received.
*/
function migrate(uint256 oldTokenAmount_) external returns (uint256 newTokenAmount_);

/**
* @dev Exchange the oldToken for the same amount of newToken.
* @param owner_ The address of the owner of the oldToken.
* @dev Exchange the oldToken for the same amount of newToken.
* @param recipient_ The address of the recipient of the newToken.
* @param oldTokenAmount_ The amount of oldToken to swap for newToken.
* @return newTokenAmount_ The amount of newToken received.
*/
function migrate(address owner_, uint256 oldTokenAmount_) external returns (uint256 newTokenAmount_);
function migrate(address recipient_, uint256 oldTokenAmount_) external returns (uint256 newTokenAmount_);

/**
* @dev Set the migrator to active or inactive.
Expand Down
16 changes: 12 additions & 4 deletions tests/Migrator.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,9 @@ contract MigratorTest is Test {
uint256 internal constant SCALAR = 10;
uint256 internal constant OLD_SUPPLY = 10_000_000e18;

address operationalAdmin = makeAddr("operationalAdmin");
address account = makeAddr("account");
address operationalAdmin = makeAddr("operationalAdmin");
address recipient = makeAddr("recipient");

Migrator internal _migrator;
MockERC20 internal _oldToken;
Expand Down Expand Up @@ -266,7 +267,7 @@ contract MigratorTest is Test {
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALAR) - newAmount);
}

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

uint256 newAmount = amount_ * SCALAR;
Expand All @@ -282,17 +283,24 @@ contract MigratorTest is Test {

assertEq(_oldToken.balanceOf(address(account)), amount_);
assertEq(_oldToken.balanceOf(address(_migrator)), 0);
assertEq(_oldToken.balanceOf(address(recipient)), 0);

assertEq(_newToken.balanceOf(address(account)), 0);
assertEq(_newToken.balanceOf(address(_migrator)), OLD_SUPPLY * SCALAR);
assertEq(_newToken.balanceOf(address(recipient)), 0);

_migrator.migrate(address(account), amount_);
vm.prank(account);
_migrator.migrate(address(recipient), amount_);

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

assertEq(_oldToken.balanceOf(address(account)), 0);
assertEq(_oldToken.balanceOf(address(_migrator)), amount_);
assertEq(_newToken.balanceOf(address(account)), newAmount);
assertEq(_oldToken.balanceOf(address(recipient)), 0);

assertEq(_newToken.balanceOf(address(account)), 0);
assertEq(_newToken.balanceOf(address(_migrator)), (OLD_SUPPLY * SCALAR) - newAmount);
assertEq(_newToken.balanceOf(address(recipient)), newAmount);
}

function testFuzz_migrate_partialMigration(uint256 amount_, uint256 partialAmount_) external {
Expand Down

0 comments on commit d5de4d8

Please sign in to comment.