This repository has been archived by the owner on Jan 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 156
/
OperationImpl.sol
962 lines (848 loc) · 26.4 KB
/
OperationImpl.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
/*
Copyright 2019 dYdX Trading Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity 0.5.7;
pragma experimental ABIEncoderV2;
import { SafeMath } from "openzeppelin-solidity/contracts/math/SafeMath.sol";
import { IAutoTrader } from "../interfaces/IAutoTrader.sol";
import { ICallee } from "../interfaces/ICallee.sol";
import { Account } from "../lib/Account.sol";
import { Actions } from "../lib/Actions.sol";
import { Cache } from "../lib/Cache.sol";
import { Decimal } from "../lib/Decimal.sol";
import { Events } from "../lib/Events.sol";
import { Exchange } from "../lib/Exchange.sol";
import { Math } from "../lib/Math.sol";
import { Monetary } from "../lib/Monetary.sol";
import { Require } from "../lib/Require.sol";
import { Storage } from "../lib/Storage.sol";
import { Types } from "../lib/Types.sol";
/**
* @title OperationImpl
* @author dYdX
*
* Logic for processing actions
*/
library OperationImpl {
using Cache for Cache.MarketCache;
using SafeMath for uint256;
using Storage for Storage.State;
using Types for Types.Par;
using Types for Types.Wei;
// ============ Constants ============
bytes32 constant FILE = "OperationImpl";
// ============ Public Functions ============
function operate(
Storage.State storage state,
Account.Info[] memory accounts,
Actions.ActionArgs[] memory actions
)
public
{
Events.logOperation();
_verifyInputs(accounts, actions);
(
bool[] memory primaryAccounts,
Cache.MarketCache memory cache
) = _runPreprocessing(
state,
accounts,
actions
);
_runActions(
state,
accounts,
actions,
cache
);
_verifyFinalState(
state,
accounts,
primaryAccounts,
cache
);
}
// ============ Helper Functions ============
function _verifyInputs(
Account.Info[] memory accounts,
Actions.ActionArgs[] memory actions
)
private
pure
{
Require.that(
actions.length != 0,
FILE,
"Cannot have zero actions"
);
Require.that(
accounts.length != 0,
FILE,
"Cannot have zero accounts"
);
for (uint256 a = 0; a < accounts.length; a++) {
for (uint256 b = a + 1; b < accounts.length; b++) {
Require.that(
!Account.equals(accounts[a], accounts[b]),
FILE,
"Cannot duplicate accounts",
a,
b
);
}
}
}
function _runPreprocessing(
Storage.State storage state,
Account.Info[] memory accounts,
Actions.ActionArgs[] memory actions
)
private
returns (
bool[] memory,
Cache.MarketCache memory
)
{
uint256 numMarkets = state.numMarkets;
bool[] memory primaryAccounts = new bool[](accounts.length);
Cache.MarketCache memory cache = Cache.create(numMarkets);
// keep track of primary accounts and indexes that need updating
for (uint256 i = 0; i < actions.length; i++) {
Actions.ActionArgs memory arg = actions[i];
Actions.ActionType actionType = arg.actionType;
Actions.MarketLayout marketLayout = Actions.getMarketLayout(actionType);
Actions.AccountLayout accountLayout = Actions.getAccountLayout(actionType);
// parse out primary accounts
if (accountLayout != Actions.AccountLayout.OnePrimary) {
Require.that(
arg.accountId != arg.otherAccountId,
FILE,
"Duplicate accounts in action",
i
);
if (accountLayout == Actions.AccountLayout.TwoPrimary) {
primaryAccounts[arg.otherAccountId] = true;
} else {
assert(accountLayout == Actions.AccountLayout.PrimaryAndSecondary);
Require.that(
!primaryAccounts[arg.otherAccountId],
FILE,
"Requires non-primary account",
arg.otherAccountId
);
}
}
primaryAccounts[arg.accountId] = true;
// keep track of indexes to update
if (marketLayout == Actions.MarketLayout.OneMarket) {
_updateMarket(state, cache, arg.primaryMarketId);
} else if (marketLayout == Actions.MarketLayout.TwoMarkets) {
Require.that(
arg.primaryMarketId != arg.secondaryMarketId,
FILE,
"Duplicate markets in action",
i
);
_updateMarket(state, cache, arg.primaryMarketId);
_updateMarket(state, cache, arg.secondaryMarketId);
} else {
assert(marketLayout == Actions.MarketLayout.ZeroMarkets);
}
}
// get any other markets for which an account has a balance
for (uint256 m = 0; m < numMarkets; m++) {
if (cache.hasMarket(m)) {
continue;
}
for (uint256 a = 0; a < accounts.length; a++) {
if (!state.getPar(accounts[a], m).isZero()) {
_updateMarket(state, cache, m);
break;
}
}
}
return (primaryAccounts, cache);
}
function _updateMarket(
Storage.State storage state,
Cache.MarketCache memory cache,
uint256 marketId
)
private
{
bool updated = cache.addMarket(state, marketId);
if (updated) {
Events.logIndexUpdate(marketId, state.updateIndex(marketId));
}
}
function _runActions(
Storage.State storage state,
Account.Info[] memory accounts,
Actions.ActionArgs[] memory actions,
Cache.MarketCache memory cache
)
private
{
for (uint256 i = 0; i < actions.length; i++) {
Actions.ActionArgs memory action = actions[i];
Actions.ActionType actionType = action.actionType;
if (actionType == Actions.ActionType.Deposit) {
_deposit(state, Actions.parseDepositArgs(accounts, action));
}
else if (actionType == Actions.ActionType.Withdraw) {
_withdraw(state, Actions.parseWithdrawArgs(accounts, action));
}
else if (actionType == Actions.ActionType.Transfer) {
_transfer(state, Actions.parseTransferArgs(accounts, action));
}
else if (actionType == Actions.ActionType.Buy) {
_buy(state, Actions.parseBuyArgs(accounts, action));
}
else if (actionType == Actions.ActionType.Sell) {
_sell(state, Actions.parseSellArgs(accounts, action));
}
else if (actionType == Actions.ActionType.Trade) {
_trade(state, Actions.parseTradeArgs(accounts, action));
}
else if (actionType == Actions.ActionType.Liquidate) {
_liquidate(state, Actions.parseLiquidateArgs(accounts, action), cache);
}
else if (actionType == Actions.ActionType.Vaporize) {
_vaporize(state, Actions.parseVaporizeArgs(accounts, action), cache);
}
else {
assert(actionType == Actions.ActionType.Call);
_call(state, Actions.parseCallArgs(accounts, action));
}
}
}
function _verifyFinalState(
Storage.State storage state,
Account.Info[] memory accounts,
bool[] memory primaryAccounts,
Cache.MarketCache memory cache
)
private
{
// verify no increase in borrowPar for closing markets
uint256 numMarkets = cache.getNumMarkets();
for (uint256 m = 0; m < numMarkets; m++) {
if (cache.getIsClosing(m)) {
Require.that(
state.getTotalPar(m).borrow <= cache.getBorrowPar(m),
FILE,
"Market is closing",
m
);
}
}
// verify account collateralization
for (uint256 a = 0; a < accounts.length; a++) {
Account.Info memory account = accounts[a];
// validate minBorrowedValue
bool collateralized = state.isCollateralized(account, cache, true);
// don't check collateralization for non-primary accounts
if (!primaryAccounts[a]) {
continue;
}
// check collateralization for primary accounts
Require.that(
collateralized,
FILE,
"Undercollateralized account",
account.owner,
account.number
);
// ensure status is normal for primary accounts
if (state.getStatus(account) != Account.Status.Normal) {
state.setStatus(account, Account.Status.Normal);
}
}
}
// ============ Action Functions ============
function _deposit(
Storage.State storage state,
Actions.DepositArgs memory args
)
private
{
state.requireIsOperator(args.account, msg.sender);
Require.that(
args.from == msg.sender || args.from == args.account.owner,
FILE,
"Invalid deposit source",
args.from
);
(
Types.Par memory newPar,
Types.Wei memory deltaWei
) = state.getNewParAndDeltaWei(
args.account,
args.market,
args.amount
);
state.setPar(
args.account,
args.market,
newPar
);
// requires a positive deltaWei
Exchange.transferIn(
state.getToken(args.market),
args.from,
deltaWei
);
Events.logDeposit(
state,
args,
deltaWei
);
}
function _withdraw(
Storage.State storage state,
Actions.WithdrawArgs memory args
)
private
{
state.requireIsOperator(args.account, msg.sender);
(
Types.Par memory newPar,
Types.Wei memory deltaWei
) = state.getNewParAndDeltaWei(
args.account,
args.market,
args.amount
);
state.setPar(
args.account,
args.market,
newPar
);
// requires a negative deltaWei
Exchange.transferOut(
state.getToken(args.market),
args.to,
deltaWei
);
Events.logWithdraw(
state,
args,
deltaWei
);
}
function _transfer(
Storage.State storage state,
Actions.TransferArgs memory args
)
private
{
state.requireIsOperator(args.accountOne, msg.sender);
state.requireIsOperator(args.accountTwo, msg.sender);
(
Types.Par memory newPar,
Types.Wei memory deltaWei
) = state.getNewParAndDeltaWei(
args.accountOne,
args.market,
args.amount
);
state.setPar(
args.accountOne,
args.market,
newPar
);
state.setParFromDeltaWei(
args.accountTwo,
args.market,
deltaWei.negative()
);
Events.logTransfer(
state,
args,
deltaWei
);
}
function _buy(
Storage.State storage state,
Actions.BuyArgs memory args
)
private
{
state.requireIsOperator(args.account, msg.sender);
address takerToken = state.getToken(args.takerMarket);
address makerToken = state.getToken(args.makerMarket);
(
Types.Par memory makerPar,
Types.Wei memory makerWei
) = state.getNewParAndDeltaWei(
args.account,
args.makerMarket,
args.amount
);
Types.Wei memory takerWei = Exchange.getCost(
args.exchangeWrapper,
makerToken,
takerToken,
makerWei,
args.orderData
);
Types.Wei memory tokensReceived = Exchange.exchange(
args.exchangeWrapper,
args.account.owner,
makerToken,
takerToken,
takerWei,
args.orderData
);
Require.that(
tokensReceived.value >= makerWei.value,
FILE,
"Buy amount less than promised",
tokensReceived.value,
makerWei.value
);
state.setPar(
args.account,
args.makerMarket,
makerPar
);
state.setParFromDeltaWei(
args.account,
args.takerMarket,
takerWei
);
Events.logBuy(
state,
args,
takerWei,
makerWei
);
}
function _sell(
Storage.State storage state,
Actions.SellArgs memory args
)
private
{
state.requireIsOperator(args.account, msg.sender);
address takerToken = state.getToken(args.takerMarket);
address makerToken = state.getToken(args.makerMarket);
(
Types.Par memory takerPar,
Types.Wei memory takerWei
) = state.getNewParAndDeltaWei(
args.account,
args.takerMarket,
args.amount
);
Types.Wei memory makerWei = Exchange.exchange(
args.exchangeWrapper,
args.account.owner,
makerToken,
takerToken,
takerWei,
args.orderData
);
state.setPar(
args.account,
args.takerMarket,
takerPar
);
state.setParFromDeltaWei(
args.account,
args.makerMarket,
makerWei
);
Events.logSell(
state,
args,
takerWei,
makerWei
);
}
function _trade(
Storage.State storage state,
Actions.TradeArgs memory args
)
private
{
state.requireIsOperator(args.takerAccount, msg.sender);
state.requireIsOperator(args.makerAccount, args.autoTrader);
Types.Par memory oldInputPar = state.getPar(
args.makerAccount,
args.inputMarket
);
(
Types.Par memory newInputPar,
Types.Wei memory inputWei
) = state.getNewParAndDeltaWei(
args.makerAccount,
args.inputMarket,
args.amount
);
Types.AssetAmount memory outputAmount = IAutoTrader(args.autoTrader).getTradeCost(
args.inputMarket,
args.outputMarket,
args.makerAccount,
args.takerAccount,
oldInputPar,
newInputPar,
inputWei,
args.tradeData
);
(
Types.Par memory newOutputPar,
Types.Wei memory outputWei
) = state.getNewParAndDeltaWei(
args.makerAccount,
args.outputMarket,
outputAmount
);
Require.that(
outputWei.isZero() || inputWei.isZero() || outputWei.sign != inputWei.sign,
FILE,
"Trades cannot be one-sided"
);
// set the balance for the maker
state.setPar(
args.makerAccount,
args.inputMarket,
newInputPar
);
state.setPar(
args.makerAccount,
args.outputMarket,
newOutputPar
);
// set the balance for the taker
state.setParFromDeltaWei(
args.takerAccount,
args.inputMarket,
inputWei.negative()
);
state.setParFromDeltaWei(
args.takerAccount,
args.outputMarket,
outputWei.negative()
);
Events.logTrade(
state,
args,
inputWei,
outputWei
);
}
function _liquidate(
Storage.State storage state,
Actions.LiquidateArgs memory args,
Cache.MarketCache memory cache
)
private
{
state.requireIsOperator(args.solidAccount, msg.sender);
// verify liquidatable
if (Account.Status.Liquid != state.getStatus(args.liquidAccount)) {
Require.that(
!state.isCollateralized(args.liquidAccount, cache, /* requireMinBorrow = */ false),
FILE,
"Unliquidatable account",
args.liquidAccount.owner,
args.liquidAccount.number
);
state.setStatus(args.liquidAccount, Account.Status.Liquid);
}
Types.Wei memory maxHeldWei = state.getWei(
args.liquidAccount,
args.heldMarket
);
Require.that(
!maxHeldWei.isNegative(),
FILE,
"Collateral cannot be negative",
args.liquidAccount.owner,
args.liquidAccount.number,
args.heldMarket
);
(
Types.Par memory owedPar,
Types.Wei memory owedWei
) = state.getNewParAndDeltaWeiForLiquidation(
args.liquidAccount,
args.owedMarket,
args.amount
);
(
Monetary.Price memory heldPrice,
Monetary.Price memory owedPrice
) = _getLiquidationPrices(
state,
cache,
args.heldMarket,
args.owedMarket
);
Types.Wei memory heldWei = _owedWeiToHeldWei(owedWei, heldPrice, owedPrice);
// if attempting to over-borrow the held asset, bound it by the maximum
if (heldWei.value > maxHeldWei.value) {
heldWei = maxHeldWei.negative();
owedWei = _heldWeiToOwedWei(heldWei, heldPrice, owedPrice);
state.setPar(
args.liquidAccount,
args.heldMarket,
Types.zeroPar()
);
state.setParFromDeltaWei(
args.liquidAccount,
args.owedMarket,
owedWei
);
} else {
state.setPar(
args.liquidAccount,
args.owedMarket,
owedPar
);
state.setParFromDeltaWei(
args.liquidAccount,
args.heldMarket,
heldWei
);
}
// set the balances for the solid account
state.setParFromDeltaWei(
args.solidAccount,
args.owedMarket,
owedWei.negative()
);
state.setParFromDeltaWei(
args.solidAccount,
args.heldMarket,
heldWei.negative()
);
Events.logLiquidate(
state,
args,
heldWei,
owedWei
);
}
function _vaporize(
Storage.State storage state,
Actions.VaporizeArgs memory args,
Cache.MarketCache memory cache
)
private
{
state.requireIsOperator(args.solidAccount, msg.sender);
// verify vaporizable
if (Account.Status.Vapor != state.getStatus(args.vaporAccount)) {
Require.that(
state.isVaporizable(args.vaporAccount, cache),
FILE,
"Unvaporizable account",
args.vaporAccount.owner,
args.vaporAccount.number
);
state.setStatus(args.vaporAccount, Account.Status.Vapor);
}
// First, attempt to refund using the same token
(
bool fullyRepaid,
Types.Wei memory excessWei
) = _vaporizeUsingExcess(state, args);
if (fullyRepaid) {
Events.logVaporize(
state,
args,
Types.zeroWei(),
Types.zeroWei(),
excessWei
);
return;
}
Types.Wei memory maxHeldWei = state.getNumExcessTokens(args.heldMarket);
Require.that(
!maxHeldWei.isNegative(),
FILE,
"Excess cannot be negative",
args.heldMarket
);
(
Types.Par memory owedPar,
Types.Wei memory owedWei
) = state.getNewParAndDeltaWeiForLiquidation(
args.vaporAccount,
args.owedMarket,
args.amount
);
(
Monetary.Price memory heldPrice,
Monetary.Price memory owedPrice
) = _getLiquidationPrices(
state,
cache,
args.heldMarket,
args.owedMarket
);
Types.Wei memory heldWei = _owedWeiToHeldWei(owedWei, heldPrice, owedPrice);
// if attempting to over-borrow the held asset, bound it by the maximum
if (heldWei.value > maxHeldWei.value) {
heldWei = maxHeldWei.negative();
owedWei = _heldWeiToOwedWei(heldWei, heldPrice, owedPrice);
state.setParFromDeltaWei(
args.vaporAccount,
args.owedMarket,
owedWei
);
} else {
state.setPar(
args.vaporAccount,
args.owedMarket,
owedPar
);
}
// set the balances for the solid account
state.setParFromDeltaWei(
args.solidAccount,
args.owedMarket,
owedWei.negative()
);
state.setParFromDeltaWei(
args.solidAccount,
args.heldMarket,
heldWei.negative()
);
Events.logVaporize(
state,
args,
heldWei,
owedWei,
excessWei
);
}
function _call(
Storage.State storage state,
Actions.CallArgs memory args
)
private
{
state.requireIsOperator(args.account, msg.sender);
ICallee(args.callee).callFunction(
msg.sender,
args.account,
args.data
);
Events.logCall(args);
}
// ============ Private Functions ============
/**
* For the purposes of liquidation or vaporization, get the value-equivalent amount of heldWei
* given owedWei and the (spread-adjusted) prices of each asset.
*/
function _owedWeiToHeldWei(
Types.Wei memory owedWei,
Monetary.Price memory heldPrice,
Monetary.Price memory owedPrice
)
private
pure
returns (Types.Wei memory)
{
return Types.Wei({
sign: false,
value: Math.getPartial(owedWei.value, owedPrice.value, heldPrice.value)
});
}
/**
* For the purposes of liquidation or vaporization, get the value-equivalent amount of owedWei
* given heldWei and the (spread-adjusted) prices of each asset.
*/
function _heldWeiToOwedWei(
Types.Wei memory heldWei,
Monetary.Price memory heldPrice,
Monetary.Price memory owedPrice
)
private
pure
returns (Types.Wei memory)
{
return Types.Wei({
sign: true,
value: Math.getPartialRoundUp(heldWei.value, heldPrice.value, owedPrice.value)
});
}
/**
* Attempt to vaporize an account's balance using the excess tokens in the protocol. Return a
* bool and a wei value. The boolean is true if and only if the balance was fully vaporized. The
* Wei value is how many excess tokens were used to partially or fully vaporize the account's
* negative balance.
*/
function _vaporizeUsingExcess(
Storage.State storage state,
Actions.VaporizeArgs memory args
)
internal
returns (bool, Types.Wei memory)
{
Types.Wei memory excessWei = state.getNumExcessTokens(args.owedMarket);
// There are no excess funds, return zero
if (!excessWei.isPositive()) {
return (false, Types.zeroWei());
}
Types.Wei memory maxRefundWei = state.getWei(args.vaporAccount, args.owedMarket);
maxRefundWei.sign = true;
// The account is fully vaporizable using excess funds
if (excessWei.value >= maxRefundWei.value) {
state.setPar(
args.vaporAccount,
args.owedMarket,
Types.zeroPar()
);
return (true, maxRefundWei);
}
// The account is only partially vaporizable using excess funds
else {
state.setParFromDeltaWei(
args.vaporAccount,
args.owedMarket,
excessWei
);
return (false, excessWei);
}
}
/**
* Return the (spread-adjusted) prices of two assets for the purposes of liquidation or
* vaporization.
*/
function _getLiquidationPrices(
Storage.State storage state,
Cache.MarketCache memory cache,
uint256 heldMarketId,
uint256 owedMarketId
)
internal
view
returns (
Monetary.Price memory,
Monetary.Price memory
)
{
uint256 originalPrice = cache.getPrice(owedMarketId).value;
Decimal.D256 memory spread = state.getLiquidationSpreadForPair(
heldMarketId,
owedMarketId
);
Monetary.Price memory owedPrice = Monetary.Price({
value: originalPrice.add(Decimal.mul(originalPrice, spread))
});
return (cache.getPrice(heldMarketId), owedPrice);
}
}