Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(test): add migration test with swap failing #162

Merged
merged 5 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 0 additions & 51 deletions contracts/facets/FoundryFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,6 @@ import {IVault} from "../interfaces/IVault.sol";
/// @author @cartercarlson, @parv3213
/// @notice This contract manages all minting / burning for meTokens protocol
contract FoundryFacet is IFoundryFacet, Modifiers {
// MINT FLOW CHART
/****************************************************************************
// //
// mint() //
// | //
// CALCULATE MINT //
// / \ //
// is hub updating or meToken migrating? -{ (Y) (N) //
// / \ //
// CALCULATE | //
// TARGET MINT | //
// | | //
// TIME-WEIGHTED | //
// AVERAGE | //
// \ / //
// MINT RETURN //
// | //
// .sub(fees) //
// //
****************************************************************************/
/// @inheritdoc IFoundryFacet
function mint(
address meToken,
Expand Down Expand Up @@ -63,37 +43,6 @@ contract FoundryFacet is IFoundryFacet, Modifiers {
);
}

// BURN FLOW CHART
/****************************************************************************
// //
// burn() //
// | //
// CALCULATE BURN //
// / \ //
// is hub updating or meToken migrating? -{ (Y) (N) //
// / \ //
// CALCULATE \ //
// TARGET BURN \ //
// / \ //
// TIME-WEIGHTED \ //
// AVERAGE \ //
// | | //
// WEIGHTED BURN RETURN BURN RETURN //
// / \ / \ //
// is msg.sender the -{ (N) (Y) (Y) (N) //
// owner? (vs buyer) / \ / \ //
// GET CALCULATE GET //
// TIME-WEIGHTED BALANCE LOCKED REFUND //
// REFUND RATIO RETURNED RATIO //
// | | | //
// .mul(wRR) .add(BLR) .mul(RR) //
// \|/ //
// | //
// ACTUAL (WEIGHTED) BURN RETURN //
// | //
// .sub(fees) //
// //
****************************************************************************/
/// @inheritdoc IFoundryFacet
function burn(
address meToken,
Expand Down
3 changes: 2 additions & 1 deletion contracts/facets/MeTokenRegistryFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ contract MeTokenRegistryFacet is
HubInfo memory hubInfo = s.hubs[hubId];
require(hubInfo.active, "Hub inactive");
require(!hubInfo.updating, "Hub updating");

if (assetsDeposited > 0) {
IERC20(hubInfo.asset).safeTransferFrom(
sender,
Expand Down Expand Up @@ -154,7 +155,7 @@ contract MeTokenRegistryFacet is
require(meTokenInfo.targetHubId != 0, "!resubscribing");
require(
!IMigration(meTokenInfo.migration).isStarted(meToken),
"cannot cancel resubscribe"
"cannot cancel"
);

meTokenInfo.startTime = 0;
Expand Down
1 change: 1 addition & 0 deletions contracts/interfaces/IMeTokenRegistryFacet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ interface IMeTokenRegistryFacet {
address migration,
bytes encodedMigrationArgs
);

/// @notice Event of canceling a meToken resubscription
/// @param meToken Address of meToken
event CancelResubscribe(address indexed meToken);
Expand Down
116 changes: 86 additions & 30 deletions contracts/libs/LibFoundry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,50 @@ library LibFoundry {
uint256 assetsReturned
);

// MINT FLOW CHART
/****************************************************************************
// //
// mint() //
// | //
// CALCULATE MINT //
// / \ //
// is hub updating or meToken migrating? -{ (Y) (N) //
// / \ //
// CALCULATE | //
// TARGET MINT | //
// | | //
// TIME-WEIGHTED | //
// AVERAGE | //
// \ / //
// MINT RETURN //
// | //
// .sub(fees) //
// //
****************************************************************************/
function mint(
address meToken,
uint256 assetsDeposited,
address recipient
) internal {
(
,
address asset,
address sender,
uint256[3] memory amounts // 0-meTokensMinted 1-fee 2-assetsDepositedAfterFees
) = handleMint(meToken, assetsDeposited);

// Mint meToken to user
IMeToken(meToken).mint(recipient, amounts[0]);
emit Mint(
meToken,
asset,
sender,
recipient,
assetsDeposited,
amounts[0]
);
}

function handleMint(address meToken, uint256 assetsDeposited)
internal
returns (
Expand Down Expand Up @@ -92,30 +136,6 @@ library LibFoundry {
return (vault, asset, sender, amounts);
}

function mint(
address meToken,
uint256 assetsDeposited,
address recipient
) internal {
(
,
address asset,
address sender,
uint256[3] memory amounts // 0-meTokensMinted 1-fee 2-assetsDepositedAfterFees
) = handleMint(meToken, assetsDeposited);

// Mint meToken to user
IMeToken(meToken).mint(recipient, amounts[0]);
emit Mint(
meToken,
asset,
sender,
recipient,
assetsDeposited,
amounts[0]
);
}

function mintWithPermit(
address meToken,
uint256 assetsDeposited,
Expand Down Expand Up @@ -151,6 +171,37 @@ library LibFoundry {
);
}

// BURN FLOW CHART
/****************************************************************************
// //
// burn() //
// | //
// CALCULATE BURN //
// / \ //
// is hub updating or meToken migrating? -{ (Y) (N) //
// / \ //
// CALCULATE \ //
// TARGET BURN \ //
// / \ //
// TIME-WEIGHTED \ //
// AVERAGE \ //
// | | //
// WEIGHTED BURN RETURN BURN RETURN //
// / \ / \ //
// is msg.sender the -{ (N) (Y) (Y) (N) //
// owner? (vs buyer) / \ / \ //
// GET CALCULATE GET //
// TIME-WEIGHTED BALANCE LOCKED REFUND //
// REFUND RATIO RETURNED RATIO //
// | | | //
// .mul(wRR) .add(BLR) .mul(RR) //
// \|/ //
// | //
// ACTUAL (WEIGHTED) BURN RETURN //
// | //
// .sub(fees) //
// //
****************************************************************************/
function burn(
address meToken,
uint256 meTokensBurned,
Expand All @@ -164,12 +215,17 @@ library LibFoundry {
// Handling changes
if (hubInfo.updating && block.timestamp > hubInfo.endTime) {
LibHub.finishUpdate(meTokenInfo.hubId);
} else if (
meTokenInfo.targetHubId != 0 &&
block.timestamp > meTokenInfo.endTime
) {
hubInfo = s.hubs[meTokenInfo.targetHubId];
meTokenInfo = LibMeToken.finishResubscribe(meToken);
} else if (meTokenInfo.targetHubId != 0) {
if (block.timestamp > meTokenInfo.endTime) {
hubInfo = s.hubs[meTokenInfo.targetHubId];
meTokenInfo = LibMeToken.finishResubscribe(meToken);
} else if (block.timestamp > meTokenInfo.startTime) {
// Handle migration actions if needed
IMigration(meTokenInfo.migration).poke(meToken);
meTokenInfo = s.meTokens[meToken];
}
/* hubInfo = s.hubs[meTokenInfo.targetHubId];
meTokenInfo = LibMeToken.finishResubscribe(meToken); */
}
// Calculate how many tokens are returned
uint256 rawAssetsReturned = _calculateRawAssetsReturned(
Expand Down
5 changes: 1 addition & 4 deletions contracts/migrations/UniswapSingleTransferMigration.sol
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ contract UniswapSingleTransferMigration is ReentrancyGuard, Vault, IMigration {
UniswapSingleTransfer storage usts = _uniswapSingleTransfers[meToken];
MeTokenInfo memory meTokenInfo = IMeTokenRegistryFacet(diamond)
.getMeTokenInfo(meToken);

if (
usts.fee != 0 && // make sure meToken is in a state of resubscription
block.timestamp > meTokenInfo.startTime && // swap can only happen after resubscribe
Expand Down Expand Up @@ -150,7 +149,7 @@ contract UniswapSingleTransferMigration is ReentrancyGuard, Vault, IMigration {
if (encodedArgs.length == 0) return false;
uint24 fee = abi.decode(encodedArgs, (uint24));

// Must have valid fees
// Invalid fee
return (fee == MINFEE || fee == MIDFEE || fee == MAXFEE);
}

Expand All @@ -167,7 +166,6 @@ contract UniswapSingleTransferMigration is ReentrancyGuard, Vault, IMigration {
);
uint256 amountIn = meTokenInfo.balancePooled +
meTokenInfo.balanceLocked;

// Only swap if
// - There are tokens to swap
// - The resubscription has started
Expand Down Expand Up @@ -205,7 +203,6 @@ contract UniswapSingleTransferMigration is ReentrancyGuard, Vault, IMigration {

// The call to `exactInputSingle` executes the swap
amountOut = _router.exactInputSingle(params);

// Based on amountIn and amountOut, update balancePooled and balanceLocked
IMeTokenRegistryFacet(diamond).updateBalances(meToken, amountOut);
}
Expand Down
1 change: 0 additions & 1 deletion contracts/vaults/SingleAssetVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ contract SingleAssetVault is Vault, ISingleAssetVault {

require(msg.sender == (meTokenInfo.migration), "!migration");
uint256 balance = meTokenInfo.balancePooled + meTokenInfo.balanceLocked;

if (
meTokenInfo.migration != address(0) &&
meTokenInfo.migration != address(this)
Expand Down
Loading