-
Notifications
You must be signed in to change notification settings - Fork 108
/
TradeLib.sol
196 lines (176 loc) · 8.29 KB
/
TradeLib.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
// SPDX-License-Identifier: BlueOak-1.0.0
pragma solidity 0.8.17;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "../../interfaces/IAsset.sol";
import "../../interfaces/IAssetRegistry.sol";
import "../../interfaces/ITrading.sol";
import "../../libraries/Fixed.sol";
import "./RecollateralizationLib.sol";
struct TradeInfo {
IAsset sell;
IAsset buy;
uint192 sellAmount; // {sellTok}
uint192 buyAmount; // {buyTok}
uint192 sellPrice; // {UoA/sellTok} can be 0
uint192 buyPrice; // {UoA/buyTok}
}
/**
* @title TradeLib
* @notice An internal lib for preparing individual trades on particular asset pairs
* Users:
* - BackingManagerLib
* - RevenueTrader
*/
library TradeLib {
using FixLib for uint192;
/// Prepare a trade to sell `trade.sellAmount` that guarantees a reasonable closing price,
/// without explicitly aiming at a particular quantity to purchase.
/// @param trade:
/// sell != 0, sellAmount >= 0 {sellTok}, sellPrice >= 0 {UoA/sellTok}
/// buy != 0, buyAmount (unused) {buyTok}, buyPrice > 0 {UoA/buyTok}
/// @return notDust True when the trade is larger than the dust amount
/// @return req The prepared trade request to send to the Broker
//
// If notDust is true, then the returned trade request satisfies:
// req.sell == trade.sell and req.buy == trade.buy,
// req.minBuyAmount * trade.buyPrice ~=
// trade.sellAmount * trade.sellPrice * (1-maxTradeSlippage),
// req.sellAmount == min(trade.sell.maxTradeSize().toQTok(), trade.sellAmount.toQTok(sell)
// 1 < req.sellAmount
//
// If notDust is false, no trade exists that satisfies those constraints.
function prepareTradeSell(
TradeInfo memory trade,
uint192 minTradeVolume,
uint192 maxTradeSlippage
) internal view returns (bool notDust, TradeRequest memory req) {
// checked for in RevenueTrader / CollateralizatlionLib
assert(trade.buyPrice > 0 && trade.buyPrice < FIX_MAX && trade.sellPrice < FIX_MAX);
(uint192 lotLow, uint192 lotHigh) = trade.sell.lotPrice();
// Don't sell dust
if (!isEnoughToSell(trade.sell, trade.sellAmount, lotLow, minTradeVolume)) {
return (false, req);
}
// Cap sell amount
uint192 maxSell = maxTradeSize(trade.sell, lotHigh); // {sellTok}
uint192 s = trade.sellAmount > maxSell ? maxSell : trade.sellAmount; // {sellTok}
// Calculate equivalent buyAmount within [0, FIX_MAX]
// {buyTok} = {sellTok} * {1} * {UoA/sellTok} / {UoA/buyTok}
uint192 b = safeMulDivCeil(
ITrading(address(this)),
s.mul(FIX_ONE.minus(maxTradeSlippage)),
trade.sellPrice, // {UoA/sellTok}
trade.buyPrice // {UoA/buyTok}
);
// {*tok} => {q*Tok}
req.sellAmount = s.shiftl_toUint(int8(trade.sell.erc20Decimals()), FLOOR);
req.minBuyAmount = b.shiftl_toUint(int8(trade.buy.erc20Decimals()), CEIL);
req.sell = trade.sell;
req.buy = trade.buy;
return (true, req);
}
/// Assuming we have `trade.sellAmount` sell tokens available, prepare a trade to cover as
/// much of our deficit of `trade.buyAmount` buy tokens as possible, given expected trade
/// slippage and the sell asset's maxTradeVolume().
/// @param trade:
/// sell != 0
/// buy != 0
/// sellAmount (unused) {sellTok}
/// buyAmount >= 0 {buyTok}
/// sellPrice > 0 {UoA/sellTok}
/// buyPrice > 0 {UoA/buyTok}
/// @return notDust Whether the prepared trade is large enough to be worth trading
/// @return req The prepared trade request to send to the Broker
//
// Returns prepareTradeSell(trade, rules), where
// req.sellAmount = min(trade.sellAmount,
// trade.buyAmount * (trade.buyPrice / trade.sellPrice) / (1-maxTradeSlippage))
// i.e, the minimum of trade.sellAmount and (a sale amount that, at current prices and
// maximum slippage, will yield at least the requested trade.buyAmount)
//
// Which means we should get that, if notDust is true, then:
// req.sell = sell and req.buy = buy
//
// 1 <= req.minBuyAmount <= max(trade.buyAmount, buy.minTradeSize()).toQTok(trade.buy)
// 1 < req.sellAmount <= min(trade.sellAmount.toQTok(trade.sell),
// sell.maxTradeSize().toQTok(trade.sell))
// req.minBuyAmount ~= trade.sellAmount * sellPrice / buyPrice * (1-maxTradeSlippage)
//
// req.sellAmount (and req.minBuyAmount) are maximal satisfying all these conditions
function prepareTradeToCoverDeficit(
TradeInfo memory trade,
uint192 minTradeVolume,
uint192 maxTradeSlippage
) internal view returns (bool notDust, TradeRequest memory req) {
assert(
trade.sellPrice > 0 &&
trade.sellPrice < FIX_MAX &&
trade.buyPrice > 0 &&
trade.buyPrice < FIX_MAX
);
// Don't buy dust.
trade.buyAmount = fixMax(trade.buyAmount, minTradeSize(minTradeVolume, trade.buyPrice));
// {sellTok} = {buyTok} * {UoA/buyTok} / {UoA/sellTok}
uint192 exactSellAmount = trade.buyAmount.mulDiv(trade.buyPrice, trade.sellPrice, CEIL);
// exactSellAmount: Amount to sell to buy `deficitAmount` if there's no slippage
// slippedSellAmount: Amount needed to sell to buy `deficitAmount`, counting slippage
uint192 slippedSellAmount = exactSellAmount.div(FIX_ONE.minus(maxTradeSlippage), CEIL);
trade.sellAmount = fixMin(slippedSellAmount, trade.sellAmount); // {sellTok}
return prepareTradeSell(trade, minTradeVolume, maxTradeSlippage);
}
/// @param asset The asset in consideration
/// @param amt {tok} The number of whole tokens we plan to sell
/// @param price {UoA/tok} The price to use for sizing
/// @param minTradeVolume {UoA} The min trade volume, passed in for gas optimization
/// @return If amt is sufficiently large to be worth selling into our trading platforms
function isEnoughToSell(
IAsset asset,
uint192 amt,
uint192 price,
uint192 minTradeVolume
) internal view returns (bool) {
return
amt.gte(minTradeSize(minTradeVolume, price)) &&
// Trading platforms often don't allow token quanta trades for rounding reasons
// {qTok} = {tok} / {tok/qTok}
amt.shiftl_toUint(int8(asset.erc20Decimals())) > 1;
}
/// @return The result of FixLib.mulDiv bounded from above by FIX_MAX in the case of overflow
function safeMulDivCeil(
ITrading trader,
uint192 x,
uint192 y,
uint192 z
) internal pure returns (uint192) {
try trader.mulDivCeil(x, y, z) returns (uint192 result) {
return result;
} catch Panic(uint256 errorCode) {
// 0x11: overflow
// 0x12: div-by-zero
// untestable:
// Overflow is protected against and checked for in FixLib.mulDiv()
// Div-by-zero is NOT protected against, but no caller will ever use 0 for z
assert(errorCode == 0x11 || errorCode == 0x12);
} catch (bytes memory reason) {
assert(keccak256(reason) == UIntOutofBoundsHash);
}
return FIX_MAX;
}
// === Private ===
/// Calculates the minTradeSize for an asset based on the given minTradeVolume and price
/// @param minTradeVolume {UoA} The min trade volume, passed in for gas optimization
/// @return {tok} The min trade size for the asset in whole tokens
function minTradeSize(uint192 minTradeVolume, uint192 price) private pure returns (uint192) {
// {tok} = {UoA} / {UoA/tok}
uint192 size = price == 0 ? FIX_MAX : minTradeVolume.div(price, CEIL);
return size > 0 ? size : 1;
}
/// Calculates the maxTradeSize for an asset based on the asset's maxTradeVolume and price
/// @return {tok} The max trade size for the asset in whole tokens
function maxTradeSize(IAsset asset, uint192 price) private view returns (uint192) {
// untestable:
// Price cannot be 0, it would've been filtered before in `prepareTradeSell`
uint192 size = price == 0 ? FIX_MAX : asset.maxTradeVolume().div(price, FLOOR);
return size > 0 ? size : 1;
}
}