-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathDriipSettlementByTrade.sol
386 lines (341 loc) · 17 KB
/
DriipSettlementByTrade.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
/*
* Hubii Nahmii
*
* Compliant with the Hubii Nahmii specification v0.12.
*
* Copyright (C) 2017-2019 Hubii AS
*/
pragma solidity >=0.4.25 <0.6.0;
pragma experimental ABIEncoderV2;
import {Ownable} from "./Ownable.sol";
import {Configurable} from "./Configurable.sol";
import {ValidatableV2} from "./ValidatableV2.sol";
import {ClientFundable} from "./ClientFundable.sol";
import {CommunityVotable} from "./CommunityVotable.sol";
import {FraudChallengable} from "./FraudChallengable.sol";
import {WalletLockable} from "./WalletLockable.sol";
import {RevenueFund} from "./RevenueFund.sol";
import {PartnerFund} from "./PartnerFund.sol";
import {DriipSettlementChallengeState} from "./DriipSettlementChallengeState.sol";
import {DriipSettlementState} from "./DriipSettlementState.sol";
import {Beneficiary} from "./Beneficiary.sol";
import {SafeMathIntLib} from "./SafeMathIntLib.sol";
import {SafeMathUintLib} from "./SafeMathUintLib.sol";
import {MonetaryTypesLib} from "./MonetaryTypesLib.sol";
import {NahmiiTypesLib} from "./NahmiiTypesLib.sol";
import {TradeTypesLib} from "./TradeTypesLib.sol";
import {DriipSettlementTypesLib} from "./DriipSettlementTypesLib.sol";
import {SettlementChallengeTypesLib} from "./SettlementChallengeTypesLib.sol";
/**
* @title DriipSettlementByTrade
* @notice Where driip settlements pertaining to trade are finalized
*/
contract DriipSettlementByTrade is Ownable, Configurable, ValidatableV2, ClientFundable, CommunityVotable,
FraudChallengable, WalletLockable {
using SafeMathIntLib for int256;
using SafeMathUintLib for uint256;
//
// Variables
// -----------------------------------------------------------------------------------------------------------------
DriipSettlementChallengeState public driipSettlementChallengeState;
DriipSettlementState public driipSettlementState;
RevenueFund public revenueFund;
PartnerFund public partnerFund;
//
// Events
// -----------------------------------------------------------------------------------------------------------------
event SettleTradeEvent(address wallet, TradeTypesLib.Trade trade, string standard);
event SettleTradeByProxyEvent(address proxy, address wallet, TradeTypesLib.Trade trade, string standard);
event SetDriipSettlementChallengeStateEvent(DriipSettlementChallengeState oldDriipSettlementChallengeState,
DriipSettlementChallengeState newDriipSettlementChallengeState);
event SetDriipSettlementStateEvent(DriipSettlementState oldDriipSettlementState,
DriipSettlementState newDriipSettlementState);
event SetRevenueFundEvent(RevenueFund oldRevenueFund, RevenueFund newRevenueFund);
event SetPartnerFundEvent(PartnerFund oldPartnerFund, PartnerFund newPartnerFund);
event StageFeesEvent(address wallet, int256 deltaAmount, int256 cumulativeAmount,
address currencyCt, uint256 currencyId);
//
// Constructor
// -----------------------------------------------------------------------------------------------------------------
constructor(address deployer) Ownable(deployer) public {
}
//
// Functions
// -----------------------------------------------------------------------------------------------------------------
/// @notice Set the driip settlement challenge state contract
/// @param newDriipSettlementChallengeState The (address of) DriipSettlementChallengeState contract instance
function setDriipSettlementChallengeState(DriipSettlementChallengeState newDriipSettlementChallengeState)
public
onlyDeployer
notNullAddress(address(newDriipSettlementChallengeState))
{
DriipSettlementChallengeState oldDriipSettlementChallengeState = driipSettlementChallengeState;
driipSettlementChallengeState = newDriipSettlementChallengeState;
emit SetDriipSettlementChallengeStateEvent(oldDriipSettlementChallengeState, driipSettlementChallengeState);
}
/// @notice Set the driip settlement state contract
/// @param newDriipSettlementState The (address of) DriipSettlementState contract instance
function setDriipSettlementState(DriipSettlementState newDriipSettlementState)
public
onlyDeployer
notNullAddress(address(newDriipSettlementState))
{
DriipSettlementState oldDriipSettlementState = driipSettlementState;
driipSettlementState = newDriipSettlementState;
emit SetDriipSettlementStateEvent(oldDriipSettlementState, driipSettlementState);
}
/// @notice Set the revenue fund contract
/// @param newRevenueFund The (address of) RevenueFund contract instance
function setRevenueFund(RevenueFund newRevenueFund)
public
onlyDeployer
notNullAddress(address(newRevenueFund))
{
RevenueFund oldRevenueFund = revenueFund;
revenueFund = newRevenueFund;
emit SetRevenueFundEvent(oldRevenueFund, revenueFund);
}
/// @notice Set the partner fund contract
/// @param newPartnerFund The (address of) partner contract instance
function setPartnerFund(PartnerFund newPartnerFund)
public
onlyDeployer
notNullAddress(address(newPartnerFund))
{
PartnerFund oldPartnerFund = partnerFund;
partnerFund = newPartnerFund;
emit SetPartnerFundEvent(oldPartnerFund, partnerFund);
}
/// @notice Get the count of settlements
function settlementsCount()
public
view
returns (uint256)
{
return driipSettlementState.settlementsCount();
}
/// @notice Get the count of settlements for given wallet
/// @param wallet The address for which to return settlement count
/// @return count of settlements for the provided wallet
function settlementsCountByWallet(address wallet)
public
view
returns (uint256)
{
return driipSettlementState.settlementsCountByWallet(wallet);
}
/// @notice Get settlement of given wallet and index
/// @param wallet The address for which to return settlement
/// @param index The wallet's settlement index
/// @return settlement for the provided wallet and index
function settlementByWalletAndIndex(address wallet, uint256 index)
public
view
returns (DriipSettlementTypesLib.Settlement memory)
{
return driipSettlementState.settlementByWalletAndIndex(wallet, index);
}
/// @notice Get settlement of given wallet and wallet nonce
/// @param wallet The address for which to return settlement
/// @param nonce The wallet's nonce
/// @return settlement for the provided wallet and index
function settlementByWalletAndNonce(address wallet, uint256 nonce)
public
view
returns (DriipSettlementTypesLib.Settlement memory)
{
return driipSettlementState.settlementByWalletAndNonce(wallet, nonce);
}
/// @notice Settle driip that is a trade
/// @param trade The trade to be settled
/// @param standard The standard of the token to be settled (discarded if settling ETH)
function settleTrade(TradeTypesLib.Trade memory trade, string memory standard)
public
{
// Settle trade
_settleTrade(msg.sender, trade, standard);
// Emit event
emit SettleTradeEvent(msg.sender, trade, standard);
}
/// @notice Settle driip that is a trade
/// @param wallet The wallet whose side of the trade is to be settled
/// @param trade The trade to be settled
/// @param standard The standard of the token to be settled (discarded if settling ETH)
function settleTradeByProxy(address wallet, TradeTypesLib.Trade memory trade, string memory standard)
public
onlyOperator
{
// Settle trade for wallet
_settleTrade(wallet, trade, standard);
// Emit event
emit SettleTradeByProxyEvent(msg.sender, wallet, trade, standard);
}
//
// Private functions
// -----------------------------------------------------------------------------------------------------------------
function _settleTrade(address wallet, TradeTypesLib.Trade memory trade, string memory standard)
private
onlySealedTrade(trade)
onlyTradeParty(trade, wallet)
{
require(
!fraudChallenge.isFraudulentTradeHash(trade.seal.hash),
"Trade deemed fraudulent [DriipSettlementByTrade.sol:200]"
);
require(
!communityVote.isDoubleSpenderWallet(wallet),
"Wallet deemed double spender [DriipSettlementByTrade.sol:204]"
);
// Require that wallet is not locked
require(!walletLocker.isLocked(wallet), "Wallet found locked [DriipSettlementByTrade.sol:210]");
// Require that the wallet's current driip settlement challenge proposals are defined wrt this trade
require(
trade.seal.hash == driipSettlementChallengeState.proposalChallengedHash(wallet, trade.currencies.intended),
"Trade not challenged in intended currency [DriipSettlementByTrade.sol:213]"
);
require(
trade.seal.hash == driipSettlementChallengeState.proposalChallengedHash(wallet, trade.currencies.conjugate),
"Trade not challenged in conjugate currency [DriipSettlementByTrade.sol:217]"
);
// Extract properties depending on settlement role
(
DriipSettlementTypesLib.SettlementRole settlementRole,
TradeTypesLib.TradeParty memory party
) = _getRoleProperties(trade, wallet);
// Require that driip settlement challenge proposals have been initiated
require(
driipSettlementChallengeState.hasProposal(wallet, party.nonce, trade.currencies.intended),
"No intended proposal found [DriipSettlementByTrade.sol:229]"
);
require(
driipSettlementChallengeState.hasProposal(wallet, party.nonce, trade.currencies.conjugate),
"No conjugate proposal found [DriipSettlementByTrade.sol:233]"
);
// Require that driip settlement challenge proposal have not been terminated already
require(
!driipSettlementChallengeState.hasProposalTerminated(wallet, trade.currencies.intended),
"Intended proposal found terminated [DriipSettlementByTrade.sol:239]"
);
require(
!driipSettlementChallengeState.hasProposalTerminated(wallet, trade.currencies.conjugate),
"Conjugate proposal found terminated [DriipSettlementByTrade.sol:243]"
);
// Require that driip settlement challenge proposals have expired
require(
driipSettlementChallengeState.hasProposalExpired(wallet, trade.currencies.intended),
"Intended proposal found not expired [DriipSettlementByTrade.sol:249]"
);
require(
driipSettlementChallengeState.hasProposalExpired(wallet, trade.currencies.conjugate),
"Conjugate proposal found not expired [DriipSettlementByTrade.sol:253]"
);
// Require that driip settlement challenge proposals qualified
require(
SettlementChallengeTypesLib.Status.Qualified == driipSettlementChallengeState.proposalStatus(wallet, trade.currencies.intended),
"Intended proposal found not qualified [DriipSettlementByTrade.sol:259]"
);
require(
SettlementChallengeTypesLib.Status.Qualified == driipSettlementChallengeState.proposalStatus(wallet, trade.currencies.conjugate),
"Conjugate proposal found not qualified [DriipSettlementByTrade.sol:263]"
);
// Require that operational mode is normal and data is available
require(configuration.isOperationalModeNormal(), "Not normal operational mode [DriipSettlementByTrade.sol:269]");
require(communityVote.isDataAvailable(), "Data not available [DriipSettlementByTrade.sol:270]");
// Init settlement, i.e. create one if no such settlement exists for the double pair of wallets and nonces
driipSettlementState.initSettlement(
TradeTypesLib.TRADE_KIND(), trade.seal.hash,
trade.seller.wallet, trade.seller.nonce,
trade.buyer.wallet, trade.buyer.nonce
);
// If exists settlement of nonce then require that wallet has not already settled
require(
!driipSettlementState.isSettlementPartyDone(wallet, party.nonce, settlementRole),
"Settlement party already done [DriipSettlementByTrade.sol:280]"
);
// Set wallet's completion of this trade to prevent the same settlement from being resettled by this wallet
driipSettlementState.completeSettlement(
wallet, party.nonce, settlementRole, true
);
// If wallet has previously settled balance of the intended currency with higher driip nonce, then don't
// settle its balance again
if (driipSettlementState.maxNonceByWalletAndCurrency(wallet, trade.currencies.intended) < party.nonce) {
// Update settled nonce of wallet and currency
driipSettlementState.setMaxNonceByWalletAndCurrency(wallet, trade.currencies.intended, party.nonce);
// Update settled balance
clientFund.updateSettledBalance(
wallet, party.balances.intended.current, trade.currencies.intended.ct,
trade.currencies.intended.id, standard, block.number
);
// Stage (stage function assures positive amount only)
clientFund.stage(
wallet,
driipSettlementChallengeState.proposalStageAmount(wallet, trade.currencies.intended),
trade.currencies.intended.ct, trade.currencies.intended.id, standard
);
}
// If wallet has previously settled balance of the conjugate currency with higher driip nonce, then don't
// settle its balance again
if (driipSettlementState.maxNonceByWalletAndCurrency(wallet, trade.currencies.conjugate) < party.nonce) {
// Update settled nonce of wallet and currency
driipSettlementState.setMaxNonceByWalletAndCurrency(wallet, trade.currencies.conjugate, party.nonce);
// Update settled balance
clientFund.updateSettledBalance(
wallet, party.balances.conjugate.current, trade.currencies.conjugate.ct,
trade.currencies.conjugate.id, standard, block.number
);
// Stage (stage function assures positive amount only)
clientFund.stage(
wallet,
driipSettlementChallengeState.proposalStageAmount(wallet, trade.currencies.conjugate),
trade.currencies.conjugate.ct, trade.currencies.conjugate.id, standard
);
}
// Stage fees to revenue fund
if (address(0) != address(revenueFund))
_stageFees(wallet, party.fees.total, revenueFund, party.nonce, standard);
// Remove driip settlement challenge proposals
driipSettlementChallengeState.terminateProposal(wallet, trade.currencies.intended, false);
driipSettlementChallengeState.terminateProposal(wallet, trade.currencies.conjugate, false);
}
function _getRoleProperties(TradeTypesLib.Trade memory trade, address wallet)
private
view
returns (
DriipSettlementTypesLib.SettlementRole settlementRole,
TradeTypesLib.TradeParty memory party
)
{
if (validator.isTradeSeller(trade, wallet)) {
settlementRole = DriipSettlementTypesLib.SettlementRole.Origin;
party = trade.seller;
} else {
settlementRole = DriipSettlementTypesLib.SettlementRole.Target;
party = trade.buyer;
}
}
function _stageFees(address wallet, NahmiiTypesLib.OriginFigure[] memory fees,
Beneficiary protocolBeneficiary, uint256 nonce, string memory standard)
private
{
// For each origin figure...
for (uint256 i = 0; i < fees.length; i++) {
// Based on originId determine if this is protocol or partner fee, and if the latter define originId as destination in beneficiary
(Beneficiary beneficiary, address destination) =
(0 == fees[i].originId) ? (protocolBeneficiary, address(0)) : (partnerFund, address(fees[i].originId));
if (driipSettlementState.totalFee(wallet, beneficiary, destination, fees[i].figure.currency).nonce < nonce) {
// Get the amount previously staged
int256 deltaAmount = fees[i].figure.amount.sub(driipSettlementState.totalFee(wallet, beneficiary, destination, fees[i].figure.currency).amount);
// Update fee total
driipSettlementState.setTotalFee(wallet, beneficiary, destination, fees[i].figure.currency, MonetaryTypesLib.NoncedAmount(nonce, fees[i].figure.amount));
// Transfer to beneficiary
clientFund.transferToBeneficiary(
destination, beneficiary, deltaAmount, fees[i].figure.currency.ct, fees[i].figure.currency.id, standard
);
// Emit event
emit StageFeesEvent(
wallet, deltaAmount, fees[i].figure.amount, fees[i].figure.currency.ct, fees[i].figure.currency.id
);
}
}
}
}