-
Notifications
You must be signed in to change notification settings - Fork 11
/
CurveLpTokenUniv3Callee.sol
180 lines (150 loc) · 6.25 KB
/
CurveLpTokenUniv3Callee.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2022 Dai Foundation
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
pragma solidity 0.6.12;
pragma experimental ABIEncoderV2;
interface GemJoinLike {
function dec() external view returns (uint256);
function gem() external view returns (address);
function exit(address, uint256) external;
}
interface DaiJoinLike {
function dai() external view returns (TokenLike);
function join(address, uint256) external;
}
interface TokenLike {
function approve(address, uint256) external;
function transfer(address, uint256) external;
function balanceOf(address) external view returns (uint256);
function symbol() external view returns (string memory);
}
interface ManagerLike {
function exit(address crop, address usr, uint256 val) external;
}
interface CurvePoolLike {
function remove_liquidity_one_coin(uint256 _token_amount, int128 i, uint256 _min_amount)
external returns (uint256);
function coins(uint256) external view returns (address);
}
interface WethLike is TokenLike {
function deposit() external payable;
}
interface UniV3RouterLike {
struct ExactInputParams {
bytes path;
address recipient;
uint256 deadline;
uint256 amountIn;
uint256 amountOutMinimum;
}
function exactInput(UniV3RouterLike.ExactInputParams calldata params)
external payable returns (uint256 amountOut);
}
contract CurveLpTokenUniv3Callee {
UniV3RouterLike public immutable uniV3Router;
DaiJoinLike public immutable daiJoin;
TokenLike public immutable dai;
address public immutable weth;
uint256 public constant RAY = 10 ** 27;
address public constant ETH = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
function _add(uint x, uint y) internal pure returns (uint z) {
require((z = x + y) >= x, "ds-math-add-overflow");
}
function _sub(uint x, uint y) internal pure returns (uint z) {
require((z = x - y) <= x, "ds-math-sub-underflow");
}
function _divup(uint256 x, uint256 y) internal pure returns (uint256 z) {
z = _add(x, _sub(y, 1)) / y;
}
struct CurveData {
address pool;
uint256 coinIndex;
}
constructor(
address uniV3Router_,
address daiJoin_,
address weth_
) public {
uniV3Router = UniV3RouterLike(uniV3Router_);
daiJoin = DaiJoinLike(daiJoin_);
TokenLike dai_ = DaiJoinLike(daiJoin_).dai();
dai = dai_;
weth = weth_;
dai_.approve(daiJoin_, type(uint256).max);
}
receive() external payable {}
function _fromWad(address gemJoin, uint256 wad) internal view returns (uint256 amt) {
amt = wad / 10 ** (_sub(18, GemJoinLike(gemJoin).dec()));
}
function clipperCall(
address sender, // Clipper caller, pays back the loan
uint256 owe, // Dai amount to pay back [rad]
uint256 slice, // Gem amount received [wad]
bytes calldata data // Extra data, see below
) external {
(
address to, // address to send remaining DAI to
address gemJoin, // gemJoin adapter address
uint256 minProfit, // minimum profit in DAI to make [wad]
bytes memory path, // uniswap v3 path
address manager, // pass address(0) if no manager
CurveData memory curveData // curve pool data
) = abi.decode(data, (address, address, uint256, bytes, address, CurveData));
address gem = GemJoinLike(gemJoin).gem();
// Convert slice to token precision
slice = _fromWad(gemJoin, slice);
// Exit gem to token
if(manager != address(0)) {
ManagerLike(manager).exit(gemJoin, address(this), slice);
} else {
GemJoinLike(gemJoin).exit(address(this), slice);
}
// curveData used explicitly to avoid stack too deep
TokenLike(gem).approve(curveData.pool, slice);
slice = CurvePoolLike(curveData.pool).remove_liquidity_one_coin({
_token_amount: slice,
i: int128(curveData.coinIndex),
_min_amount: 0 // minProfit is checked below
});
gem = CurvePoolLike(curveData.pool).coins(curveData.coinIndex);
if (gem == ETH) {
gem = weth;
WethLike(gem).deposit{
value: slice
}();
}
// Approve uniV3 to take gem
TokenLike(gem).approve(address(uniV3Router), slice);
// Calculate amount of DAI to Join (as erc20 WAD value)
uint256 daiToJoin = _divup(owe, RAY);
UniV3RouterLike.ExactInputParams memory params = UniV3RouterLike.ExactInputParams({
path: path,
recipient: address(this),
deadline: block.timestamp,
amountIn: slice,
amountOutMinimum: _add(daiToJoin, minProfit)
});
uniV3Router.exactInput(params);
// Although Uniswap will accept all gems, this check is a sanity check, just in case
// Transfer any lingering gem to specified address
if (TokenLike(gem).balanceOf(address(this)) > 0) {
TokenLike(gem).transfer(to, TokenLike(gem).balanceOf(address(this)));
}
// Convert DAI bought to internal vat value of the msg.sender of Clipper.take
daiJoin.join(sender, daiToJoin);
// Transfer remaining DAI to specified address
dai.transfer(to, dai.balanceOf(address(this)));
}
}