Skip to content

Commit

Permalink
Fix FraxETH strategy tests (#1854)
Browse files Browse the repository at this point in the history
* Fix FraxETH strategy tests

* Fix fraxETH Redeem test and move WETH replacement to mocks

* Remove unnecessary approve
  • Loading branch information
shahthepro authored Oct 5, 2023
1 parent c6cc86f commit 829a127
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 59 deletions.
20 changes: 11 additions & 9 deletions contracts/contracts/mocks/MocksfrxETH.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import "./MintableERC20.sol";

contract MocksfrxETH is MintableERC20 {
address public frxETH;
address public sfrxETH;
mapping(address => uint256) public assetBalance;

constructor(address _frxETH) ERC20("sfrxETH", "sfrxETH") {
frxETH = _frxETH;
Expand All @@ -22,27 +20,32 @@ contract MocksfrxETH is MintableERC20 {
{
ERC20(frxETH).transferFrom(msg.sender, address(this), assets);

assetBalance[receiver] += assets;
_mint(receiver, assets);

return assets;
}

function maxWithdraw(address owner) external view returns (uint256) {
return assetBalance[owner];
return balanceOf(owner);
}

function setMaxWithdrawableBalance(address owner, uint256 balance)
external
{
assetBalance[owner] = balance;
uint256 currentBalance = balanceOf(owner);
if (currentBalance > balance) {
_burn(owner, currentBalance - balance);
} else if (balance > currentBalance) {
_mint(owner, balance - currentBalance);
}
}

function redeem(
uint256 shares,
address receiver,
address owner
) external returns (uint256 assets) {
assetBalance[owner] -= shares;
_burn(owner, shares);

ERC20(frxETH).transfer(receiver, shares);

Expand All @@ -54,7 +57,7 @@ contract MocksfrxETH is MintableERC20 {
address receiver,
address owner
) external returns (uint256 shares) {
assetBalance[owner] -= shares;
_burn(owner, shares);

ERC20(frxETH).transfer(receiver, shares);

Expand All @@ -66,8 +69,7 @@ contract MocksfrxETH is MintableERC20 {
payable
returns (uint256 shares)
{
assetBalance[recipient] += msg.value;

_mint(recipient, msg.value);
shares = msg.value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,11 @@ contract InitializeGovernedUpgradeabilityProxy is Governable {
* It should include the signature and the parameters of the function to be called, as described in
* https://solidity.readthedocs.io/en/v0.4.24/abi-spec.html#function-selector-and-argument-encoding.
*/
function upgradeToAndCall(
address newImplementation,
bytes calldata data
) external payable onlyGovernor {
function upgradeToAndCall(address newImplementation, bytes calldata data)
external
payable
onlyGovernor
{
_upgradeTo(newImplementation);
(bool success, ) = newImplementation.delegatecall(data);
require(success);
Expand Down
9 changes: 8 additions & 1 deletion contracts/deploy/000_mock.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
const { parseUnits } = require("ethers").utils;
const { isMainnetOrFork } = require("../test/helpers");
const { threeCRVPid } = require("../utils/constants");
const { replaceContractAt } = require("../utils/deploy");

const addresses = require("../utils/addresses");

const {
abi: FACTORY_ABI,
Expand Down Expand Up @@ -105,6 +108,11 @@ const deployMocks = async ({ getNamedAccounts, deployments }) => {
const usdc = await ethers.getContract("MockUSDC");
const usdt = await ethers.getContract("MockUSDT");

// Replace WETH
const mockWETH = await ethers.getContract("MockWETH");
await replaceContractAt(addresses.mainnet.WETH, mockWETH);
const weth = await ethers.getContractAt("MockWETH", addresses.mainnet.WETH);

// Deploy mock aTokens (Aave)
// MockAave is the mock lendingPool
const lendingPool = await ethers.getContract("MockAave");
Expand Down Expand Up @@ -320,7 +328,6 @@ const deployMocks = async ({ getNamedAccounts, deployments }) => {
},
});

const weth = await ethers.getContract("MockWETH");
await deploy("MockUniswapV3Router", {
from: deployerAddr,
contract: {
Expand Down
34 changes: 6 additions & 28 deletions contracts/test/_fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const { formatUnits } = require("ethers/lib/utils");

const addresses = require("../utils/addresses");
const { setFraxOraclePrice } = require("../utils/frax");
const { replaceContractAt } = require("../utils/deploy");
const {
balancer_rETH_WETH_PID,
balancer_stETH_WETH_PID,
Expand Down Expand Up @@ -380,7 +381,7 @@ const defaultFixture = deployments.createFixture(async () => {
dai = await ethers.getContract("MockDAI");
tusd = await ethers.getContract("MockTUSD");
usdc = await ethers.getContract("MockUSDC");
weth = await ethers.getContract("MockWETH");
weth = await ethers.getContractAt("MockWETH", addresses.mainnet.WETH);
ogn = await ethers.getContract("MockOGN");
LUSD = await ethers.getContract("MockLUSD");
ogv = await ethers.getContract("MockOGV");
Expand Down Expand Up @@ -653,7 +654,7 @@ async function oethDefaultFixture() {
const fixture = await defaultFixture();

const { weth, reth, stETH, frxETH, sfrxETH } = fixture;
const { matt, josh, domen, daniel, franck, governor, oethVault } = fixture;
const { matt, josh, domen, daniel, franck, oethVault } = fixture;

if (isFork) {
for (const user of [matt, josh, domen, daniel, franck]) {
Expand All @@ -675,27 +676,15 @@ async function oethDefaultFixture() {
);
await mockedMinter.connect(franck).setAssetAddress(fixture.sfrxETH.address);

// Replace WETH contract with MockWETH
const mockWETH = await ethers.getContract("MockWETH");
await replaceContractAt(addresses.mainnet.WETH, mockWETH);
const stubbedWETH = await ethers.getContractAt(
"MockWETH",
addresses.mainnet.WETH
);
fixture.weth = stubbedWETH;

// And Fund it
_hardhatSetBalance(stubbedWETH.address, "999999999999999");

// And make sure vault knows about it
await oethVault.connect(governor).supportAsset(addresses.mainnet.WETH, 0);
// Fund WETH contract
_hardhatSetBalance(weth.address, "999999999999999");

// Fund all with mockTokens
await fundAccountsForOETHUnitTests();

// Reset allowances
for (const user of [matt, josh, domen, daniel, franck]) {
for (const asset of [stubbedWETH, reth, stETH, frxETH, sfrxETH]) {
for (const asset of [weth, reth, stETH, frxETH, sfrxETH]) {
await resetAllowance(asset, user, oethVault.address);
}
}
Expand Down Expand Up @@ -2117,16 +2106,6 @@ async function fluxStrategyFixture() {
return fixture;
}

async function replaceContractAt(targetAddress, mockContract) {
const signer = (await hre.ethers.getSigners())[0];
const mockCode = await signer.provider.getCode(mockContract.address);

await hre.network.provider.request({
method: "hardhat_setCode",
params: [targetAddress, mockCode],
});
}

/**
* A fixture is a setup function that is run only the first time it's invoked. On subsequent invocations,
* Hardhat will reset the state of the network to what it was at the point after the fixture was initially executed.
Expand Down Expand Up @@ -2200,7 +2179,6 @@ module.exports = {
fraxETHStrategyFixture,
oethMorphoAaveFixture,
mintWETH,
replaceContractAt,
oeth1InchSwapperFixture,
oethCollateralSwapFixture,
ousdCollateralSwapFixture,
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ const getAssetAddresses = async (deployments) => {
cUSDC: (await deployments.get("MockCUSDC")).address,
cUSDT: (await deployments.get("MockCUSDT")).address,
NonStandardToken: (await deployments.get("MockNonStandardToken")).address,
WETH: (await deployments.get("MockWETH")).address,
WETH: addresses.mainnet.WETH,
COMP: (await deployments.get("MockCOMP")).address,
ThreePool: (await deployments.get("MockCurvePool")).address,
ThreePoolToken: (await deployments.get("Mock3CRV")).address,
Expand Down
3 changes: 2 additions & 1 deletion contracts/test/strategies/fraxeth.fork-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ forkOnlyDescribe("ForkTest: FraxETH Strategy", function () {
.withdraw(weth.address, weth.address, oethUnits("12"))
).to.be.revertedWith("Unexpected asset address");
});
it("Should allow withdrawAll twice", async () => {
// TODO: Reenable this after FraxETH strategy has been upgraded
it.skip("Should allow withdrawAll twice", async () => {
const { oethVault, fraxEthStrategy } = fixture;
const fakeVaultSigner = await impersonateAndFundContract(
oethVault.address
Expand Down
18 changes: 7 additions & 11 deletions contracts/test/strategies/fraxeth.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { expect } = require("chai");

const { oethUnits, units } = require("../helpers");
const { oethUnits, units, ethUnits } = require("../helpers");

const {
createFixtureLoader,
Expand Down Expand Up @@ -39,8 +39,7 @@ describe("FraxETH Strategy", function () {
});

describe("Redeem", function () {
// TODO: Debug and fix this test
it.skip("Should allow redeem", async () => {
it("Should allow redeem", async () => {
const {
daniel,
domen,
Expand All @@ -58,9 +57,6 @@ describe("FraxETH Strategy", function () {
const assets = [frxETH, weth, reth, stETH];
const mintAmounts = ["10.2333", "20.45", "23.456", "15.3434"];

// Just assuming a 1:1 for all assets for simplicity of tests
await reth.connect(daniel).setExchangeRate(oethUnits("1"));

// Rebase & Allocate
await oethVault.connect(daniel).rebase();
await oethVault.connect(daniel).allocate();
Expand All @@ -72,10 +68,6 @@ describe("FraxETH Strategy", function () {
.mint(assets[i].address, oethUnits(mintAmounts[i]), "0");
}

// Rebase & Allocate
await oethVault.connect(daniel).rebase();
await oethVault.connect(daniel).allocate();

// Now try redeeming
const supplyBeforeRedeem = await oeth.totalSupply();
const userAssetBalanceBeforeRedeem = await Promise.all(
Expand Down Expand Up @@ -108,8 +100,12 @@ describe("FraxETH Strategy", function () {
// User should have got other assets
let netGainedAssetValue = BigNumber.from(0);
for (let i = 0; i < assets.length; i++) {
const redeemPrice = await oethVault.priceUnitRedeem(assets[i].address);
netGainedAssetValue = netGainedAssetValue.add(
userAssetBalanceAfterRedeem[i].sub(userAssetBalanceBeforeRedeem[i])
userAssetBalanceAfterRedeem[i]
.sub(userAssetBalanceBeforeRedeem[i])
.mul(redeemPrice)
.div(ethUnits("1"))
);
}
expect(netGainedAssetValue).to.approxEqualTolerance(
Expand Down
4 changes: 0 additions & 4 deletions contracts/test/vault/oneinch-swapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -635,8 +635,6 @@ describe("1Inch Swapper", () => {
.connect(strategist)
.mintTo(mock1InchSwapRouter.address, toAmount.mul(2));

await swapper1Inch.approveAssets([weth.address]);

const tx = swapper1Inch
.connect(strategist)
.swap(weth.address, frxETH.address, fromAmount, toAmount, data);
Expand Down Expand Up @@ -690,8 +688,6 @@ describe("1Inch Swapper", () => {
.connect(strategist)
.mintTo(swapper1Inch.address, toAmount.mul(2));

await swapper1Inch.approveAssets([weth.address]);

const tx = swapper1Inch
.connect(strategist)
.swap(weth.address, frxETH.address, fromAmount, toAmount, data);
Expand Down
11 changes: 11 additions & 0 deletions contracts/utils/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -1248,6 +1248,16 @@ function deploymentWithGuardianGovernor(opts, fn) {
return main;
}

async function replaceContractAt(targetAddress, mockContract) {
const signer = (await hre.ethers.getSigners())[0];
const mockCode = await signer.provider.getCode(mockContract.address);

await hre.network.provider.request({
method: "hardhat_setCode",
params: [targetAddress, mockCode],
});
}

module.exports = {
log,
sleep,
Expand All @@ -1261,4 +1271,5 @@ module.exports = {
deploymentWithProposal,
deploymentWithGovernanceProposal,
deploymentWithGuardianGovernor,
replaceContractAt,
};

0 comments on commit 829a127

Please sign in to comment.