This repository has been archived by the owner on Nov 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGeneralRepay.sol
74 lines (64 loc) · 2.34 KB
/
GeneralRepay.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
Copyright 2022 JOJO Exchange
SPDX-License-Identifier: BUSL-1.1*/
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "../../../src/Interface/IJUSDBank.sol";
import "../../../src/Interface/IJUSDExchange.sol";
pragma solidity 0.8.9;
contract GeneralRepay {
address public immutable USDC;
address public jusdBank;
address public jusdExchange;
address public immutable JUSD;
using SafeERC20 for IERC20;
constructor(
address _jusdBank,
address _jusdExchange,
address _USDC,
address _JUSD
) {
jusdBank = _jusdBank;
jusdExchange = _jusdExchange;
USDC = _USDC;
JUSD = _JUSD;
}
function repayJUSD(
address asset,
uint256 amount,
address to,
bytes memory param
) external {
IERC20(asset).safeTransferFrom(msg.sender, address(this), amount);
uint256 minReceive;
if (asset != USDC) {
(address approveTarget, address swapTarget, uint256 minAmount, bytes memory data) = abi
.decode(param, (address, address, uint256, bytes));
IERC20(asset).approve(approveTarget, amount);
(bool success, ) = swapTarget.call(data);
if (success == false) {
assembly {
let ptr := mload(0x40)
let size := returndatasize()
returndatacopy(ptr, 0, size)
revert(ptr, size)
}
}
minReceive = minAmount;
}
uint256 USDCAmount = IERC20(USDC).balanceOf(address(this));
require(USDCAmount >= minReceive, "receive amount is too small");
uint256 JUSDAmount = USDCAmount;
uint256 borrowBalance = IJUSDBank(jusdBank).getBorrowBalance(to);
if (USDCAmount <= borrowBalance) {
IERC20(USDC).approve(jusdExchange, USDCAmount);
IJUSDExchange(jusdExchange).buyJUSD(USDCAmount, address(this));
} else {
IERC20(USDC).approve(jusdExchange, borrowBalance);
IJUSDExchange(jusdExchange).buyJUSD(borrowBalance, address(this));
IERC20(USDC).safeTransfer(to, USDCAmount - borrowBalance);
JUSDAmount = borrowBalance;
}
IERC20(JUSD).approve(jusdBank, JUSDAmount);
IJUSDBank(jusdBank).repay(JUSDAmount, to);
}
}