Skip to content

Commit

Permalink
feat(snippets): explicitly put the receiver as the user + few comment…
Browse files Browse the repository at this point in the history
…s fixed
  • Loading branch information
tomrpl committed Oct 31, 2023
1 parent 3658970 commit d0879c5
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 22 deletions.
24 changes: 14 additions & 10 deletions src/Snippets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -230,22 +230,22 @@ contract Snippets {
}

/**
* @notice Handles the withdrawal of collateral by a user from a specific market of a specific amount. The withdrawed 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.
*/
function withdrawCollateral(
MarketParams memory marketParams,
uint256 amount,
address user,
address receiver
address user
) external {
ERC20(marketParams.collateralToken).approve(
address(morpho),
type(uint256).max
);
address onBehalf = user;
address receiver = user;
morpho.withdrawCollateral(marketParams, amount, onBehalf, receiver);
}

Expand All @@ -260,15 +260,15 @@ contract Snippets {
function withdrawAmount(
MarketParams memory marketParams,
uint256 amount,
address user,
address receiver
address user
) external returns (uint256 assetsWithdrawn, uint256 sharesWithdrawn) {
ERC20(marketParams.loanToken).approve(
address(morpho),
type(uint256).max
);
uint256 shares = 0;
address onBehalf = user;
address receiver = user;
(assetsWithdrawn, sharesWithdrawn) = morpho.withdraw(
marketParams,
amount,
Expand All @@ -287,8 +287,7 @@ contract Snippets {
*/
function withdraw50Percent(
MarketParams memory marketParams,
address user,
address receiver
address user
) external returns (uint256 assetsWithdrawn, uint256 sharesWithdrawn) {
ERC20(marketParams.loanToken).approve(
address(morpho),
Expand All @@ -300,6 +299,8 @@ contract Snippets {
uint256 shares = supplyShares / 2;

address onBehalf = user;
address receiver = user;

(assetsWithdrawn, sharesWithdrawn) = morpho.withdraw(
marketParams,
amount,
Expand All @@ -318,8 +319,7 @@ contract Snippets {
*/
function withdrawAll(
MarketParams memory marketParams,
address user,
address receiver
address user
) external returns (uint256 assetsWithdrawn, uint256 sharesWithdrawn) {
ERC20(marketParams.loanToken).approve(
address(morpho),
Expand All @@ -330,6 +330,8 @@ contract Snippets {
uint256 amount = 0;

address onBehalf = user;
address receiver = user;

(assetsWithdrawn, sharesWithdrawn) = morpho.withdraw(
marketParams,
amount,
Expand Down Expand Up @@ -358,12 +360,14 @@ contract Snippets {
);
uint256 shares = 0;
address onBehalf = user;
address receiver = user;

(assetsBorrowed, sharesBorrowed) = morpho.borrow(
marketParams,
amount,
shares,
onBehalf,
onBehalf
receiver
);
}

Expand Down
41 changes: 29 additions & 12 deletions test/forge/TestIntegrationSnippets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ contract TestIntegrationSnippets is BaseTest {
marketParams,
address(this)
);

if (borrowed == 0) {
actualHF = type(uint256).max;
} else {
Expand All @@ -250,6 +251,30 @@ contract TestIntegrationSnippets is BaseTest {
assertEq(expectedHF, actualHF);
}

function testHealthfactor0Borrow(
uint256 amountSupplied,
uint256 amountBorrowed,
uint256 timeElapsed,
uint256 fee
) public {
vm.assume(amountBorrowed == 0);
_generatePendingInterest(
amountSupplied,
amountBorrowed,
timeElapsed,
fee
);
_accrueInterest(marketParams);

uint256 expectedHF = snippets.userHealthFactor(
marketParams,
id,
address(this)
);

assertEq(expectedHF, type(uint256).max);
}

// ---- Test Managing Functions ----

function testSupplyAssets(uint256 amount) public {
Expand Down Expand Up @@ -287,8 +312,7 @@ contract TestIntegrationSnippets is BaseTest {
(uint256 assetsWithdrawn, ) = snippets.withdrawAmount(
marketParams,
amount,
address(snippets),
RECEIVER
address(snippets)
);
assertEq(assetsWithdrawn, amount, "returned asset amount");
}
Expand All @@ -300,8 +324,7 @@ contract TestIntegrationSnippets is BaseTest {
snippets.supply(marketParams, amount, address(snippets));
(uint256 assetsWithdrawn, ) = snippets.withdraw50Percent(
marketParams,
address(snippets),
RECEIVER
address(snippets)
);
assertEq(assetsWithdrawn, amount / 2, "returned asset amount");
}
Expand All @@ -313,8 +336,7 @@ contract TestIntegrationSnippets is BaseTest {
snippets.supply(marketParams, amount, address(snippets));
(uint256 assetsWithdrawn, ) = snippets.withdrawAll(
marketParams,
address(snippets),
RECEIVER
address(snippets)
);
assertEq(assetsWithdrawn, amount, "returned asset amount");
assertEq(
Expand All @@ -335,12 +357,7 @@ contract TestIntegrationSnippets is BaseTest {
amount,
"collateral"
);
snippets.withdrawCollateral(
marketParams,
amount,
address(snippets),
RECEIVER
);
snippets.withdrawCollateral(marketParams, amount, address(snippets));
assertEq(morpho.collateral(id, address(snippets)), 0, "collateral");
}

Expand Down

0 comments on commit d0879c5

Please sign in to comment.