-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add moonwell oracle and liquidation contract
- Loading branch information
JoscelynFarr
committed
Apr 26, 2024
1 parent
0045afd
commit f2838bf
Showing
3 changed files
with
96 additions
and
106 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
/* | ||
Copyright 2022 JOJO Exchange | ||
SPDX-License-Identifier: BUSL-1.1 | ||
*/ | ||
|
||
pragma solidity ^0.8.19; | ||
|
||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; | ||
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | ||
import "./interfaces/IJUSDBank.sol"; | ||
import "./interfaces/IJUSDExchange.sol"; | ||
import "./libraries/SignedDecimalMath.sol"; | ||
|
||
interface MTokenInterface { | ||
function redeem(uint redeemTokens) external returns (uint); | ||
} | ||
|
||
contract FlashLoanLiquidateMtoken is Ownable { | ||
using SafeERC20 for IERC20; | ||
using SignedDecimalMath for uint256; | ||
|
||
address public immutable USDC; | ||
address public immutable JUSD; | ||
address public jusdBank; | ||
address public jusdExchange; | ||
address public insurance; | ||
|
||
struct LiquidateData { | ||
uint256 actualCollateral; | ||
uint256 insuranceFee; | ||
uint256 actualLiquidatedT0; | ||
uint256 actualLiquidated; | ||
uint256 liquidatedRemainUSDC; | ||
} | ||
|
||
constructor(address _jusdBank, address _jusdExchange, address _USDC, address _JUSD, address _insurance) { | ||
jusdBank = _jusdBank; | ||
jusdExchange = _jusdExchange; | ||
USDC = _USDC; | ||
JUSD = _JUSD; | ||
insurance = _insurance; | ||
} | ||
|
||
function JOJOFlashLoan(address asset, uint256 amount, address to, bytes calldata param) external { | ||
(LiquidateData memory liquidateData, bytes memory originParam) = abi.decode(param, (LiquidateData, bytes)); | ||
(address liquidator, uint256 minReceive) = | ||
abi.decode(originParam, (address, uint256)); | ||
|
||
MTokenInterface(asset).redeem(amount); | ||
uint256 USDCAmount = IERC20(USDC).balanceOf(address(this)); | ||
require(USDCAmount >= minReceive, "receive amount is too small"); | ||
IERC20(USDC).approve(jusdExchange, liquidateData.actualLiquidated); | ||
IJUSDExchange(jusdExchange).buyJUSD(liquidateData.actualLiquidated, address(this)); | ||
IERC20(JUSD).approve(jusdBank, liquidateData.actualLiquidated); | ||
IJUSDBank(jusdBank).repay(liquidateData.actualLiquidated, to); | ||
IERC20(USDC).safeTransfer(insurance, liquidateData.insuranceFee); | ||
if (liquidateData.liquidatedRemainUSDC != 0) { | ||
IERC20(USDC).safeTransfer(address(jusdBank), liquidateData.liquidatedRemainUSDC); | ||
} | ||
IERC20(USDC).safeTransfer( | ||
liquidator, | ||
USDCAmount - liquidateData.insuranceFee - liquidateData.actualLiquidated | ||
- liquidateData.liquidatedRemainUSDC | ||
); | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/* | ||
Copyright 2022 JOJO Exchange | ||
SPDX-License-Identifier: BUSL-1.1 | ||
*/ | ||
|
||
pragma solidity ^0.8.19; | ||
|
||
import "@openzeppelin/contracts/access/Ownable.sol"; | ||
import "@openzeppelin/contracts/utils/math/SafeCast.sol"; | ||
|
||
interface MTokenInterface { | ||
function exchangeRateStored() external view returns (uint); | ||
} | ||
|
||
contract MoonwellOracle { | ||
uint256 public price; | ||
address public immutable source; | ||
string public description; | ||
|
||
constructor(string memory _description, address _source) { | ||
description = _description; | ||
source = _source; | ||
} | ||
|
||
function getAssetPrice() external view returns (uint256) { | ||
uint256 rate = MTokenInterface(source).exchangeRateStored(); | ||
return rate; | ||
} | ||
} |