forked from AlphaFinanceLab/alpha-homora-v2-integration-doc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TraderJoeSpellV3IntegrationAvax.sol
222 lines (186 loc) · 7.83 KB
/
TraderJoeSpellV3IntegrationAvax.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
// 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/homorav2/banks/IBankAVAX.sol';
import '../../interfaces/homorav2/wrappers/IWBoostedMasterChefJoeWorker.sol';
import '../../interfaces/homorav2/spells/ITraderJoeSpellV3.sol';
import '../../interfaces/traderjoe/IBoostedMasterChefJoe.sol';
import '../../interfaces/traderjoe/ITraderJoeFactory.sol';
import 'forge-std/console2.sol';
contract TraderJoeSpellV3IntegrationAvax is BaseIntegration {
using SafeERC20 for IERC20;
using HomoraMath for uint;
IBankAVAX bank; // homora bank
ITraderJoeFactory factory; // traderjoe factory
uint constant PRECISION = 10**18;
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 BoostedMasterChefJoe
}
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(IBankAVAX _bank, ITraderJoeFactory _factory) {
bank = _bank;
factory = _factory;
}
function openPosition(ITraderJoeSpellV3 _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,
ITraderJoeSpellV3.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,
ITraderJoeSpellV3 _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,
ITraderJoeSpellV3.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,
ITraderJoeSpellV3 _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,
ITraderJoeSpellV3.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(lp);
doRefund(rewardToken);
}
function harvestRewards(uint _positionId, ITraderJoeSpellV3 _spell) external {
bank.execute(
_positionId,
address(_spell),
abi.encodeWithSelector(ITraderJoeSpellV3.harvestWMasterChef.selector)
);
// find reward token address from wrapper
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);
IWBoostedMasterChefJoeWorker wrapper = IWBoostedMasterChefJoeWorker(collateralTokenAddress);
IBoostedMasterChefJoe chef = IBoostedMasterChefJoe(wrapper.chef());
// get info for calculating rewards
(uint poolId, uint startRewardTokenPerShare) = wrapper.decodeId(collateralId);
uint endRewardTokenPerShare = wrapper.accJoePerShare();
(uint totalSupply, , ) = chef.userInfo(poolId, address(wrapper)); // total lp from wrapper deposited in Chef
// pending rewards separates into two parts
// 1. pending rewards that are in the wrapper contract
// 2. pending rewards that wrapper hasn't claimed from Chef's contract
(uint pendingRewardFromChef, , , ) = chef.pendingTokens(poolId, address(wrapper));
endRewardTokenPerShare += (pendingRewardFromChef * PRECISION) / totalSupply;
uint stReward = (startRewardTokenPerShare * collateralAmount).divCeil(PRECISION);
uint enReward = (endRewardTokenPerShare * collateralAmount) / PRECISION;
pendingRewards = (enReward > stReward) ? enReward - stReward : 0;
}
function getRewardToken(uint _positionId) internal view returns (address rewardToken) {
// query position info from position id
(, address collateralTokenAddress, , ) = bank.getPositionInfo(_positionId);
IWBoostedMasterChefJoeWorker wrapper = IWBoostedMasterChefJoeWorker(collateralTokenAddress);
// find reward token address from wrapper
rewardToken = address(wrapper.joe());
}
}