Skip to content

Commit

Permalink
Merge pull request #142 from meTokens/fix/cancelResubscribe
Browse files Browse the repository at this point in the history
`CancelResubscribe` revert if Migration has started (usts.started == true)
  • Loading branch information
Carter Carlson authored Apr 7, 2022
2 parents dbb990f + 8a4c954 commit d9dc4cb
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 10 deletions.
2 changes: 1 addition & 1 deletion contracts/facets/MeTokenRegistryFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ contract MeTokenRegistryFacet is
require(sender == meTokenInfo.owner, "!owner");
require(meTokenInfo.targetHubId != 0, "!resubscribing");
require(
block.timestamp < meTokenInfo.startTime,
!IMigration(meTokenInfo.migration).migrationStarted(meToken),
"Resubscription has started"
);

Expand Down
8 changes: 8 additions & 0 deletions contracts/interfaces/IMigration.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@ interface IMigration {
function finishMigration(address meToken)
external
returns (uint256 amountOut);

/// @notice Method returns bool if migration started
/// @param meToken Address of meToken
/// @return started True if migration started else false
function migrationStarted(address meToken)
external
view
returns (bool started);
}
10 changes: 10 additions & 0 deletions contracts/migrations/SameAssetTransferMigration.sol
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,14 @@ contract SameAssetTransferMigration is ReentrancyGuard, Vault, IMigration {
if (meTokenInfo.hubId == 0) return false;
return true;
}

/// @inheritdoc IMigration
function migrationStarted(address meToken)
external
view
override
returns (bool started)
{
return _sameAssetMigration[meToken].started;
}
}
14 changes: 13 additions & 1 deletion contracts/migrations/UniswapSingleTransferMigration.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ contract UniswapSingleTransferMigration is ReentrancyGuard, Vault, IMigration {
meTokenInfo.hubId
);
if (
usts.soonest != 0 && block.timestamp > usts.soonest && !usts.started
usts.soonest != 0 && // this is to ensure the meToken is resubscribing
block.timestamp > usts.soonest &&
!usts.started
) {
ISingleAssetVault(hubInfo.vault).startMigration(meToken);
usts.started = true;
Expand Down Expand Up @@ -163,6 +165,16 @@ contract UniswapSingleTransferMigration is ReentrancyGuard, Vault, IMigration {
}
}

/// @inheritdoc IMigration
function migrationStarted(address meToken)
external
view
override
returns (bool started)
{
return _uniswapSingleTransfers[meToken].started;
}

/// @dev parent call must have reentrancy check
function _swap(address meToken) private returns (uint256 amountOut) {
UniswapSingleTransfer storage usts = _uniswapSingleTransfers[meToken];
Expand Down
63 changes: 55 additions & 8 deletions test/contracts/facets/MeTokenRegistryFacet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const setup = async () => {
let hub: HubFacet;
let token: ERC20;
let fee: FeesFacet;
let dai: ERC20;
let weth: ERC20;
let account0: SignerWithAddress;
let account1: SignerWithAddress;
Expand Down Expand Up @@ -175,6 +176,7 @@ const setup = async () => {
foundry.address // diamond
);
await migration.deployed();
dai = await getContractAt<ERC20>("ERC20", DAI);
weth = await getContractAt<ERC20>("ERC20", WETH);
});

Expand Down Expand Up @@ -612,7 +614,7 @@ const setup = async () => {
meTokenRegistry.connect(account1).cancelResubscribe(meTokenAddr1)
).to.be.revertedWith("!resubscribing");
});
it("Successfully resets meToken info", async () => {
it("Successfully cancels resubscribe", async () => {
block = await ethers.provider.getBlock("latest");
expect(
(await meTokenRegistry.getMeTokenInfo(meTokenAddr0)).startTime
Expand Down Expand Up @@ -666,15 +668,60 @@ const setup = async () => {
);
await tx.wait();
});
it("Fails if resubscription already started", async () => {
const meTokenRegistryDetails = await meTokenRegistry.getMeTokenInfo(
meTokenAddr0
it("should revert if migration started (usts.started = true)", async () => {
// forward time to endCoolDown
await mineBlock(
(
await meTokenRegistry.getMeTokenInfo(meTokenAddr0)
).endCooldown.toNumber() + 2
);
// forward time after start time
await mineBlock(meTokenRegistryDetails.startTime.toNumber() + 2);
block = await ethers.provider.getBlock("latest");
expect(meTokenRegistryDetails.startTime).to.be.lt(block.timestamp);

const earliestSwapTime = block.timestamp + 600 * 60; // 10h in future
encodedMigrationArgs = ethers.utils.defaultAbiCoder.encode(
["uint256", "uint24"],
[earliestSwapTime, fees]
);

await meTokenRegistry.initResubscribe(
meToken,
targetHubId,
migration.address,
encodedMigrationArgs
);

await dai
.connect(tokenHolder)
.transfer(account0.address, tokenDeposited);
await dai.approve(
singleAssetVault.address,
ethers.constants.MaxUint256
);
await foundry.mint(meTokenAddr0, tokenDeposited, account0.address);

await mineBlock(
(await migration.getDetails(meTokenAddr0)).soonest.toNumber() + 2
); // starTime > soonest

tx = await migration.poke(meTokenAddr0); // would call startMigration and swap

// called from startMigration
await expect(tx)
.to.emit(dai, "Transfer")
.withArgs(
singleAssetVault.address,
migration.address,
tokenDeposited
);
await expect(tx)
.to.emit(singleAssetVault, "StartMigration")
.withArgs(meTokenAddr0);
// migration -> uniswap
await expect(tx).to.emit(dai, "Transfer");
// uniswap -> migration
await expect(tx).to.emit(weth, "Transfer");

// should revert to cancelResubscribe now
await expect(
meTokenRegistry.cancelResubscribe(meTokenAddr0)
).to.be.revertedWith("Resubscription has started");
Expand All @@ -687,7 +734,7 @@ const setup = async () => {
meTokenRegistry.connect(account1).finishResubscribe(meTokenAddr1)
).to.be.revertedWith("No targetHubId");
});
it("Fails if updating but cooldown not reached", async () => {
it("Fails if updating but endTime not reached", async () => {
const meTokenRegistryDetails = await meTokenRegistry.getMeTokenInfo(
meTokenAddr0
);
Expand Down

0 comments on commit d9dc4cb

Please sign in to comment.