This repository has been archived by the owner on May 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
StrategyUtils.sol
177 lines (156 loc) · 6.94 KB
/
StrategyUtils.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
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity 0.8.17;
import {IERC20} from "../../../interfaces/IERC20.sol";
import {
TradeParams,
DepositTradeParams,
RedeemParams,
SingleSidedRewardTradeParams
} from "../../../interfaces/notional/ISingleSidedLPStrategyVault.sol";
import {TradeHandler} from "../../trading/TradeHandler.sol";
import {Deployments} from "../../global/Deployments.sol";
import {Constants} from "../../global/Constants.sol";
import {Errors} from "../../global/Errors.sol";
import {ITradingModule, Trade, TradeType, DexId} from "../../../interfaces/trading/ITradingModule.sol";
/**
* @notice External library deployed for the purposes of handling SingleSidedLP trades. All
* the methods in this library are called inside a `delegateCall` context which ensures that
* the library has access to the calling vault's token balances
*/
library StrategyUtils {
using TradeHandler for Trade;
/// @notice Trades the amount of primary token into other secondary tokens prior
/// to entering a pool.
function executeDepositTrades(
IERC20[] memory tokens,
uint256[] memory amounts,
DepositTradeParams[] memory depositTrades,
uint256 primaryIndex
) external returns (uint256[] memory) {
address primaryToken = address(tokens[primaryIndex]);
for (uint256 i; i < amounts.length; i++) {
if (i == primaryIndex) continue;
DepositTradeParams memory t = depositTrades[i];
// Do not allow ZERO_EX trading in this method since we cannot validate
// the arbitrary exchange data.
if (DexId(t.tradeParams.dexId) == DexId.ZERO_EX) revert Errors.InvalidDexId(uint256(DexId.ZERO_EX));
if (t.tradeAmount > 0) {
// Always selling the primaryToken and buying the secondary token.
(uint256 amountSold, uint256 amountBought) = _executeDynamicSlippageTradeExactIn(
Deployments.TRADING_MODULE, t.tradeParams, primaryToken, address(tokens[i]), t.tradeAmount
);
amounts[i] = amountBought;
// Will revert on underflow if over-selling the primary borrowed
amounts[primaryIndex] -= amountSold;
}
}
return amounts;
}
/// @notice Trades the amount of secondary tokens into the primary token after
/// exiting a pool.
function executeRedemptionTrades(
IERC20[] memory tokens,
uint256[] memory exitBalances,
TradeParams[] memory redemptionTrades,
uint256 primaryIndex
) external returns (uint256 finalPrimaryBalance) {
address primaryToken = address(tokens[primaryIndex]);
for (uint256 i; i < exitBalances.length; i++) {
if (i == primaryIndex) {
finalPrimaryBalance += exitBalances[i];
continue;
}
TradeParams memory t = redemptionTrades[i];
// Do not allow ZERO_EX trading in this method since we cannot validate
// the arbitrary exchange data.
if (DexId(t.dexId) == DexId.ZERO_EX) revert Errors.InvalidDexId(uint256(DexId.ZERO_EX));
// Always sell the entire exit balance to the primary token
if (exitBalances[i] > 0) {
(/* */, uint256 amountBought) = _executeDynamicSlippageTradeExactIn(
Deployments.TRADING_MODULE, t, address(tokens[i]), primaryToken, exitBalances[i]
);
finalPrimaryBalance += amountBought;
}
}
}
/// @notice Executes a set of trades to sell the reward token for constituent pool tokens.
function executeRewardTrades(
IERC20[] memory tokens,
SingleSidedRewardTradeParams[] calldata trades,
address rewardToken,
address poolToken
) external returns(uint256[] memory amounts, uint256 amountSold) {
amounts = new uint256[](trades.length);
for (uint256 i; i < trades.length; i++) {
// All trades must sell the same token.
require(trades[i].sellToken == rewardToken);
// Bypass certain invalid trades
if (trades[i].amount == 0) continue;
if (trades[i].buyToken == poolToken) continue;
// The reward trade can only purchase tokens that go into the pool
require(trades[i].buyToken == address(tokens[i]));
// It may be possible that the entire balance of reward tokens is not sold by the vault,
// but that is ok.
(uint256 sold, uint256 bought) = _executeTradeWithStaticSlippage(
Deployments.TRADING_MODULE, trades[i].tradeParams, rewardToken, trades[i].buyToken, trades[i].amount
);
amounts[i] = bought;
amountSold += sold;
}
}
/// @notice Executes a trade that uses a dynamic slippage amount relative to the current
/// oracle price.
function _executeDynamicSlippageTradeExactIn(
ITradingModule tradingModule,
TradeParams memory params,
address sellToken,
address buyToken,
uint256 amount
) internal returns (uint256 amountSold, uint256 amountBought) {
// Can only do exact in trades
require(
params.tradeType == TradeType.EXACT_IN_SINGLE ||
params.tradeType == TradeType.EXACT_IN_BATCH
);
// Ensure that the slippage percent is valid
require(params.oracleSlippagePercentOrLimit <= Constants.SLIPPAGE_LIMIT_PRECISION);
Trade memory trade = Trade(
params.tradeType,
sellToken,
buyToken,
amount,
0, // No absolute slippage limit is set here
block.timestamp, // deadline
params.exchangeData
);
(amountSold, amountBought) = trade._executeTradeWithDynamicSlippage(
params.dexId, tradingModule, uint32(params.oracleSlippagePercentOrLimit)
);
}
/// @notice Executes a trade with a static slippage limit, only used during
/// reward reinvestment trades since oracles between the reward token and the
/// purchased tokens may not exist.
function _executeTradeWithStaticSlippage(
ITradingModule tradingModule,
TradeParams memory params,
address sellToken,
address buyToken,
uint256 amount
) internal returns (uint256 amountSold, uint256 amountBought) {
require(
params.tradeType == TradeType.EXACT_IN_SINGLE ||
params.tradeType == TradeType.EXACT_IN_BATCH
);
Trade memory trade = Trade(
params.tradeType,
sellToken,
buyToken,
amount,
params.oracleSlippagePercentOrLimit,
block.timestamp, // deadline
params.exchangeData
);
// Execute trade using the absolute slippage limit set by `oracleSlippagePercentOrLimit`
(amountSold, amountBought) = trade._executeTrade(params.dexId, tradingModule);
}
}