-
Notifications
You must be signed in to change notification settings - Fork 3
/
Pay.sol
121 lines (108 loc) · 4.01 KB
/
Pay.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// SPDX-License-Identifier: MIT
pragma solidity =0.8.4;
import {IConvenience} from '../interfaces/IConvenience.sol';
import {IFactory} from '@timeswap-labs/timeswap-v1-core/contracts/interfaces/IFactory.sol';
import {IWETH} from '../interfaces/IWETH.sol';
import {IERC20} from '@openzeppelin/contracts/token/ERC20/IERC20.sol';
import {IPair} from '@timeswap-labs/timeswap-v1-core/contracts/interfaces/IPair.sol';
import {IPay} from '../interfaces/IPay.sol';
import {IDue} from '../interfaces/IDue.sol';
import {PayMath} from './PayMath.sol';
import {MsgValue} from './MsgValue.sol';
import {ETH} from './ETH.sol';
library Pay {
using PayMath for IPair;
function pay(
mapping(IERC20 => mapping(IERC20 => mapping(uint256 => IConvenience.Native))) storage natives,
IFactory factory,
IPay.Repay memory params
) external returns (uint128 assetIn, uint128 collateralOut) {
(assetIn, collateralOut) = _pay(
natives,
factory,
IPay._Repay(
params.asset,
params.collateral,
params.maturity,
msg.sender,
params.collateralTo,
params.ids,
params.maxAssetsIn,
params.deadline
)
);
}
function payETHAsset(
mapping(IERC20 => mapping(IERC20 => mapping(uint256 => IConvenience.Native))) storage natives,
IFactory factory,
IWETH weth,
IPay.RepayETHAsset memory params
) external returns (uint128 assetIn, uint128 collateralOut) {
uint128 maxAssetIn = MsgValue.getUint112();
(assetIn, collateralOut) = _pay(
natives,
factory,
IPay._Repay(
weth,
params.collateral,
params.maturity,
address(this),
params.collateralTo,
params.ids,
params.maxAssetsIn,
params.deadline
)
);
if (maxAssetIn > assetIn) ETH.transfer(payable(msg.sender), maxAssetIn - assetIn);
}
function payETHCollateral(
mapping(IERC20 => mapping(IERC20 => mapping(uint256 => IConvenience.Native))) storage natives,
IFactory factory,
IWETH weth,
IPay.RepayETHCollateral memory params
) external returns (uint128 assetIn, uint128 collateralOut) {
(assetIn, collateralOut) = _pay(
natives,
factory,
IPay._Repay(
params.asset,
weth,
params.maturity,
msg.sender,
address(this),
params.ids,
params.maxAssetsIn,
params.deadline
)
);
if (collateralOut > 0) {
weth.withdraw(collateralOut);
ETH.transfer(params.collateralTo, collateralOut);
}
}
function _pay(
mapping(IERC20 => mapping(IERC20 => mapping(uint256 => IConvenience.Native))) storage natives,
IFactory factory,
IPay._Repay memory params
) private returns (uint128 assetIn, uint128 collateralOut) {
require(params.deadline >= block.timestamp, 'E504');
require(params.maturity > block.timestamp, 'E508');
IPair pair = factory.getPair(params.asset, params.collateral);
require(address(pair) != address(0), 'E501');
IDue collateralizedDebt = natives[params.asset][params.collateral][params.maturity].collateralizedDebt;
require(address(collateralizedDebt) != address(0), 'E502');
(uint112[] memory assetsIn, uint112[] memory collateralsOut) = pair.givenMaxAssetsIn(
params.maturity,
collateralizedDebt,
params.ids,
params.maxAssetsIn
);
(assetIn, collateralOut) = collateralizedDebt.burn(
params.collateralTo,
params.ids,
assetsIn,
collateralsOut,
bytes(abi.encode(params.asset, params.collateral, params.from))
);
}
}