@dolomite-exchange/dolomite-margin 0.1.1 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. package/dist/contracts/protocol/Admin.sol +319 -0
  2. package/dist/contracts/protocol/DolomiteMargin.sol +56 -0
  3. package/dist/contracts/protocol/Getters.sol +527 -0
  4. package/dist/contracts/protocol/Operation.sol +67 -0
  5. package/dist/contracts/protocol/Permission.sol +68 -0
  6. package/dist/contracts/protocol/State.sol +34 -0
  7. package/dist/contracts/protocol/impl/AdminImpl.sol +534 -0
  8. package/dist/contracts/protocol/impl/LiquidateOrVaporizeImpl.sol +415 -0
  9. package/dist/contracts/protocol/impl/OperationImpl.sol +655 -0
  10. package/dist/contracts/protocol/impl/artifacts/OperationImpl.json +80 -0
  11. package/dist/contracts/protocol/impl/artifacts/OperationImpl_metadata.json +193 -0
  12. package/dist/contracts/protocol/interfaces/IAutoTrader.sol +63 -0
  13. package/dist/contracts/protocol/interfaces/ICallee.sol +48 -0
  14. package/dist/contracts/protocol/interfaces/IDolomiteMargin.sol +468 -0
  15. package/dist/contracts/protocol/interfaces/IERC20.sol +100 -0
  16. package/dist/contracts/protocol/interfaces/IExchangeWrapper.sol +78 -0
  17. package/dist/contracts/protocol/interfaces/IInterestSetter.sol +51 -0
  18. package/dist/contracts/protocol/interfaces/ILiquidationCallback.sol +46 -0
  19. package/dist/contracts/protocol/interfaces/IPriceOracle.sol +54 -0
  20. package/dist/contracts/protocol/interfaces/IRecyclable.sol +115 -0
  21. package/dist/contracts/protocol/lib/Account.sol +78 -0
  22. package/dist/contracts/protocol/lib/Actions.sol +407 -0
  23. package/dist/contracts/protocol/lib/Cache.sol +251 -0
  24. package/dist/contracts/protocol/lib/Decimal.sol +86 -0
  25. package/dist/contracts/protocol/lib/EnumerableSet.sol +114 -0
  26. package/dist/contracts/protocol/lib/Events.sol +463 -0
  27. package/dist/contracts/protocol/lib/Exchange.sol +151 -0
  28. package/dist/contracts/protocol/lib/Interest.sol +194 -0
  29. package/dist/contracts/protocol/lib/Math.sol +166 -0
  30. package/dist/contracts/protocol/lib/Monetary.sol +44 -0
  31. package/dist/contracts/protocol/lib/Require.sol +413 -0
  32. package/dist/contracts/protocol/lib/Storage.sol +790 -0
  33. package/dist/contracts/protocol/lib/Time.sol +42 -0
  34. package/dist/contracts/protocol/lib/Token.sol +97 -0
  35. package/dist/contracts/protocol/lib/Types.sol +314 -0
  36. package/package.json +3 -2
@@ -0,0 +1,319 @@
1
+ /*
2
+
3
+ Copyright 2019 dYdX Trading Inc.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+
17
+ */
18
+
19
+ pragma solidity ^0.5.7;
20
+ pragma experimental ABIEncoderV2;
21
+
22
+ import { Ownable } from "openzeppelin-solidity/contracts/ownership/Ownable.sol";
23
+ import { ReentrancyGuard } from "openzeppelin-solidity/contracts/utils/ReentrancyGuard.sol";
24
+ import { State } from "./State.sol";
25
+ import { AdminImpl } from "./impl/AdminImpl.sol";
26
+ import { IInterestSetter } from "./interfaces/IInterestSetter.sol";
27
+ import { IPriceOracle } from "./interfaces/IPriceOracle.sol";
28
+ import { Decimal } from "./lib/Decimal.sol";
29
+ import { Interest } from "./lib/Interest.sol";
30
+ import { Monetary } from "./lib/Monetary.sol";
31
+ import { Token } from "./lib/Token.sol";
32
+
33
+
34
+ /**
35
+ * @title Admin
36
+ * @author dYdX
37
+ *
38
+ * Public functions that allow the privileged owner address to manage DolomiteMargin
39
+ */
40
+ contract Admin is
41
+ State,
42
+ Ownable,
43
+ ReentrancyGuard
44
+ {
45
+ // ============ Token Functions ============
46
+
47
+ /**
48
+ * Withdraw an ERC20 token for which there is an associated market. Only excess tokens can be
49
+ * withdrawn. The number of excess tokens is calculated by taking the current number of tokens
50
+ * held in DolomiteMargin, adding the number of tokens owed to DolomiteMargin by borrowers, and subtracting the
51
+ * number of tokens owed to suppliers by DolomiteMargin.
52
+ */
53
+ function ownerWithdrawExcessTokens(
54
+ uint256 marketId,
55
+ address recipient
56
+ )
57
+ public
58
+ onlyOwner
59
+ nonReentrant
60
+ returns (uint256)
61
+ {
62
+ return AdminImpl.ownerWithdrawExcessTokens(
63
+ g_state,
64
+ marketId,
65
+ recipient
66
+ );
67
+ }
68
+
69
+ /**
70
+ * Withdraw an ERC20 token for which there is no associated market.
71
+ */
72
+ function ownerWithdrawUnsupportedTokens(
73
+ address token,
74
+ address recipient
75
+ )
76
+ public
77
+ onlyOwner
78
+ nonReentrant
79
+ returns (uint256)
80
+ {
81
+ return AdminImpl.ownerWithdrawUnsupportedTokens(
82
+ g_state,
83
+ token,
84
+ recipient
85
+ );
86
+ }
87
+
88
+ // ============ Market Functions ============
89
+
90
+ /**
91
+ * Add a new market to DolomiteMargin. Must be for a previously-unsupported ERC20 token.
92
+ */
93
+ function ownerAddMarket(
94
+ address token,
95
+ IPriceOracle priceOracle,
96
+ IInterestSetter interestSetter,
97
+ Decimal.D256 memory marginPremium,
98
+ Decimal.D256 memory spreadPremium,
99
+ bool isClosing,
100
+ bool isRecyclable
101
+ )
102
+ public
103
+ onlyOwner
104
+ nonReentrant
105
+ {
106
+ AdminImpl.ownerAddMarket(
107
+ g_state,
108
+ token,
109
+ priceOracle,
110
+ interestSetter,
111
+ marginPremium,
112
+ spreadPremium,
113
+ isClosing,
114
+ isRecyclable
115
+ );
116
+ }
117
+
118
+ /**
119
+ * Removes a market from DolomiteMargin, sends any remaining tokens in this contract to `salvager` and invokes the recyclable
120
+ * callback
121
+ */
122
+ function ownerRemoveMarkets(
123
+ uint[] memory marketIds,
124
+ address salvager
125
+ )
126
+ public
127
+ onlyOwner
128
+ nonReentrant
129
+ {
130
+ AdminImpl.ownerRemoveMarkets(
131
+ g_state,
132
+ marketIds,
133
+ salvager
134
+ );
135
+ }
136
+
137
+ /**
138
+ * Set (or unset) the status of a market to "closing". The borrowedValue of a market cannot
139
+ * increase while its status is "closing".
140
+ */
141
+ function ownerSetIsClosing(
142
+ uint256 marketId,
143
+ bool isClosing
144
+ )
145
+ public
146
+ onlyOwner
147
+ nonReentrant
148
+ {
149
+ AdminImpl.ownerSetIsClosing(
150
+ g_state,
151
+ marketId,
152
+ isClosing
153
+ );
154
+ }
155
+
156
+ /**
157
+ * Set the price oracle for a market.
158
+ */
159
+ function ownerSetPriceOracle(
160
+ uint256 marketId,
161
+ IPriceOracle priceOracle
162
+ )
163
+ public
164
+ onlyOwner
165
+ nonReentrant
166
+ {
167
+ AdminImpl.ownerSetPriceOracle(
168
+ g_state,
169
+ marketId,
170
+ priceOracle
171
+ );
172
+ }
173
+
174
+ /**
175
+ * Set the interest-setter for a market.
176
+ */
177
+ function ownerSetInterestSetter(
178
+ uint256 marketId,
179
+ IInterestSetter interestSetter
180
+ )
181
+ public
182
+ onlyOwner
183
+ nonReentrant
184
+ {
185
+ AdminImpl.ownerSetInterestSetter(
186
+ g_state,
187
+ marketId,
188
+ interestSetter
189
+ );
190
+ }
191
+
192
+ /**
193
+ * Set a premium on the minimum margin-ratio for a market. This makes it so that any positions
194
+ * that include this market require a higher collateralization to avoid being liquidated.
195
+ */
196
+ function ownerSetMarginPremium(
197
+ uint256 marketId,
198
+ Decimal.D256 memory marginPremium
199
+ )
200
+ public
201
+ onlyOwner
202
+ nonReentrant
203
+ {
204
+ AdminImpl.ownerSetMarginPremium(
205
+ g_state,
206
+ marketId,
207
+ marginPremium
208
+ );
209
+ }
210
+
211
+ /**
212
+ * Set a premium on the liquidation spread for a market. This makes it so that any liquidations
213
+ * that include this market have a higher spread than the global default.
214
+ */
215
+ function ownerSetSpreadPremium(
216
+ uint256 marketId,
217
+ Decimal.D256 memory spreadPremium
218
+ )
219
+ public
220
+ onlyOwner
221
+ nonReentrant
222
+ {
223
+ AdminImpl.ownerSetSpreadPremium(
224
+ g_state,
225
+ marketId,
226
+ spreadPremium
227
+ );
228
+ }
229
+
230
+ // ============ Risk Functions ============
231
+
232
+ /**
233
+ * Set the global minimum margin-ratio that every position must maintain to prevent being
234
+ * liquidated.
235
+ */
236
+ function ownerSetMarginRatio(
237
+ Decimal.D256 memory ratio
238
+ )
239
+ public
240
+ onlyOwner
241
+ nonReentrant
242
+ {
243
+ AdminImpl.ownerSetMarginRatio(
244
+ g_state,
245
+ ratio
246
+ );
247
+ }
248
+
249
+ /**
250
+ * Set the global liquidation spread. This is the spread between oracle prices that incentivizes
251
+ * the liquidation of risky positions.
252
+ */
253
+ function ownerSetLiquidationSpread(
254
+ Decimal.D256 memory spread
255
+ )
256
+ public
257
+ onlyOwner
258
+ nonReentrant
259
+ {
260
+ AdminImpl.ownerSetLiquidationSpread(
261
+ g_state,
262
+ spread
263
+ );
264
+ }
265
+
266
+ /**
267
+ * Set the global earnings-rate variable that determines what percentage of the interest paid
268
+ * by borrowers gets passed-on to suppliers.
269
+ */
270
+ function ownerSetEarningsRate(
271
+ Decimal.D256 memory earningsRate
272
+ )
273
+ public
274
+ onlyOwner
275
+ nonReentrant
276
+ {
277
+ AdminImpl.ownerSetEarningsRate(
278
+ g_state,
279
+ earningsRate
280
+ );
281
+ }
282
+
283
+ /**
284
+ * Set the global minimum-borrow value which is the minimum value of any new borrow on DolomiteMargin.
285
+ */
286
+ function ownerSetMinBorrowedValue(
287
+ Monetary.Value memory minBorrowedValue
288
+ )
289
+ public
290
+ onlyOwner
291
+ nonReentrant
292
+ {
293
+ AdminImpl.ownerSetMinBorrowedValue(
294
+ g_state,
295
+ minBorrowedValue
296
+ );
297
+ }
298
+
299
+ // ============ Global Operator Functions ============
300
+
301
+ /**
302
+ * Approve (or disapprove) an address that is permissioned to be an operator for all accounts in
303
+ * DolomiteMargin. Intended only to approve smart-contracts.
304
+ */
305
+ function ownerSetGlobalOperator(
306
+ address operator,
307
+ bool approved
308
+ )
309
+ public
310
+ onlyOwner
311
+ nonReentrant
312
+ {
313
+ AdminImpl.ownerSetGlobalOperator(
314
+ g_state,
315
+ operator,
316
+ approved
317
+ );
318
+ }
319
+ }
@@ -0,0 +1,56 @@
1
+ /*
2
+
3
+ Copyright 2019 dYdX Trading Inc.
4
+
5
+ Licensed under the Apache License, Version 2.0 (the "License");
6
+ you may not use this file except in compliance with the License.
7
+ You may obtain a copy of the License at
8
+
9
+ http://www.apache.org/licenses/LICENSE-2.0
10
+
11
+ Unless required by applicable law or agreed to in writing, software
12
+ distributed under the License is distributed on an "AS IS" BASIS,
13
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
+ See the License for the specific language governing permissions and
15
+ limitations under the License.
16
+
17
+ */
18
+
19
+ pragma solidity ^0.5.7;
20
+ pragma experimental ABIEncoderV2;
21
+
22
+ import { Admin } from "./Admin.sol";
23
+ import { Getters } from "./Getters.sol";
24
+ import { Operation } from "./Operation.sol";
25
+ import { Permission } from "./Permission.sol";
26
+ import { State } from "./State.sol";
27
+ import { Storage } from "./lib/Storage.sol";
28
+ import { IDolomiteMargin } from "./interfaces/IDolomiteMargin.sol";
29
+
30
+
31
+ /**
32
+ * @title DolomiteMargin
33
+ * @author dYdX
34
+ *
35
+ * Main contract that inherits from other contracts
36
+ */
37
+ contract DolomiteMargin is
38
+ IDolomiteMargin,
39
+ State,
40
+ Admin,
41
+ Getters,
42
+ Operation,
43
+ Permission
44
+ {
45
+ // ============ Constructor ============
46
+
47
+ constructor(
48
+ Storage.RiskParams memory riskParams,
49
+ Storage.RiskLimits memory riskLimits
50
+ )
51
+ public
52
+ {
53
+ g_state.riskParams = riskParams;
54
+ g_state.riskLimits = riskLimits;
55
+ }
56
+ }