Skip to content

Commit

Permalink
Merge pull request #73 from meTokens/test/meTokenRegistry-resubscribe…
Browse files Browse the repository at this point in the history
…-rebased

Test/me token registry resubscribe rebased
  • Loading branch information
Carl Farterson authored Dec 14, 2021
2 parents c2fd546 + 0969695 commit 0102f0c
Show file tree
Hide file tree
Showing 10 changed files with 991 additions and 302 deletions.
10 changes: 8 additions & 2 deletions contracts/migrations/UniswapSingleTransferMigration.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity ^0.8.0;

import {ISwapRouter} from "@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/proxy/utils/Initializable.sol";

Expand Down Expand Up @@ -69,7 +70,6 @@ contract UniswapSingleTransferMigration is
UniswapSingleTransfer storage usts_ = _uniswapSingleTransfers[_meToken];
usts_.fee = fee;
usts_.soonest = soonest;
usts_.started = true;
}

function poke(address _meToken) external override {
Expand All @@ -79,6 +79,7 @@ contract UniswapSingleTransferMigration is
Details.Hub memory hub_ = hub.getDetails(meToken_.hubId);
if (usts_.soonest != 0 && block.timestamp > usts_.soonest) {
ISingleAssetVault(hub_.vault).startMigration(_meToken);
usts_.started = true;
}
_swap(_meToken);
}
Expand All @@ -99,6 +100,7 @@ contract UniswapSingleTransferMigration is
// TODO: require migration hasn't finished, block.timestamp > meToken_.startTime
if (!usts_.started) {
ISingleAssetVault(hub_.vault).startMigration(_meToken);
usts_.started = true;
}

if (!usts_.swapped) {
Expand Down Expand Up @@ -154,12 +156,15 @@ contract UniswapSingleTransferMigration is
Details.MeToken memory meToken_ = meTokenRegistry.getDetails(_meToken);
Details.Hub memory hub_ = hub.getDetails(meToken_.hubId);
Details.Hub memory targetHub_ = hub.getDetails(meToken_.targetHubId);
uint256 amountIn = meToken_.balancePooled + meToken_.balanceLocked;

// Only swap if
// - There are tokens to swap
// - The resubscription has started
// - The asset hasn't been swapped
// - Current time is past the soonest it can swap, and time to swap has been set
if (
amountIn == 0 ||
!usts_.started ||
usts_.swapped ||
usts_.soonest == 0 ||
Expand All @@ -168,7 +173,8 @@ contract UniswapSingleTransferMigration is
return 0;
}

uint256 amountIn = meToken_.balancePooled + meToken_.balanceLocked;
// Approve router to spend
IERC20(hub_.asset).approve(address(_router), amountIn);

// https://docs.uniswap.org/protocol/guides/swaps/single-swaps
ISwapRouter.ExactInputSingleParams memory params = ISwapRouter
Expand Down
34 changes: 16 additions & 18 deletions contracts/registries/MeTokenRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,22 @@ contract MeTokenRegistry is Ownable, IMeTokenRegistry {
require(!targetHub_.updating, "targetHub updating");

// Ensure the migration we're using is approved
if (hub_.asset != targetHub_.asset || _migration != address(0)) {
require(
migrationRegistry.isApproved(
hub_.vault,
targetHub_.vault,
_migration
),
"!approved"
);
require(
IVault(_migration).isValid(_meToken, _encodedMigrationArgs),
"Invalid _encodedMigrationArgs"
);
IMigration(_migration).initMigration(
_meToken,
_encodedMigrationArgs
);
}
require(hub_.asset != targetHub_.asset, "asset same");
require(_migration != address(0), "migration address(0)");

require(
migrationRegistry.isApproved(
hub_.vault,
targetHub_.vault,
_migration
),
"!approved"
);
require(
IVault(_migration).isValid(_meToken, _encodedMigrationArgs),
"Invalid _encodedMigrationArgs"
);
IMigration(_migration).initMigration(_meToken, _encodedMigrationArgs);

meToken_.startTime = block.timestamp + _warmup;
meToken_.endTime = block.timestamp + _warmup + _duration;
Expand Down
13 changes: 12 additions & 1 deletion contracts/vaults/SingleAssetVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,20 @@ contract SingleAssetVault is Ownable, Vault, ISingleAssetVault {
// After warmup period, if there's a migration vault,
// Send meTokens' collateral to the migration
function startMigration(address _meToken) external override {
require(msg.sender == address(hub), "!hub");
Details.MeToken memory meToken_ = meTokenRegistry.getDetails(_meToken);
Details.Hub memory hub_ = hub.getDetails(meToken_.hubId);

require(
msg.sender == address(hub) ||
// TODO: Ensure meToken can actually start migrating from the migration addr
// and properly check these arguments
migrationRegistry.isApproved(
address(this),
address(this),
meToken_.migration
),
"!hub || !migrationRegistry.approved"
);
uint256 balance = meToken_.balancePooled + meToken_.balanceLocked;

if (
Expand Down
6 changes: 3 additions & 3 deletions test/contracts/curves/BancorBancorCurve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ describe("BancorPowerCurve", () => {
// estimate = 4.000000000000000001
expect(toETHNumber(estimate)).to.be.approximately(
calculatedRes,
0.000000000000001
0.00000000000001
);
});
it("viewAssetsReturned should work with a max of 999999999999999000000000000000000 supply should work", async () => {
Expand All @@ -205,7 +205,7 @@ describe("BancorPowerCurve", () => {
// estimate = 0.002
expect(toETHNumber(estimate)).to.be.approximately(
calculatedRes,
0.000000000000001
0.00000000000001
);
});
it("initReconfigure() should work", async () => {
Expand Down Expand Up @@ -266,7 +266,7 @@ describe("BancorPowerCurve", () => {
);
expect(toETHNumber(estimate)).to.be.approximately(
calculatedRes,
0.00000000000000000001
0.000000000001
);
});
it("viewTargetAssetsReturned() to zero supply should work", async () => {
Expand Down
6 changes: 3 additions & 3 deletions test/contracts/curves/BancorZeroCurve.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ describe("BancorABDK", () => {
// estimate = 4.000000000000000001
expect(toETHNumber(estimate)).to.be.approximately(
calculatedRes,
0.000000000000001
0.00000000000001
);
});
it("viewAssetsReturned should work with a max of 999999999999999000000000000000000 supply should work", async () => {
Expand All @@ -203,7 +203,7 @@ describe("BancorABDK", () => {
// estimate = 0.002
expect(toETHNumber(estimate)).to.be.approximately(
calculatedRes,
0.000000000000001
0.00000000000001
);
});
it("initReconfigure() should work", async () => {
Expand Down Expand Up @@ -264,7 +264,7 @@ describe("BancorABDK", () => {
);
expect(toETHNumber(estimate)).to.be.approximately(
calculatedRes,
0.00000000000000000001
0.000000000001
);
});
it("viewTargetAssetsReturned() to zero supply should work", async () => {
Expand Down
16 changes: 8 additions & 8 deletions test/contracts/curves/allCurves.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ const setup = async () => {
calculateCollateralReturned,
calculateTokenReturned,
calculateTokenReturnedFromZero,
precision: 0.00000000000000000001,
precision: 0.000000000001,
};

// Second Curve
Expand Down Expand Up @@ -126,14 +126,14 @@ const setup = async () => {
calculateCollateralReturned,
calculateTokenReturned,
calculateTokenReturnedFromZero,
precision: 0.00000000000000000001,
precision: 0.000000000001,
};

// third curve
const bancorABDK3 = await deploy<BancorABDK>("BancorABDK");

baseY = one.mul(1);
reserveWeight = 150000;
reserveWeight = 100000;
targetReserveWeight = reserveWeight + 10000;
encodedCurveDetails = ethers.utils.defaultAbiCoder.encode(
["uint256", "uint32"],
Expand Down Expand Up @@ -165,14 +165,14 @@ const setup = async () => {
calculateCollateralReturned,
calculateTokenReturned,
calculateTokenReturnedFromZero,
precision: 0.00000000000000000001,
precision: 0.000000000001,
};

// fourth curve
const bancorABDK4 = await deploy<BancorABDK>("BancorABDK");

baseY = one.mul(1);
reserveWeight = 150000;
reserveWeight = 100000;
targetReserveWeight = reserveWeight - 20000;
encodedCurveDetails = ethers.utils.defaultAbiCoder.encode(
["uint256", "uint32"],
Expand Down Expand Up @@ -204,7 +204,7 @@ const setup = async () => {
calculateCollateralReturned,
calculateTokenReturned,
calculateTokenReturnedFromZero,
precision: 0.00000000000000000001,
precision: 0.000000000001,
};

// fifth curve
Expand Down Expand Up @@ -243,7 +243,7 @@ const setup = async () => {
calculateCollateralReturned,
calculateTokenReturned,
calculateTokenReturnedFromZero,
precision: 0.00000000000000000001,
precision: 0.000000000001,
};

// sixth curve
Expand Down Expand Up @@ -282,7 +282,7 @@ const setup = async () => {
calculateCollateralReturned,
calculateTokenReturned,
calculateTokenReturnedFromZero,
precision: 0.00000000000000000001,
precision: 0.000000000001,
};

return [curve1, curve2, curve3, curve4, curve5, curve6];
Expand Down
6 changes: 4 additions & 2 deletions test/contracts/curves/helper/curvesTestsHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,8 +216,10 @@ export const curvesTestsHelper = async ({
999999999999999,
reserveWeight / MAX_WEIGHT
);
// precision is not as high as usual
expect(toETHNumber(estimate)).to.be.approximately(calculatedRes, 0.00001);
expect(toETHNumber(estimate)).to.be.approximately(
calculatedRes,
0.000000000001
);
});

it("initReconfigure() should work", async () => {
Expand Down
Loading

0 comments on commit 0102f0c

Please sign in to comment.