Created
June 18, 2025 18:10
-
-
Save Turupawn/f9b29c200fc2ac3c2b1cc2ef883e0eb0 to your computer and use it in GitHub Desktop.
morpho testground
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
// IERC20 interface needed to interact with USDC and WETH | |
interface IERC20 { | |
function approve(address spender, uint256 amount) external returns (bool); | |
function balanceOf(address account) external view returns (uint256); | |
function transfer(address recipient, uint256 amount) external returns (bool); | |
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool); | |
} | |
// Morpho interface to get and repay loans | |
interface IMorpho { | |
struct MarketParams { | |
address loanToken; | |
address collateralToken; | |
address oracle; | |
address irm; | |
uint256 lltv; | |
} | |
function supplyCollateral( | |
MarketParams calldata marketParams, | |
uint256 amount, | |
address onBehalf, | |
bytes calldata data | |
) external; | |
function borrow( | |
MarketParams calldata marketParams, | |
uint256 amount, | |
uint256 maxShares, | |
address onBehalf, | |
address receiver | |
) external returns (uint256 assetsBorrowed, uint256 sharesBorrowed); | |
function repay( | |
MarketParams calldata marketParams, | |
uint256 amount, | |
uint256 maxShares, | |
address onBehalf, | |
bytes calldata data | |
) external returns (uint256 assetsRepaid, uint256 sharesRepaid); | |
function withdrawCollateral( | |
MarketParams calldata marketParams, | |
uint256 amount, | |
address onBehalf, | |
address receiver | |
) external; | |
} | |
// Demo contrat that abstracts borrowing and repayment into two functions | |
contract MorphoBorrower { | |
address public constant morphoAddress = 0xBBBBBbbBBb9cC5e90e3b3Af64bdAF62C37EEFFCb; // Morpho address on Base | |
address public constant loanToken = 0x4200000000000000000000000000000000000006; // WETH on base | |
address public constant collateralToken = 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913; // USDC on base | |
address public constant oracle = 0xD09048c8B568Dbf5f189302beA26c9edABFC4858; // Chainlink oracle for our market | |
address public constant irm = 0x46415998764C29aB2a25CbeA6254146D50D22687; // Adaptative curve strategy for our market | |
uint256 public constant lltv = 860000000000000000; // 86% loan to liquidation treshold | |
// Our target morpho market settings | |
IMorpho.MarketParams public marketParams = IMorpho.MarketParams({ | |
loanToken: loanToken, | |
collateralToken: collateralToken, | |
oracle: oracle, | |
irm: irm, | |
lltv: lltv | |
}); | |
uint256 public sharesBorrowed; // Asset borrow tracking | |
// Borrow WETH against USDC | |
function borrowAgainstCollateral(uint256 collateralAmount, uint256 borrowAmount) external { | |
// Transfer USDC from user and supply it as collateral in the Morpho market | |
IERC20(collateralToken).transferFrom(msg.sender, address(this), collateralAmount); | |
IERC20(collateralToken).approve(morphoAddress, collateralAmount); | |
IMorpho(morphoAddress).supplyCollateral( | |
marketParams, | |
collateralAmount, | |
address(this), | |
"" | |
); | |
// | |
(uint256 assetsBorrowed, uint256 shares) = IMorpho(morphoAddress).borrow( | |
marketParams, | |
borrowAmount, | |
0, | |
address(this), | |
address(this) | |
); | |
sharesBorrowed = shares; | |
IERC20(loanToken).transfer(msg.sender, assetsBorrowed); | |
} | |
function repayAndWithdraw(uint256 repayAmount, uint256 collateralAmount) external { | |
// Pull loanToken from sender | |
IERC20(loanToken).transferFrom(msg.sender, address(this), repayAmount); | |
// Approve Morpho to use tokens | |
IERC20(loanToken).approve(morphoAddress, repayAmount); | |
// Record balance before | |
uint256 before = IERC20(loanToken).balanceOf(address(this)); | |
// Full repayment using shares | |
(uint256 assetsRepaid, ) = IMorpho(morphoAddress).repay( | |
marketParams, | |
0, | |
sharesBorrowed, | |
address(this), | |
"" | |
); | |
// Refund leftover loan tokens to sender | |
uint256 afterRepay = IERC20(loanToken).balanceOf(address(this)); | |
if (afterRepay > before - assetsRepaid) { | |
uint256 refund = afterRepay - (before - assetsRepaid); | |
IERC20(loanToken).transfer(msg.sender, refund); | |
} | |
// Withdraw collateral and send to sender | |
IMorpho(morphoAddress).withdrawCollateral( | |
marketParams, | |
collateralAmount, | |
address(this), | |
msg.sender | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment