forked from AlphaFinanceLab/alpha-homora-v2-integration-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSushiswapSpellV1IntegrationEth.sol
235 lines (200 loc) · 7.98 KB
/
SushiswapSpellV1IntegrationEth.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.16;
import 'OpenZeppelin/[email protected]/contracts/token/ERC20/IERC20.sol';
import 'OpenZeppelin/[email protected]/contracts/token/ERC20/utils/SafeERC20.sol';
import 'OpenZeppelin/[email protected]/contracts/token/ERC20/extensions/IERC20Metadata.sol';
import '../BaseIntegration.sol';
import '../utils/HomoraMath.sol';
import '../../interfaces/sushiswap/IMasterChef.sol';
import '../../interfaces/sushiswap/ISushiswapFactory.sol';
import '../../interfaces/homorav2/banks/IBankETH.sol';
import '../../interfaces/homorav2/wrappers/IWMasterChef.sol';
import '../../interfaces/homorav2/spells/ISushiswapSpellV1.sol';
import 'forge-std/console2.sol';
contract SushiswapSpellV1IntegrationEth is BaseIntegration {
using SafeERC20 for IERC20;
using HomoraMath for uint;
IBankETH bank; // homora bank
ISushiswapFactory factory; // sushiswap factory
uint constant PRECISION = 10**12;
struct AddLiquidityParams {
address tokenA; // The first token of pool
address tokenB; // The second token of pool
uint amtAUser; // Supplied tokenA amount
uint amtBUser; // Supplied tokenB amount
uint amtLPUser; // Supplied LP token amount
uint amtABorrow; // Borrow tokenA amount
uint amtBBorrow; // Borrow tokenB amount
uint amtLPBorrow; // Borrow LP token amount (should be 0, not support borrowing LP tokens)
uint amtAMin; // Desired tokenA amount (slippage control)
uint amtBMin; // Desired tokenB amount (slippage control)
uint poolId; // pool id of MasterChef
}
struct RemoveLiquidityParams {
address tokenA; // The first token of pool
address tokenB; // The second token of pool
uint amtLPTake; // Amount of LP being removed from the position
uint amtLPWithdraw; // Amount of LP that user receives (remainings are converted to underlying tokens).
uint amtARepay; // Amount of tokenA that user repays (repay all -> type(uint).max)
uint amtBRepay; // Amount of tokenB that user repays (repay all -> type(uint).max)
uint amtLPRepay; // Amount of LP that user repays (should be 0, not support borrowing LP tokens).
uint amtAMin; // Desired tokenA amount (slippage control)
uint amtBMin; // Desired tokenB amount (slippage control)
}
constructor(IBankETH _bank, ISushiswapFactory _factory) {
bank = _bank;
factory = _factory;
}
function openPosition(ISushiswapSpellV1 _spell, AddLiquidityParams memory _params)
external
returns (uint positionId)
{
address lp = factory.getPair(_params.tokenA, _params.tokenB);
// approve tokens
ensureApprove(_params.tokenA, address(bank));
ensureApprove(_params.tokenB, address(bank));
ensureApprove(lp, address(bank));
// transfer tokens from user
IERC20(_params.tokenA).safeTransferFrom(msg.sender, address(this), _params.amtAUser);
IERC20(_params.tokenB).safeTransferFrom(msg.sender, address(this), _params.amtBUser);
IERC20(lp).safeTransferFrom(msg.sender, address(this), _params.amtLPUser);
bytes memory executeData = abi.encodeWithSelector(
_spell.addLiquidityWMasterChef.selector,
_params.tokenA,
_params.tokenB,
ISushiswapSpellV1.Amounts(
_params.amtAUser,
_params.amtBUser,
_params.amtLPUser,
_params.amtABorrow,
_params.amtBBorrow,
_params.amtLPBorrow,
_params.amtAMin,
_params.amtBMin
),
_params.poolId
);
// (0 is reserved for opening new position)
positionId = bank.execute(0, address(_spell), executeData);
doRefundETH();
doRefund(_params.tokenA);
doRefund(_params.tokenB);
doRefund(lp);
}
function increasePosition(
uint _positionId,
ISushiswapSpellV1 _spell,
AddLiquidityParams memory _params
) external {
address lp = factory.getPair(_params.tokenA, _params.tokenB);
address rewardToken = getRewardToken(_positionId);
// approve tokens
ensureApprove(_params.tokenA, address(bank));
ensureApprove(_params.tokenB, address(bank));
ensureApprove(lp, address(bank));
// transfer tokens from user
IERC20(_params.tokenA).safeTransferFrom(msg.sender, address(this), _params.amtAUser);
IERC20(_params.tokenB).safeTransferFrom(msg.sender, address(this), _params.amtBUser);
IERC20(lp).safeTransferFrom(msg.sender, address(this), _params.amtLPUser);
bytes memory executeData = abi.encodeWithSelector(
_spell.addLiquidityWMasterChef.selector,
_params.tokenA,
_params.tokenB,
ISushiswapSpellV1.Amounts(
_params.amtAUser,
_params.amtBUser,
_params.amtLPUser,
_params.amtABorrow,
_params.amtBBorrow,
_params.amtLPBorrow,
_params.amtAMin,
_params.amtBMin
),
_params.poolId
);
bank.execute(_positionId, address(_spell), executeData);
doRefundETH();
doRefund(_params.tokenA);
doRefund(_params.tokenB);
doRefund(lp);
doRefund(rewardToken);
}
function reducePosition(
uint _positionId,
ISushiswapSpellV1 _spell,
RemoveLiquidityParams memory _params
) external {
address lp = factory.getPair(_params.tokenA, _params.tokenB);
address rewardToken = getRewardToken(_positionId);
bytes memory executeData = abi.encodeWithSelector(
_spell.removeLiquidityWMasterChef.selector,
_params.tokenA,
_params.tokenB,
ISushiswapSpellV1.RepayAmounts(
_params.amtLPTake,
_params.amtLPWithdraw,
_params.amtARepay,
_params.amtBRepay,
_params.amtLPRepay,
_params.amtAMin,
_params.amtBMin
)
);
bank.execute(_positionId, address(_spell), executeData);
doRefundETH();
doRefund(_params.tokenA);
doRefund(_params.tokenB);
doRefund(rewardToken);
doRefund(lp);
}
function harvestRewards(uint _positionId, ISushiswapSpellV1 _spell) external {
bank.execute(
_positionId,
address(_spell),
abi.encodeWithSelector(_spell.harvestWMasterChef.selector)
);
address rewardToken = getRewardToken(_positionId);
doRefund(rewardToken);
}
function getPendingRewards(uint _positionId) external view returns (uint pendingRewards) {
// query position info from position id
(, address collateralTokenAddress, uint collateralId, uint collateralAmount) = bank
.getPositionInfo(_positionId);
IWMasterChef wrapper = IWMasterChef(collateralTokenAddress);
IMasterChef chef = IMasterChef(wrapper.chef());
// get info for calculating rewards
(uint poolId, uint startRewardTokenPerShare) = wrapper.decodeId(collateralId);
uint endRewardTokenPerShare = calculateAccRewardPerShareChef(chef, poolId);
uint stReward = (startRewardTokenPerShare * collateralAmount).divCeil(PRECISION);
uint enReward = (endRewardTokenPerShare * collateralAmount) / PRECISION;
pendingRewards = (enReward > stReward) ? enReward - stReward : 0;
}
function calculateAccRewardPerShareChef(IMasterChef _chef, uint _poolId)
internal
view
returns (uint accSushiPerShare)
{
address lpToken;
uint allocPoint;
uint lastRewardBlock;
(lpToken, allocPoint, lastRewardBlock, accSushiPerShare) = _chef.poolInfo(_poolId);
if (block.number <= lastRewardBlock) {
return accSushiPerShare;
}
uint lpSupply = IERC20(lpToken).balanceOf(address(_chef));
if (lpSupply == 0) {
lastRewardBlock = block.number;
return accSushiPerShare;
}
uint multiplier = _chef.getMultiplier(lastRewardBlock, block.number);
uint sushiReward = (multiplier * _chef.sushiPerBlock() * allocPoint) / _chef.totalAllocPoint();
accSushiPerShare += ((sushiReward * PRECISION) / lpSupply);
}
function getRewardToken(uint _positionId) internal view returns (address rewardToken) {
// query position info from position id
(, address collateralTokenAddress, , ) = bank.getPositionInfo(_positionId);
IWMasterChef wrapper = IWMasterChef(collateralTokenAddress);
// find reward token address from wrapper
rewardToken = address(wrapper.sushi());
}
}