-
Notifications
You must be signed in to change notification settings - Fork 1
/
SwingTrader.sol
271 lines (221 loc) · 7.63 KB
/
SwingTrader.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
import "openzeppelin/token/ERC20/ERC20.sol";
import "openzeppelin/token/ERC20/utils/SafeERC20.sol";
import "../StabilizedPoolExtensions/StabilizedPoolUnit.sol";
import "../StabilizedPoolExtensions/DataLabExtension.sol";
import "../StabilizedPoolExtensions/ProfitDistributorExtension.sol";
import "../StabilizedPoolExtensions/DexHandlerExtension.sol";
import "../StabilizedPoolExtensions/SwingTraderManagerExtension.sol";
import "../libraries/SafeBurnMintableERC20.sol";
/// @title Swing Trader
/// @author 0xScotch <scotch@malt.money>
/// @notice The sole aim of this contract is to defend peg and try to profit in the process.
/// @dev It does so from a privileged internal position where it is allowed to purchase on the AMM even in recovery mode
contract SwingTrader is
StabilizedPoolUnit,
DataLabExtension,
ProfitDistributorExtension,
DexHandlerExtension,
SwingTraderManagerExtension
{
using SafeERC20 for ERC20;
using SafeBurnMintableERC20 for IBurnMintableERC20;
bytes32 public immutable CAPITAL_DELEGATE_ROLE;
bytes32 public immutable MANAGER_ROLE;
uint256 public deployedCapital;
uint256 public totalProfit;
event Delegation(uint256 amount, address destination, address delegate);
event BuyMalt(uint256 amount);
event SellMalt(uint256 amount, uint256 profit);
constructor(
address timelock,
address repository,
address poolFactory
) StabilizedPoolUnit(timelock, repository, poolFactory) {
CAPITAL_DELEGATE_ROLE = 0x6b525fb9eaf138d3dc2ac8323126c54cad39e34e800f9605cb60df858920b17b;
MANAGER_ROLE = 0x241ecf16d79d0f8dbfb92cbc07fe17840425976cf0667f022fe9877caa831b08;
_roleSetup(
0x6b525fb9eaf138d3dc2ac8323126c54cad39e34e800f9605cb60df858920b17b,
timelock
);
}
function setupContracts(
address _collateralToken,
address _malt,
address _dexHandler,
address _swingTraderManager,
address _maltDataLab,
address _profitDistributor,
address pool
) external onlyRoleMalt(POOL_FACTORY_ROLE, "Only pool factory role") {
require(!contractActive, "SwingTrader: Already setup");
require(_collateralToken != address(0), "SwingTrader: ColToken addr(0)");
require(_malt != address(0), "SwingTrader: Malt addr(0)");
require(_dexHandler != address(0), "SwingTrader: DexHandler addr(0)");
require(_swingTraderManager != address(0), "SwingTrader: Manager addr(0)");
require(_maltDataLab != address(0), "SwingTrader: MaltDataLab addr(0)");
contractActive = true;
_setupRole(MANAGER_ROLE, _swingTraderManager);
_setupRole(CAPITAL_DELEGATE_ROLE, _swingTraderManager);
collateralToken = ERC20(_collateralToken);
malt = IBurnMintableERC20(_malt);
dexHandler = IDexHandler(_dexHandler);
maltDataLab = IMaltDataLab(_maltDataLab);
profitDistributor = IProfitDistributor(_profitDistributor);
swingTraderManager = ISwingTrader(_swingTraderManager);
(, address updater, ) = poolFactory.getPool(pool);
_setPoolUpdater(updater);
}
function _beforeSetSwingTraderManager(address _swingTraderManager)
internal
override
{
_transferRole(
_swingTraderManager,
address(swingTraderManager),
MANAGER_ROLE
);
_transferRole(
_swingTraderManager,
address(swingTraderManager),
CAPITAL_DELEGATE_ROLE
);
}
function buyMalt(uint256 maxCapital)
external
onlyRoleMalt(MANAGER_ROLE, "Must have swing trader manager privs")
onlyActive
returns (uint256 capitalUsed)
{
if (maxCapital == 0) {
return 0;
}
uint256 balance = collateralToken.balanceOf(address(this));
if (balance == 0) {
return 0;
}
if (maxCapital < balance) {
balance = maxCapital;
}
collateralToken.safeTransfer(address(dexHandler), balance);
dexHandler.buyMalt(balance, 10000); // 100% allowable slippage
deployedCapital = deployedCapital + balance;
emit BuyMalt(balance);
return balance;
}
function sellMalt(uint256 maxAmount)
external
onlyRoleMalt(MANAGER_ROLE, "Must have swing trader manager privs")
onlyActive
returns (uint256 amountSold)
{
if (maxAmount == 0) {
return 0;
}
uint256 totalMaltBalance = malt.balanceOf(address(this));
if (totalMaltBalance == 0) {
return 0;
}
(uint256 basis, ) = costBasis();
if (maxAmount > totalMaltBalance) {
maxAmount = totalMaltBalance;
}
malt.safeTransfer(address(dexHandler), maxAmount);
uint256 rewards = dexHandler.sellMalt(maxAmount, 10000);
uint256 deployed = deployedCapital; // gas
if (rewards <= deployed && maxAmount < totalMaltBalance) {
// If all malt is spent we want to reset deployed capital
deployedCapital = deployed - rewards;
} else {
deployedCapital = 0;
}
uint256 profit = _calculateProfit(basis, maxAmount, rewards);
_handleProfitDistribution(profit);
totalProfit += profit;
emit SellMalt(maxAmount, profit);
return maxAmount;
}
function _handleProfitDistribution(uint256 profit) internal virtual {
if (profit != 0) {
collateralToken.safeTransfer(address(profitDistributor), profit);
profitDistributor.handleProfit(profit);
}
}
function costBasis() public view returns (uint256 cost, uint256 decimals) {
// Always returns using the decimals of the collateralToken as that is the
// currency costBasis is calculated in
decimals = collateralToken.decimals();
uint256 maltBalance = maltDataLab.maltToRewardDecimals(
malt.balanceOf(address(this))
);
uint256 deployed = deployedCapital; // gas
if (deployed == 0 || maltBalance == 0) {
return (0, decimals);
}
return ((deployed * (10**decimals)) / maltBalance, decimals);
}
function _calculateProfit(
uint256 costBasis,
uint256 soldAmount,
uint256 recieved
) internal returns (uint256 profit) {
if (costBasis == 0) {
return 0;
}
uint256 decimals = collateralToken.decimals();
uint256 maltDecimals = malt.decimals();
soldAmount = maltDataLab.maltToRewardDecimals(soldAmount);
uint256 soldBasis = (costBasis * soldAmount) / (10**decimals);
require(recieved > soldBasis, "Not profitable trade");
profit = recieved - soldBasis;
}
function calculateSwingTraderMaltRatio()
external
view
returns (uint256 maltRatio)
{
uint256 decimals = collateralToken.decimals();
uint256 maltDecimals = malt.decimals();
uint256 stCollateralBalance = collateralToken.balanceOf(address(this));
uint256 stMaltBalance = maltDataLab.maltToRewardDecimals(
malt.balanceOf(address(this))
);
uint256 stMaltValue = (stMaltBalance * maltDataLab.priceTarget()) /
(10**decimals);
uint256 netBalance = stCollateralBalance + stMaltValue;
if (netBalance > 0) {
maltRatio = ((stMaltValue * (10**decimals)) / netBalance);
} else {
maltRatio = 0;
}
}
function getTokenBalances()
external
view
returns (uint256 maltBalance, uint256 collateralBalance)
{
maltBalance = malt.balanceOf(address(this));
collateralBalance = collateralToken.balanceOf(address(this));
}
function delegateCapital(uint256 amount, address destination)
external
onlyRoleMalt(CAPITAL_DELEGATE_ROLE, "Must have capital delegation privs")
onlyActive
{
collateralToken.safeTransfer(destination, amount);
emit Delegation(amount, destination, msg.sender);
}
function _accessControl()
internal
virtual
override(
DataLabExtension,
ProfitDistributorExtension,
DexHandlerExtension,
SwingTraderManagerExtension
)
{
_onlyRoleMalt(POOL_UPDATER_ROLE, "Must have pool updater role");
}
}