Skip to content
This repository has been archived by the owner on Aug 26, 2024. It is now read-only.

retro tests #32

Merged
merged 15 commits into from
Sep 12, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ contract LeveredPositionFactoryFirstExtension is
error PositionNotClosed();

function _getExtensionFunctions() external pure override returns (bytes4[] memory) {
uint8 fnsCount = 8;
uint8 fnsCount = 9;
bytes4[] memory functionSelectors = new bytes4[](fnsCount);
functionSelectors[--fnsCount] = this.removeClosedPosition.selector;
functionSelectors[--fnsCount] = this.closeAndRemoveUserPosition.selector;
functionSelectors[--fnsCount] = this.getMinBorrowNative.selector;
functionSelectors[--fnsCount] = this.getRedemptionStrategies.selector;
functionSelectors[--fnsCount] = this.getBorrowableMarketsByCollateral.selector;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,8 +255,8 @@ contract LiquidatorsRegistryExtension is LiquidatorsRegistryStorage, DiamondExte
strategyData = solidlyLpTokenLiquidatorData(inputToken, outputToken);
} else if (isStrategy(strategy, "UniswapV2LiquidatorFunder")) {
strategyData = uniswapV2LiquidatorData(inputToken, outputToken);
} else if (isStrategy(strategy, "UniswapV3Liquidator")) {
strategyData = uniswapV3LiquidatorData(inputToken, outputToken);
} else if (isStrategy(strategy, "UniswapV3LiquidatorFunder")) {
strategyData = uniswapV3LiquidatorFunderData(inputToken, outputToken);
} else if (isStrategy(strategy, "AlgebraSwapLiquidator")) {
strategyData = algebraSwapLiquidatorData(inputToken, outputToken);
} else if (isStrategy(strategy, "GammaAlgebraLpTokenLiquidator")) {
Expand Down Expand Up @@ -433,7 +433,7 @@ contract LiquidatorsRegistryExtension is LiquidatorsRegistryStorage, DiamondExte
strategyData = abi.encode(outputToken);
}

function uniswapV3LiquidatorData(IERC20Upgradeable inputToken, IERC20Upgradeable outputToken)
function uniswapV3LiquidatorFunderData(IERC20Upgradeable inputToken, IERC20Upgradeable outputToken)
internal
view
returns (bytes memory strategyData)
Expand Down
6 changes: 5 additions & 1 deletion contracts/test/AccrueInterestTest.t.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.0;

import { IERC20Upgradeable } from "openzeppelin-contracts-upgradeable/contracts/token/ERC20/IERC20Upgradeable.sol";

import { UpgradesBaseTest } from "./UpgradesBaseTest.sol";
import { CErc20Delegate } from "../compound/CErc20Delegate.sol";
import { ICErc20 } from "../compound/CTokenInterfaces.sol";
Expand Down Expand Up @@ -183,9 +185,11 @@ contract AccrueInterestTest is UpgradesBaseTest {
function testMintGated() public fork(POLYGON_MAINNET) {
address newMarket = 0x26EA46e975778662f98dAa0E7a12858dA9139262;
address assetWhale = 0xEd41f5967252248412E6C69475ae8a5A4274A6f8;

// approve spending
vm.startPrank(assetWhale);
IERC20Upgradeable(CErc20Delegate(newMarket).underlying()).approve(newMarket, 1e6);
require(CErc20Delegate(newMarket).mint(1e6) == 0, "!mint failed");
vm.stopPrank();
}

function testDeployCToken() public debuggingOnly fork(POLYGON_MAINNET) {
Expand Down
6 changes: 4 additions & 2 deletions contracts/test/AnyLiquidationTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ contract AnyLiquidationTest is BaseTest {
}

function testSpecificRandom() public debuggingOnly {
testBscAnyLiquidation(1668);
testPolygonAnyLiquidation(14341);
// testPolygonAnyLiquidation(101);
}

Expand Down Expand Up @@ -223,7 +223,7 @@ contract AnyLiquidationTest is BaseTest {
(, PoolDirectory.Pool[] memory pools) = PoolDirectory(ap.getAddress("PoolDirectory")).getActivePools();

uint256 initRandom = random;
while (random - initRandom < 100) {
while (true) {
// get a random pool and a random borrower from it
(vars.comptroller, vars.borrower) = getPoolAndBorrower(random, pools);

Expand All @@ -240,6 +240,8 @@ contract AnyLiquidationTest is BaseTest {
}
}
}
// fail gracefully when there are no positions to liquidate
if (random - initRandom < 100) return;
random++;
}

Expand Down
1 change: 1 addition & 0 deletions contracts/test/AuthoritiesRegistryTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ contract AuthoritiesRegistryTest is WithPool {
vm.expectRevert("not owner or pool");
registry.reconfigureAuthority(address(comptroller));

vm.prank(registry.owner());
registry.reconfigureAuthority(address(comptroller));
}

Expand Down
167 changes: 138 additions & 29 deletions contracts/test/LeveredPositionTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { ILeveredPositionFactory } from "../ionic/levered/ILeveredPositionFactor
import { LeveredPositionsLens } from "../ionic/levered/LeveredPositionsLens.sol";
import { LiquidatorsRegistry } from "../liquidators/registry/LiquidatorsRegistry.sol";
import { LiquidatorsRegistryExtension } from "../liquidators/registry/LiquidatorsRegistryExtension.sol";
import { LiquidatorsRegistrySecondExtension } from "../liquidators/registry/LiquidatorsRegistrySecondExtension.sol";
import { ILiquidatorsRegistry } from "../liquidators/registry/ILiquidatorsRegistry.sol";
import { IRedemptionStrategy } from "../liquidators/IRedemptionStrategy.sol";
import { ICErc20 } from "../compound/CTokenInterfaces.sol";
Expand Down Expand Up @@ -91,8 +92,7 @@ contract LeveredPositionFactoryTest is BaseTest {

function afterForkSetUp() internal override {
factory = ILeveredPositionFactory(ap.getAddress("LeveredPositionFactory"));
lens = new LeveredPositionsLens();
lens.initialize(factory);
lens = LeveredPositionsLens(ap.getAddress("LeveredPositionsLens"));
}

function testChapelNetApy() public debuggingOnly fork(BSC_CHAPEL) {
Expand Down Expand Up @@ -157,40 +157,38 @@ abstract contract LeveredPositionTest is MarketsTest {
function afterForkSetUp() internal virtual override {
super.afterForkSetUp();

if (block.chainid == BSC_MAINNET) {
vm.prank(ap.owner());
ap.setAddress("ALGEBRA_SWAP_ROUTER", 0x327Dd3208f0bCF590A66110aCB6e5e6941A4EfA0);
}

registry = ILiquidatorsRegistry(ap.getAddress("LiquidatorsRegistry"));
factory = ILeveredPositionFactory(ap.getAddress("LeveredPositionFactory"));
{
// upgrade the factory
LeveredPositionFactoryFirstExtension newExt1 = new LeveredPositionFactoryFirstExtension();
LeveredPositionFactorySecondExtension newExt2 = new LeveredPositionFactorySecondExtension();

vm.startPrank(factory.owner());
DiamondBase asBase = DiamondBase(address(factory));
address[] memory oldExts = asBase._listExtensions();
if (oldExts.length == 1) {
asBase._registerExtension(newExt1, DiamondExtension(oldExts[0]));
asBase._registerExtension(newExt2, DiamondExtension(address(0)));
} else if (oldExts.length == 2) {
asBase._registerExtension(newExt1, DiamondExtension(oldExts[0]));
asBase._registerExtension(newExt2, DiamondExtension(oldExts[1]));
}
vm.stopPrank();
}
registry = factory.liquidatorsRegistry();
// {
// // upgrade the factory
// LeveredPositionFactoryFirstExtension newExt1 = new LeveredPositionFactoryFirstExtension();
// LeveredPositionFactorySecondExtension newExt2 = new LeveredPositionFactorySecondExtension();
//
// vm.startPrank(factory.owner());
// DiamondBase asBase = DiamondBase(address(factory));
// address[] memory oldExts = asBase._listExtensions();
//
// if (oldExts.length == 1) {
// asBase._registerExtension(newExt1, DiamondExtension(oldExts[0]));
// asBase._registerExtension(newExt2, DiamondExtension(address(0)));
// } else if (oldExts.length == 2) {
// asBase._registerExtension(newExt1, DiamondExtension(oldExts[0]));
// asBase._registerExtension(newExt2, DiamondExtension(oldExts[1]));
// }
// vm.stopPrank();
// }

lens = LeveredPositionsLens(ap.getAddress("LeveredPositionsLens"));
}

function upgradeRegistry() internal {
DiamondBase asBase = DiamondBase(address(registry));
address[] memory exts = asBase._listExtensions();
LiquidatorsRegistryExtension newExt = new LiquidatorsRegistryExtension();
LiquidatorsRegistryExtension newExt1 = new LiquidatorsRegistryExtension();
LiquidatorsRegistrySecondExtension newExt2 = new LiquidatorsRegistrySecondExtension();
vm.prank(SafeOwnable(address(registry)).owner());
asBase._registerExtension(newExt, DiamondExtension(exts[0]));
asBase._registerExtension(newExt1, DiamondExtension(exts[0]));
asBase._registerExtension(newExt2, DiamondExtension(exts[1]));
}

function upgradePoolAndMarkets() internal {
Expand Down Expand Up @@ -220,15 +218,15 @@ abstract contract LeveredPositionTest is MarketsTest {
function _configurePair(address _collat, address _stable) internal {
collateralMarket = ICErc20(_collat);
stableMarket = ICErc20(_stable);

//upgradePoolAndMarkets();
_unpauseMarkets(_collat, _stable);
//_unpauseMarkets(_collat, _stable);
vm.prank(factory.owner());
factory._setPairWhitelisted(collateralMarket, stableMarket, true);
}

function _whitelistTestUser(address user) internal {
address pool = address(collateralMarket.comptroller());
ffd.authoritiesRegistry().leveredPositionsFactory();
PoolRolesAuthority pra = ffd.authoritiesRegistry().poolsAuthorities(pool);

vm.startPrank(pra.owner());
Expand Down Expand Up @@ -922,6 +920,117 @@ contract PearlUsdrMaticUsdrLpLeveredPositionTest is LeveredPositionTest {
}
}

contract RetroCashAUsdcCashLeveredPositionTest is LeveredPositionTest {
function setUp() public fork(POLYGON_MAINNET) {}

function afterForkSetUp() internal override {
super.afterForkSetUp();
upgradeRegistry();

uint256 depositAmount = 700e18;

// LP token underlying xCASH-USDC
address lpTokenMarket = 0x1D2A7078a404ab970f951d5A6dbECD9e24838FB6;
address cashMarket = 0xf69207CFDe6228A1e15A34F2b0c4fDe0845D9eBa;
address lpTokenWhale = 0x38e481367E0c50f4166AD2A1C9fde0E3c662CFBa;
address cashWhale = 0x88C522E526E5Eea8d636fd6805cA7fEB488780D0;

_configurePair(lpTokenMarket, cashMarket);
_fundMarketAndSelf(ICErc20(lpTokenMarket), lpTokenWhale);
_fundMarketAndSelf(ICErc20(cashMarket), cashWhale);

(position, maxLevRatio, minLevRatio) = _openLeveredPosition(address(this), depositAmount);
}
}

contract RetroUsdcAUsdcCashLeveredPositionTest is LeveredPositionTest {
function setUp() public fork(POLYGON_MAINNET) {}

function afterForkSetUp() internal override {
super.afterForkSetUp();

uint256 depositAmount = 700e18;

// LP token underlying xCASH-USDC
address lpTokenMarket = 0x1D2A7078a404ab970f951d5A6dbECD9e24838FB6;
address usdcMarket = 0x38EbA94210bCEf3F9231E1764EE230abC14D1cbc;
address lpTokenWhale = 0x38e481367E0c50f4166AD2A1C9fde0E3c662CFBa;
address usdcWhale = 0x5a52E96BAcdaBb82fd05763E25335261B270Efcb;

_configurePair(lpTokenMarket, usdcMarket);
_fundMarketAndSelf(ICErc20(lpTokenMarket), lpTokenWhale);
_fundMarketAndSelf(ICErc20(usdcMarket), usdcWhale);

(position, maxLevRatio, minLevRatio) = _openLeveredPosition(address(this), depositAmount);
}
}

contract RetroUsdcAUsdcWethLeveredPositionTest is LeveredPositionTest {
function setUp() public fork(POLYGON_MAINNET) {}

function afterForkSetUp() internal override {
super.afterForkSetUp();

uint256 depositAmount = 8e18;

// LP token underlying xUSDC-WETH05
address lpTokenMarket = 0xC7cA03A0bE1dBAc350E5BfE5050fC5af6406490E;
address usdcMarket = 0x38EbA94210bCEf3F9231E1764EE230abC14D1cbc;
address lpTokenWhale = 0x38e481367E0c50f4166AD2A1C9fde0E3c662CFBa;
address usdcWhale = 0x5a52E96BAcdaBb82fd05763E25335261B270Efcb;

_configurePair(lpTokenMarket, usdcMarket);
_fundMarketAndSelf(ICErc20(lpTokenMarket), lpTokenWhale);
_fundMarketAndSelf(ICErc20(usdcMarket), usdcWhale);

(position, maxLevRatio, minLevRatio) = _openLeveredPosition(address(this), depositAmount);
}
}

contract RetroCashAUsdcWethLeveredPositionTest is LeveredPositionTest {
function setUp() public fork(POLYGON_MAINNET) {}

function afterForkSetUp() internal override {
super.afterForkSetUp();

uint256 depositAmount = 8e18;

// LP token underlying xUSDC-WETH05
address lpTokenMarket = 0xC7cA03A0bE1dBAc350E5BfE5050fC5af6406490E;
address cashMarket = 0xf69207CFDe6228A1e15A34F2b0c4fDe0845D9eBa;
address lpTokenWhale = 0x38e481367E0c50f4166AD2A1C9fde0E3c662CFBa;
address cashWhale = 0x88C522E526E5Eea8d636fd6805cA7fEB488780D0;

_configurePair(lpTokenMarket, cashMarket);
_fundMarketAndSelf(ICErc20(lpTokenMarket), lpTokenWhale);
_fundMarketAndSelf(ICErc20(cashMarket), cashWhale);

(position, maxLevRatio, minLevRatio) = _openLeveredPosition(address(this), depositAmount);
}
}

contract RetroCashAWbtcWethLeveredPositionTest is LeveredPositionTest {
function setUp() public fork(POLYGON_MAINNET) {}

function afterForkSetUp() internal override {
super.afterForkSetUp();

uint256 depositAmount = 5e18;

// LP token underlying xWBTC-WETH05
address lpTokenMarket = 0xCB1a06eff3459078c26516ae3a1dB44A61D2DbCA;
address cashMarket = 0xf69207CFDe6228A1e15A34F2b0c4fDe0845D9eBa;
address lpTokenWhale = 0x38e481367E0c50f4166AD2A1C9fde0E3c662CFBa;
address cashWhale = 0x88C522E526E5Eea8d636fd6805cA7fEB488780D0;

_configurePair(lpTokenMarket, cashMarket);
_fundMarketAndSelf(ICErc20(lpTokenMarket), lpTokenWhale);
_fundMarketAndSelf(ICErc20(cashMarket), cashWhale);

(position, maxLevRatio, minLevRatio) = _openLeveredPosition(address(this), depositAmount);
}
}

/*
contract XYLeveredPositionTest is LeveredPositionTest {
function setUp() public fork(X_CHAIN_ID) {}
Expand Down
13 changes: 13 additions & 0 deletions contracts/test/LiquidatorsRegistryTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,17 @@ contract LiquidatorsRegistryTest is BaseTest {

_swap(tusdWhale, inputToken, inputAmount, outputToken, 5e16);
}

function testRedemptionPathCashAUsdcWeth() public debuggingOnly fork(POLYGON_MAINNET) {
IERC20Upgradeable cash = IERC20Upgradeable(0x5D066D022EDE10eFa2717eD3D79f22F949F8C175);
IERC20Upgradeable usdc = IERC20Upgradeable(0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174);
IERC20Upgradeable aUsdcWeth = IERC20Upgradeable(0xe058e1FfFF9B13d3FCd4803FDb55d1Cc2fe07DDC);

upgradeRegistry();

registry.redemptionStrategiesByTokens(cash, aUsdcWeth);
registry.redemptionStrategiesByTokens(aUsdcWeth, cash);
registry.redemptionStrategiesByTokens(cash, usdc);
registry.redemptionStrategiesByTokens(usdc, cash);
}
}
22 changes: 22 additions & 0 deletions contracts/test/liquidators/GammaLpTokenLiquidatorTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,26 @@ contract GammaLpTokenLiquidatorTest is BaseTest {
assertEq(IERC20Upgradeable(wtoken).balanceOf(address(uWrapper)), 0, "!unused wtoken");
assertEq(usdc.balanceOf(address(uWrapper)), 0, "!unused usdc");
}

function testUsdcWethGammaUniV3LpTokenWrapper() public debuggingOnly fork(POLYGON_MAINNET) {
address USDC_WETH_RETRO_GAMMA_VAULT = 0xe058e1FfFF9B13d3FCd4803FDb55d1Cc2fe07DDC;
address usdcAddress = 0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174;
address usdcWhale = 0xe7804c37c13166fF0b37F5aE0BB07A3aEbb6e245;
IERC20Upgradeable usdc = IERC20Upgradeable(usdcAddress);

vm.prank(usdcWhale);
usdc.transfer(address(uWrapper), 9601.830212e6);

(IERC20Upgradeable outputToken, uint256 outputAmount) = uWrapper.redeem(
usdc,
9601.830212e6,
abi.encode(uniV3SwapRouter, uniProxyUni, USDC_WETH_RETRO_GAMMA_VAULT)
);

emit log_named_uint("lp tokens minted", outputAmount);

assertGt(outputToken.balanceOf(address(uWrapper)), 0, "!wrapped");
assertEq(IERC20Upgradeable(wtoken).balanceOf(address(uWrapper)), 0, "!unused wtoken");
assertEq(usdc.balanceOf(address(uWrapper)), 0, "!unused usdc");
}
}
2 changes: 1 addition & 1 deletion contracts/test/oracles/default/GammaPriceOracleTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ contract GammaPoolPriceOracleTest is BaseTest {
uint256 price_WETH_USDT = aOracle.price(WETH_USDT_QS_GAMMA_VAULT) / (1e18 / withdrawAmount);

uint256 expectedPrice = priceAtWithdraw(WETH_USDT_QS_WHALE, WETH_USDT_QS_GAMMA_VAULT, withdrawAmount);
assertApproxEqRel(price_WETH_USDT, expectedPrice, 5e16, "!aWETH-USDT price");
assertApproxEqRel(price_WETH_USDT, expectedPrice, 10e16, "!aWETH-USDT price");
}
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/test/oracles/default/PythPriceOracleTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ contract PythPriceOracleTest is BaseTest {
assertApproxEqRel(price, priceMpo, 1e16);
}

function testLineaTokenPrice() public fork(LINEA_MAINNET) {
function testLineaTokenPrice() public debuggingOnly fork(LINEA_MAINNET) {
PythStructs.Price memory pythPrice = IPyth(lineaPyth).getPriceUnsafe(btcUsdTokenPriceFeed);
emit log_named_uint("price", uint256(uint64(pythPrice.price)));
emit log_named_uint("updated", pythPrice.publishTime);
Expand Down