Skip to content

Commit

Permalink
Merge pull request #54 from meTokens/test/refundRatio
Browse files Browse the repository at this point in the history
Test/refund ratio
  • Loading branch information
zgorizzo69 authored Nov 19, 2021
2 parents d258698 + 630fd17 commit c62bf56
Show file tree
Hide file tree
Showing 7 changed files with 620 additions and 131 deletions.
1 change: 0 additions & 1 deletion contracts/Foundry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ contract Foundry is IFoundry, Ownable, Initializable {
) {
meToken_ = meTokenRegistry.finishResubscribe(_meToken);
}

// Calculate how many tokens tokens are returned
uint256 tokensReturned = calculateBurnReturn(_meToken, _meTokensBurned);

Expand Down
61 changes: 36 additions & 25 deletions contracts/Hub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,17 @@ contract Hub is Ownable, Initializable {
bytes memory _encodedCurveDetails
) external {
Details.Hub storage hub_ = _hubs[_id];
if (hub_.updating && block.timestamp > hub_.endTime) {
Details.Hub memory hubUpdated = finishUpdate(_id);
hub_.refundRatio = hubUpdated.refundRatio;
hub_.targetRefundRatio = hubUpdated.targetRefundRatio;
hub_.curve = hubUpdated.curve;
hub_.targetCurve = hubUpdated.targetCurve;
hub_.reconfigure = hubUpdated.reconfigure;
hub_.updating = hubUpdated.updating;
hub_.startTime = hubUpdated.startTime;
hub_.endTime = hubUpdated.endTime;
}
require(!hub_.updating, "already updating");
require(block.timestamp >= hub_.endCooldown, "Still cooling down");
if (_targetRefundRatio != 0) {
Expand Down Expand Up @@ -113,31 +124,6 @@ contract Hub is Ownable, Initializable {
hub_.endCooldown = block.timestamp + _warmup + _duration + _cooldown;
}

function finishUpdate(uint256 id) external returns (Details.Hub memory) {
Details.Hub storage hub_ = _hubs[id];
require(block.timestamp > hub_.endTime, "Still updating");

if (hub_.targetRefundRatio != 0) {
hub_.refundRatio = hub_.targetRefundRatio;
hub_.targetRefundRatio = 0;
}

if (hub_.reconfigure) {
if (hub_.targetCurve == address(0)) {
ICurve(hub_.curve).finishReconfigure(id);
} else {
hub_.curve = hub_.targetCurve;
hub_.targetCurve = address(0);
}
hub_.reconfigure = false;
}

hub_.updating = false;
hub_.startTime = 0;
hub_.endTime = 0;
return hub_;
}

function setWarmup(uint256 warmup_) external onlyOwner {
require(warmup_ != _warmup, "warmup_ == _warmup");
_warmup = warmup_;
Expand Down Expand Up @@ -176,4 +162,29 @@ contract Hub is Ownable, Initializable {
function getCooldown() external view returns (uint256) {
return _cooldown;
}

function finishUpdate(uint256 id) public returns (Details.Hub memory) {
Details.Hub storage hub_ = _hubs[id];
require(block.timestamp > hub_.endTime, "Still updating");

if (hub_.targetRefundRatio != 0) {
hub_.refundRatio = hub_.targetRefundRatio;
hub_.targetRefundRatio = 0;
}

if (hub_.reconfigure) {
if (hub_.targetCurve == address(0)) {
ICurve(hub_.curve).finishReconfigure(id);
} else {
hub_.curve = hub_.targetCurve;
hub_.targetCurve = address(0);
}
hub_.reconfigure = false;
}

hub_.updating = false;
hub_.startTime = 0;
hub_.endTime = 0;
return hub_;
}
}
125 changes: 125 additions & 0 deletions test/contracts/libs/WeightedAverage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import { BigNumber } from "@ethersproject/bignumber";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { expect } from "chai";
import { ethers } from "hardhat";
import { WeightedAverage } from "../../../artifacts/types/WeightedAverage";
import { deploy, weightedAverageSimulation } from "../../utils/helpers";

describe("WeightedAverage.sol", () => {
let wa: WeightedAverage;
let account0: SignerWithAddress;
let account1: SignerWithAddress;
before(async () => {
[account0, account1] = await ethers.getSigners();
wa = await deploy<WeightedAverage>("WeightedAverage");

await wa.deployed();
});

describe("calculate()", () => {
it("Returns amount if block.timestamp < startTime", async () => {
const block = await ethers.provider.getBlock("latest");

const cur = await wa.calculate(42, 245646, block.timestamp + 1, 1);
expect(cur).to.equal(42);
});
it("Returns targetAmount if block.timestamp > endTime", async () => {
const block = await ethers.provider.getBlock("latest");
const cur = await wa.calculate(
42,
245646,
block.timestamp - 2,
block.timestamp - 1
);
expect(cur).to.equal(245646);
});
it("works with different amount and target amount", async () => {
const block = await ethers.provider.getBlock("latest");
const amount = "100";
const targetAmount = "1000";
const startTime = block.timestamp - 50;
const endTime = block.timestamp + 50;
const cur = await wa.calculate(
ethers.utils.parseEther(amount),
ethers.utils.parseEther(targetAmount),
startTime,
endTime
);
const calcRes = weightedAverageSimulation(
Number(amount),
Number(targetAmount),
startTime,
endTime,
block.timestamp
);
expect(calcRes).to.equal(550);
expect(Number(ethers.utils.formatEther(cur))).to.equal(calcRes);
});
it("works at the begining of migration", async () => {
const block = await ethers.provider.getBlock("latest");
const amount = "0";
const targetAmount = "10";
const startTime = block.timestamp - 1;
const endTime = block.timestamp + 9;
const cur = await wa.calculate(
ethers.utils.parseEther(amount),
ethers.utils.parseEther(targetAmount),
startTime,
endTime
);
const calcRes = weightedAverageSimulation(
Number(amount),
Number(targetAmount),
startTime,
endTime,
block.timestamp
);
expect(calcRes).to.equal(1);
expect(Number(ethers.utils.formatEther(cur))).to.equal(calcRes);
});
it("works in the middle of migration", async () => {
const block = await ethers.provider.getBlock("latest");
const amount = "0";
const targetAmount = "10";
const startTime = block.timestamp - 5;
const endTime = block.timestamp + 5;
const cur = await wa.calculate(
ethers.utils.parseEther(amount),
ethers.utils.parseEther(targetAmount),
startTime,
endTime
);
const calcRes = weightedAverageSimulation(
Number(amount),
Number(targetAmount),
startTime,
endTime,
block.timestamp
);
expect(calcRes).to.equal(5);
expect(Number(ethers.utils.formatEther(cur))).to.equal(calcRes);
});
it("works at the end of migration", async () => {
const block = await ethers.provider.getBlock("latest");
const amount = "0";
const targetAmount = "10";
const startTime = block.timestamp - 9;
const endTime = block.timestamp + 1;
const cur = await wa.calculate(
ethers.utils.parseEther(amount),
ethers.utils.parseEther(targetAmount),
startTime,
endTime
);
const calcRes = weightedAverageSimulation(
Number(amount),
Number(targetAmount),
startTime,
endTime,
block.timestamp
);
expect(calcRes).to.equal(9);
expect(Number(ethers.utils.formatEther(cur))).to.equal(calcRes);
});
});
});
2 changes: 1 addition & 1 deletion test/contracts/migrations/UniswapSingleTransfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { CurveRegistry } from "../../../artifacts/types/CurveRegistry";
import { MigrationRegistry } from "../../../artifacts/types/MigrationRegistry";
import { SingleAssetVault } from "../../../artifacts/types/SingleAssetVault";
import { MeToken } from "../../../artifacts/types/MeToken";
import { impersonate, mineBlock, passOneHour } from "../../utils/hardhatNode";
import { impersonate, mineBlock, passHours } from "../../utils/hardhatNode";
import { UniswapSingleTransfer } from "../../../artifacts/types/UniswapSingleTransfer";
import { hubSetup } from "../../utils/hubSetup";
import { expect } from "chai";
Expand Down
Loading

0 comments on commit c62bf56

Please sign in to comment.