Skip to content

Commit

Permalink
feat(refactoBis): testing ok
Browse files Browse the repository at this point in the history
  • Loading branch information
tomrpl committed Nov 9, 2023
1 parent 760a882 commit 0cf4788
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 34 deletions.
29 changes: 26 additions & 3 deletions foundry.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,29 @@
[profile.default]
src = "src"
out = "out"
libs = ["lib"]
test = "test/forge"
evm_version = "paris"
fs_permissions = [
{ access = "read", path = "./config/"},
{ access = "read", path = "./lib/morpho-blue/out/"}
]
libs = ["node_modules", "lib"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
[profile.default.rpc_endpoints]
mainnet = "https://eth-mainnet.g.alchemy.com/v2/${ALCHEMY_KEY}"
tenderly = "https://rpc.tenderly.co/fork/${TENDERLY_FORK_ID}"

[profile.default.fmt]
wrap_comments = true


[profile.build]
via-ir = true
test = "/dev/null"
script = "/dev/null"


[profile.test]
via-ir = false


# See more config options https://github.com/foundry-rs/foundry/tree/master/config
1 change: 0 additions & 1 deletion lib/metamorpho
Submodule metamorpho deleted from 04ef54
1 change: 0 additions & 1 deletion lib/openzeppelin-contracts
Submodule openzeppelin-contracts deleted from 281550
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@
"build:forge": "FOUNDRY_PROFILE=build forge build",
"build:hardhat": "hardhat compile",
"build:blue": "cd lib/morpho-blue/ && yarn build:forge && cd ../..",
"build:metamorpho": "cd lib/metamorpho/ && yarn build:forge && cd ../..",
"build:irm": "cd lib/morpho-blue-irm/ && forge build && cd ../..",
"typecheck": "tsc --noEmit",
"test:forge": "yarn build:blue && FOUNDRY_PROFILE=test forge test",
"test:forge": "yarn build:blue && yarn build:metamorpho && FOUNDRY_PROFILE=test forge test",
"test:forge:local": "FOUNDRY_MATCH_CONTRACT=LocalTest yarn test:forge",
"test:forge:ethereum": "FOUNDRY_MATCH_CONTRACT=EthereumTest yarn test:forge",
"test:hardhat": "hardhat test",
Expand Down
10 changes: 6 additions & 4 deletions src/blue/BlueSnippets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ import {Id, IMorpho, MarketParams, Market} from "@morpho-blue/interfaces/IMorpho
import {IERC20} from "@morpho-blue/interfaces/IERC20.sol";
import {IIrm} from "@morpho-blue/interfaces/IIrm.sol";
import {IOracle} from "@morpho-blue/interfaces/IOracle.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {ERC20} from "@openzeppelin/token/ERC20/ERC20.sol";

import {MorphoBalancesLib} from "@morpho-blue/libraries/periphery/MorphoBalancesLib.sol";
import {MarketParamsLib} from "@morpho-blue/libraries/MarketParamsLib.sol";
import {MorphoLib} from "@morpho-blue/libraries/periphery/MorphoLib.sol";
import {MorphoStorageLib} from "@morpho-blue/libraries/periphery/MorphoStorageLib.sol";
import {MathLib} from "@morpho-blue/libraries/MathLib.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import {SafeERC20} from "@openzeppelin/token/ERC20/utils/SafeERC20.sol";
import {SharesMathLib} from "@morpho-blue/libraries/SharesMathLib.sol";

import {ORACLE_PRICE_SCALE} from "@morpho-blue/libraries/ConstantsLib.sol";
Expand Down Expand Up @@ -43,7 +43,8 @@ contract BlueSnippets {

/* VIEW FUNCTIONS */

// INFORMATIONAL: No 'Total Supply' and no 'Total Borrow' functions to calculate on chain as there could be some weird oracles / markets created
// INFORMATIONAL: No 'Total Supply' and no 'Total Borrow' functions to calculate on chain as there could be some
// weird oracles / markets created

/**
* @notice Calculates the supply APR (Annual Percentage Rate) for a given market.
Expand Down Expand Up @@ -196,7 +197,8 @@ contract BlueSnippets {
}

/**
* @notice Handles the withdrawal of collateral by a user from a specific market of a specific amount. The withdrawn funds are going to the receiver.
* @notice Handles the withdrawal of collateral by a user from a specific market of a specific amount. The withdrawn
* funds are going to the receiver.
* @param marketParams The parameters of the market.
* @param amount The amount of collateral the user is withdrawing.
* @param user The address of the user withdrawing the collateral.
Expand Down
10 changes: 5 additions & 5 deletions src/metamorpho/MetamorphoSnippets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {MorphoBalancesLib} from "@morpho-blue/libraries/periphery/MorphoBalances
import {MathLib, WAD} from "@morpho-blue/libraries/MathLib.sol";

import {Math} from "@openzeppelin/utils/math/Math.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {ERC20} from "@openzeppelin/token/ERC20/ERC20.sol";

contract MetamorphoSnippets {
uint256 constant FEE = 0.2 ether; // 20%
Expand Down Expand Up @@ -46,7 +46,7 @@ contract MetamorphoSnippets {
}

function supplyQueueVault() public view returns (Id[] memory supplyQueueList) {
uint256 queueLength = vault.supplyQueueSize();
uint256 queueLength = vault.supplyQueueLength();
supplyQueueList = new Id[](queueLength);
for (uint256 i; i < queueLength; ++i) {
supplyQueueList[i] = vault.supplyQueue(i);
Expand All @@ -55,7 +55,7 @@ contract MetamorphoSnippets {
}

function withdrawQueueVault() public view returns (Id[] memory withdrawQueueList) {
uint256 queueLength = vault.withdrawQueueSize();
uint256 queueLength = vault.supplyQueueLength();
withdrawQueueList = new Id[](queueLength);
for (uint256 i; i < queueLength; ++i) {
withdrawQueueList[i] = vault.withdrawQueue(i);
Expand All @@ -65,7 +65,7 @@ contract MetamorphoSnippets {

function capMarket(MarketParams memory marketParams) public view returns (uint192 cap) {
Id id = marketParams.id();
(cap,) = vault.config(id);
cap = vault.config(id).cap;
}

// TO TEST
Expand All @@ -92,7 +92,7 @@ contract MetamorphoSnippets {

function supplyAPRVault() public view returns (uint256 avgSupplyRate) {
uint256 ratio;
uint256 queueLength = vault.withdrawQueueSize();
uint256 queueLength = vault.withdrawQueueLength();

// TODO: Verify that the idle liquidity is taken into account
uint256 totalAmount = totalDepositVault();
Expand Down
28 changes: 9 additions & 19 deletions test/forge/metamorpho/TestMetamorphoSnippets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ contract TestIntegrationSnippets is IntegrationTest {

assertGt(shares, 0, "shares");
assertEq(vault.balanceOf(ONBEHALF), shares, "balanceOf(ONBEHALF)");
assertEq(morpho.expectedSupplyBalance(allMarkets[0], address(vault)), assets, "expectedSupplyBalance(vault)");
assertEq(morpho.expectedSupplyAssets(allMarkets[0], address(vault)), assets, "expectedSupplyAssets(vault)");
}

function testVaultAmountInMarket(uint256 assets) public {
Expand All @@ -70,10 +70,10 @@ contract TestIntegrationSnippets is IntegrationTest {

assertGt(shares, 0, "shares");
assertEq(vault.balanceOf(ONBEHALF), shares, "balanceOf(ONBEHALF)");
assertEq(morpho.expectedSupplyBalance(allMarkets[0], address(vault)), assets, "expectedSupplyBalance(vault)");
assertEq(morpho.expectedSupplyAssets(allMarkets[0], address(vault)), assets, "expectedSupplyAssets(vault)");

uint256 vaultAmount = snippets.vaultAmountInMarket(allMarkets[0]);
assertEq(assets, vaultAmount, "expectedSupplyBalance(vault)");
assertEq(assets, vaultAmount, "expectedSupplyAssets(vault)");
}

function testTotalSharesUserVault(uint256 deposited) public {
Expand Down Expand Up @@ -128,7 +128,7 @@ contract TestIntegrationSnippets is IntegrationTest {
expectedWithdrawQueue[2] = allMarkets[0].id();

vm.prank(ALLOCATOR);
vault.sortWithdrawQueue(indexes);
vault.updateWithdrawQueue(indexes);

Id[] memory withdrawQueueList = snippets.withdrawQueueVault();
assertEq(Id.unwrap(vault.withdrawQueue(0)), Id.unwrap(expectedWithdrawQueue[0]));
Expand All @@ -142,21 +142,11 @@ contract TestIntegrationSnippets is IntegrationTest {
// OK
function testCapMarket(MarketParams memory marketParams) public {
Id idMarket = marketParams.id();
(uint192 cap,) = vault.config(idMarket);
uint192 cap = vault.config(idMarket).cap;
uint192 snippetCap = snippets.capMarket(marketParams);
assertEq(cap, snippetCap, "cap per market");
}

// OK
function testSubmitCapOverflow(uint256 seed, uint256 cap) public {
MarketParams memory marketParams = _randomMarketParams(seed);
cap = bound(cap, uint256(type(uint192).max) + 1, type(uint256).max);

vm.prank(CURATOR);
vm.expectRevert(abi.encodeWithSelector(SafeCast.SafeCastOverflowedUintDowncast.selector, uint8(192), cap));
vault.submitCap(marketParams, cap);
}

// TODO Implement the TEST SUPPLY APR EQUAL 0 Function
// function testSupplyAPREqual0(MarketParams memory marketParams, Market memory market) public {
// vm.assume(market.totalBorrowAssets == 0);
Expand All @@ -166,7 +156,7 @@ contract TestIntegrationSnippets is IntegrationTest {
// vm.assume(market.fee < 1 ether);
// vm.assume(market.totalSupplyAssets >= market.totalBorrowAssets);

// (uint256 totalSupplyAssets,, uint256 totalBorrowAssets,) = morpho.expectedMarketBalances(marketParams);
// (uint256 totalSupplyAssets,, uint256 totalBorrowAssets,) = morpho.expectedMarketAssetss(marketParams);
// uint256 borrowTrue = irm.borrowRate(marketParams, market);
// uint256 utilization = totalBorrowAssets == 0 ? 0 : totalBorrowAssets.wDivUp(totalSupplyAssets);

Expand All @@ -182,7 +172,7 @@ contract TestIntegrationSnippets is IntegrationTest {
// vm.assume(market.fee < 1 ether);
// vm.assume(market.totalSupplyAssets >= market.totalBorrowAssets);

// (uint256 totalSupplyAssets,, uint256 totalBorrowAssets,) = morpho.expectedMarketBalances(marketParams);
// (uint256 totalSupplyAssets,, uint256 totalBorrowAssets,) = morpho.expectedMarketAssetss(marketParams);
// uint256 borrowTrue = irm.borrowRate(marketParams, market);
// assertGt(borrowTrue, 0, "intermediary test");
// uint256 utilization = totalBorrowAssets == 0 ? 0 : totalBorrowAssets.wDivUp(totalSupplyAssets);
Expand Down Expand Up @@ -257,7 +247,7 @@ contract TestIntegrationSnippets is IntegrationTest {
assertEq(redeemed, minted, "shares");
assertEq(vault.balanceOf(address(snippets)), 0, "balanceOf(address(snippets))");
assertEq(loanToken.balanceOf(address(snippets)), assets, "loanToken.balanceOf(address(snippets))");
assertEq(morpho.expectedSupplyBalance(allMarkets[0], address(vault)), 0, "expectedSupplyBalance(vault)");
assertEq(morpho.expectedSupplyAssets(allMarkets[0], address(vault)), 0, "expectedSupplyAssets(vault)");
}

function testRedeemAll(uint256 deposited) public {
Expand All @@ -280,7 +270,7 @@ contract TestIntegrationSnippets is IntegrationTest {
assertEq(redeemed, deposited, "assets");
assertEq(vault.balanceOf(address(snippets)), 0, "balanceOf(address(snippets))");
assertEq(loanToken.balanceOf(address(snippets)), deposited, "loanToken.balanceOf(address(snippets))");
assertEq(morpho.expectedSupplyBalance(allMarkets[0], address(vault)), 0, "expectedSupplyBalance(vault)");
assertEq(morpho.expectedSupplyAssets(allMarkets[0], address(vault)), 0, "expectedSupplyAssets(vault)");
}

function _setCaps() internal {
Expand Down

0 comments on commit 0cf4788

Please sign in to comment.