-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathattributes.h
More file actions
568 lines (485 loc) · 17 KB
/
Copy pathattributes.h
File metadata and controls
568 lines (485 loc) · 17 KB
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
// Copyright (c) 2020 The DeFi Foundation
// Distributed under the MIT software license, see the accompanying
// file LICENSE or http://www.opensource.org/licenses/mit-license.php.
#ifndef DEFI_DFI_GOVVARIABLES_ATTRIBUTES_H
#define DEFI_DFI_GOVVARIABLES_ATTRIBUTES_H
#include <amount.h>
#include <dfi/balances.h>
#include <dfi/evm.h>
#include <dfi/gv.h>
#include <dfi/oracles.h>
namespace Consensus {
struct Params;
}
enum VersionTypes : uint8_t {
v0 = 0,
};
enum AttributeTypes : uint8_t {
Live = 'l',
Oracles = 'o',
Param = 'a',
Token = 't',
Poolpairs = 'p',
Locks = 'L',
Governance = 'g',
Transfer = 'b',
EVMType = 'e',
Vaults = 'v',
Rules = 'r',
};
enum ParamIDs : uint8_t {
DFIP2201 = 'a',
DFIP2203 = 'b',
TokenID = 'c',
Economy = 'e',
DFIP2206A = 'f',
DFIP2206F = 'g',
Feature = 'h',
Auction = 'i',
Foundation = 'j',
DFIP2211F = 'k',
BlockTime = 'l',
};
enum OracleIDs : uint8_t {
Splits = 'a',
};
enum EVMIDs : uint8_t {
Block = 'a',
};
enum EVMKeys : uint8_t {
Finalized = 'a',
GasLimit = 'b',
GasTargetFactor = 'c',
RbfIncrementMinPct = 'd',
};
enum GovernanceIDs : uint8_t {
Global = 'a',
Proposals = 'b',
};
enum TransferIDs : uint8_t {
DVMToEVM = 'a',
EVMToDVM = 'b',
};
enum VaultIDs : uint8_t {
DUSDVault = 'a',
Parameters = 'b',
};
enum RulesIDs : uint8_t {
TXRules = 'a',
};
enum EconomyKeys : uint8_t {
PaybackDFITokens = 'a',
PaybackTokens = 'b',
DFIP2203Current = 'c',
DFIP2203Burned = 'd',
DFIP2203Minted = 'e',
DFIP2206FCurrent = 'f',
DFIP2206FBurned = 'g',
DFIP2206FMinted = 'h',
DexTokens = 'i',
NegativeInt = 'j',
NegativeIntCurrent = 'k',
BatchRoundingExcess = 'n', // Extra added to loan amounts on auction creation due to round errors.
ConsolidatedInterest = 'o', // Amount added to loan amounts after auction with no bids.
PaybackDFITokensPrincipal = 'p', // Same as PaybackDFITokens but without interest.
Loans = 'q',
TransferDomainStatsLive = 'r',
EVMBlockStatsLive = 's',
};
enum DFIPKeys : uint8_t {
Active = 'a',
Premium = 'b',
MinSwap = 'c',
RewardPct = 'd',
BlockPeriod = 'e',
DUSDInterestBurn = 'g',
DUSDLoanBurn = 'h',
StartBlock = 'i',
GovUnset = 'j',
GovFoundation = 'k',
MNSetRewardAddress = 'l',
MNSetOperatorAddress = 'm',
MNSetOwnerAddress = 'n',
Members = 'p',
GovernanceEnabled = 'q',
CFPPayout = 'r',
EmissionUnusedFund = 's',
MintTokens = 't',
EVMEnabled = 'u',
ICXEnabled = 'v',
TransferDomain = 'w',
LiquidityCalcSamplingPeriod = 'x',
AverageLiquidityPercentage = 'y',
EmissionReduction = 'z',
TargetSpacing = 'A',
TargetTimespam = 'B',
};
enum GovernanceKeys : uint8_t {
FeeRedistribution = 'a',
FeeBurnPct = 'b',
CFPFee = 'd',
CFPApprovalThreshold = 'e',
VOCFee = 'f',
VOCEmergencyFee = 'g',
VOCEmergencyPeriod = 'h',
VOCApprovalThreshold = 'i',
Quorum = 'j',
VotingPeriod = 'k',
VOCEmergencyQuorum = 'l',
CFPMaxCycles = 'm',
};
enum TokenKeys : uint8_t {
PaybackDFI = 'a',
PaybackDFIFeePCT = 'b',
LoanPayback = 'c',
LoanPaybackFeePCT = 'd',
DexInFeePct = 'e',
DexOutFeePct = 'f',
DFIP2203Enabled = 'g',
FixedIntervalPriceId = 'h',
LoanCollateralEnabled = 'i',
LoanCollateralFactor = 'j',
LoanMintingEnabled = 'k',
LoanMintingInterest = 'l',
Ascendant = 'm',
Descendant = 'n',
Epitaph = 'o',
LoanPaybackCollateral = 'p',
};
enum PoolKeys : uint8_t {
TokenAFeePCT = 'a',
TokenBFeePCT = 'b',
TokenAFeeDir = 'c',
TokenBFeeDir = 'd',
};
enum TransferKeys : uint8_t {
TransferEnabled = 'a',
SrcFormats = 'b',
DestFormats = 'c',
AuthFormats = 'd',
NativeEnabled = 'e',
DATEnabled = 'f',
Disallowed = 'g',
};
enum VaultKeys : uint8_t {
CreationFee = 'a',
DUSDVaultEnabled = 'w',
};
enum OracleKeys : uint8_t {
FractionalSplits = 0,
};
enum RulesKeys : uint8_t {
CoreOPReturn = 'a',
DVMOPReturn = 'b',
EVMOPReturn = 'c',
};
struct CDataStructureV0 {
uint8_t type;
uint32_t typeId;
uint32_t key;
uint32_t keyId;
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream &s, Operation ser_action) {
READWRITE(type);
READWRITE(typeId);
READWRITE(VARINT(key));
if (IsExtendedSize()) {
READWRITE(keyId);
} else {
keyId = 0;
}
}
bool IsExtendedSize() const {
return type == AttributeTypes::Token && (key == TokenKeys::LoanPayback || key == TokenKeys::LoanPaybackFeePCT);
}
bool operator<(const CDataStructureV0 &o) const {
return std::tie(type, typeId, key, keyId) < std::tie(o.type, o.typeId, o.key, o.keyId);
}
};
// for future use
struct CDataStructureV1 {
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream &s, Operation ser_action) {}
bool operator<(const CDataStructureV1 &o) const { return false; }
};
struct CTokenPayback {
CBalances tokensFee;
CBalances tokensPayback;
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream &s, Operation ser_action) {
READWRITE(tokensFee);
READWRITE(tokensPayback);
}
};
struct CFeeDir {
uint8_t feeDir;
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream &s, Operation ser_action) {
READWRITE(feeDir);
}
};
ResVal<CScript> GetFutureSwapContractAddress(const std::string &contract);
struct CDexTokenInfo {
struct CTokenInfo {
uint64_t swaps;
uint64_t feeburn;
uint64_t commissions;
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream &s, Operation ser_action) {
READWRITE(swaps);
READWRITE(feeburn);
READWRITE(commissions);
}
};
CTokenInfo totalTokenA;
CTokenInfo totalTokenB;
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream &s, Operation ser_action) {
READWRITE(totalTokenA);
READWRITE(totalTokenB);
}
};
enum FeeDirValues : uint8_t { Both, In, Out };
struct CTransferDomainStatsLive {
CStatsTokenBalances dvmEvmTotal;
CStatsTokenBalances evmDvmTotal;
CStatsTokenBalances dvmIn;
CStatsTokenBalances evmIn;
CStatsTokenBalances dvmOut;
CStatsTokenBalances evmOut;
CStatsTokenBalances dvmCurrent;
CStatsTokenBalances evmCurrent;
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream &s, Operation ser_action) {
READWRITE(dvmEvmTotal);
READWRITE(evmDvmTotal);
READWRITE(dvmIn);
READWRITE(evmIn);
READWRITE(dvmOut);
READWRITE(evmOut);
READWRITE(dvmCurrent);
READWRITE(evmCurrent);
}
static constexpr CDataStructureV0 Key = {AttributeTypes::Live,
ParamIDs::Economy,
EconomyKeys::TransferDomainStatsLive};
};
enum XVmAddressFormatTypes : uint8_t {
None,
Bech32,
Bech32ProxyErc55,
PkHash,
PkHashProxyErc55,
Erc55,
};
struct CEvmBlockStatsLive {
CAmount feeBurnt;
CAmount feeBurntMin = std::numeric_limits<CAmount>::max();
uint256 feeBurntMinHash;
CAmount feeBurntMax = std::numeric_limits<CAmount>::min();
uint256 feeBurntMaxHash;
CAmount feePriority;
CAmount feePriorityMin = std::numeric_limits<CAmount>::max();
uint256 feePriorityMinHash;
CAmount feePriorityMax = std::numeric_limits<CAmount>::min();
uint256 feePriorityMaxHash;
ADD_SERIALIZE_METHODS;
template <typename Stream, typename Operation>
inline void SerializationOp(Stream &s, Operation ser_action) {
READWRITE(feeBurnt);
READWRITE(feeBurntMin);
READWRITE(feeBurntMinHash);
READWRITE(feeBurntMax);
READWRITE(feeBurntMaxHash);
READWRITE(feePriority);
READWRITE(feePriorityMin);
READWRITE(feePriorityMinHash);
READWRITE(feePriorityMax);
READWRITE(feePriorityMaxHash);
}
static constexpr CDataStructureV0 Key = {AttributeTypes::Live, ParamIDs::Economy, EconomyKeys::EVMBlockStatsLive};
};
using CDexBalances = std::map<DCT_ID, CDexTokenInfo>;
using OracleSplits = std::map<uint32_t, int32_t>;
using OracleSplits64 = std::map<uint32_t, CAmount>;
using DescendantValue = std::pair<uint32_t, int32_t>;
using AscendantValue = std::pair<uint32_t, std::string>;
using CAttributeType = std::variant<CDataStructureV0, CDataStructureV1>;
// Unused legacy types but can be changed and update for future use.
// Required for sync to maintain consistent variant indexing.
using LegacyEntry1 = std::map<std::string, std::string>;
using LegacyEntry2 = std::map<std::string, uint64_t>;
using LegacyEntry3 = std::map<std::string, int64_t>;
using CAttributeValue = std::variant<bool,
CAmount,
CBalances,
CTokenPayback,
CTokenCurrencyPair,
OracleSplits,
DescendantValue,
AscendantValue,
CFeeDir,
CDexBalances,
std::set<CScript>,
std::set<std::string>,
LegacyEntry1,
LegacyEntry2,
LegacyEntry3,
int32_t,
uint32_t,
uint64_t,
XVmAddressFormatItems,
CTransferDomainStatsLive,
CEvmBlockStatsLive,
OracleSplits64>;
void TrackNegativeInterest(CCustomCSView &mnview, const CTokenAmount &amount);
void TrackLiveBalances(CCustomCSView &mnview, const CBalances &balances, const uint8_t key);
void TrackDUSDAdd(CCustomCSView &mnview, const CTokenAmount &amount);
void TrackDUSDSub(CCustomCSView &mnview, const CTokenAmount &amount);
bool IsEVMEnabled(const std::shared_ptr<ATTRIBUTES> attributes);
bool IsEVMEnabled(const CCustomCSView &view);
Res StoreGovVars(const CGovernanceHeightMessage &obj, CCustomCSView &view);
int64_t GetTargetSpacing(const CCustomCSView &view);
int64_t GetTargetTimespan(const CCustomCSView &view);
int64_t DifficultyAdjustment(const CCustomCSView &view);
int32_t GetEmissionReduction(const CCustomCSView &view);
uint32_t BlocksPerDay(const CCustomCSView &view);
uint32_t BlocksCollateralizationRatioCalculation(const CCustomCSView &view);
uint32_t BlocksCollateralAuction(const CCustomCSView &view);
enum GovVarsFilter {
All,
NoAttributes,
AttributesOnly,
PrefixedAttributes,
LiveAttributes,
Version2Dot7,
};
class ATTRIBUTES : public GovVariable, public AutoRegistrator<GovVariable, ATTRIBUTES> {
public:
virtual ~ATTRIBUTES() override {}
std::string GetName() const override { return TypeName(); }
bool IsEmpty() const override;
Res Import(const UniValue &val) override;
UniValue Export() const override;
UniValue ExportFiltered(GovVarsFilter filter, const std::string &prefix, CCustomCSView *view = nullptr) const;
Res CheckKeys() const;
Res Validate(const CCustomCSView &mnview) const override;
Res Apply(CCustomCSView &mnview, const uint32_t height) override;
Res Erase(CCustomCSView &mnview, uint32_t height, const std::vector<std::string> &) override;
static constexpr const char *TypeName() { return "ATTRIBUTES"; }
static GovVariable *Create() { return new ATTRIBUTES(); }
template <typename T>
static void GetIf(std::optional<T> &opt, const CAttributeValue &var) {
if (auto value = std::get_if<T>(&var)) {
opt = *value;
}
}
template <typename T>
static void GetIf(T &val, const CAttributeValue &var) {
if (auto value = std::get_if<T>(&var)) {
val = *value;
}
}
template <typename K, typename T>
[[nodiscard]] T GetValue(const K &key, T value) const {
static_assert(std::is_convertible_v<K, CAttributeType>);
auto it = attributes.find(key);
if (it != attributes.end()) {
GetIf(value, it->second);
}
return value;
}
template <typename K, typename T>
void SetValue(const K &key, T &&value) {
static_assert(std::is_convertible_v<K, CAttributeType>);
static_assert(std::is_convertible_v<T, CAttributeValue>);
changed.insert(key);
attributes[key] = std::forward<T>(value);
}
template <typename K>
bool EraseKey(const K &key) {
static_assert(std::is_convertible_v<K, CAttributeType>);
if (attributes.erase(key)) {
changed.insert(key);
return true;
}
return false;
}
template <typename K>
[[nodiscard]] bool CheckKey(const K &key) const {
static_assert(std::is_convertible_v<K, CAttributeType>);
return attributes.count(key) > 0;
}
template <typename C, typename K>
void ForEach(const C &callback, const K &key) const {
static_assert(std::is_convertible_v<K, CAttributeType>);
static_assert(std::is_invocable_r_v<bool, C, K, CAttributeValue>);
for (auto it = attributes.lower_bound(key); it != attributes.end(); ++it) {
if (auto attrV0 = std::get_if<K>(&it->first)) {
if (!std::invoke(callback, *attrV0, it->second)) {
break;
}
}
}
}
[[nodiscard]] const std::map<CAttributeType, CAttributeValue> &GetAttributesMap() const { return attributes; }
ADD_OVERRIDE_VECTOR_SERIALIZE_METHODS
ADD_OVERRIDE_SERIALIZE_METHODS(CDataStream)
template <typename Stream, typename Operation>
inline void SerializationOp(Stream &s, Operation ser_action) {
READWRITE(attributes);
}
uint32_t time{0};
std::shared_ptr<CScopedTemplate> evmTemplate{};
// For formatting in export
static const std::map<uint8_t, std::string> &displayVersions();
static const std::map<uint8_t, std::string> &displayTypes();
static const std::map<uint8_t, std::string> &displayParamsIDs();
static const std::map<uint8_t, std::string> &allowedExportParamsIDs();
static const std::map<uint8_t, std::string> &displayLocksIDs();
static const std::map<uint8_t, std::string> &displayOracleIDs();
static const std::map<uint8_t, std::string> &displayGovernanceIDs();
static const std::map<uint8_t, std::string> &displayTransferIDs();
static const std::map<uint8_t, std::string> &displayEVMIDs();
static const std::map<uint8_t, std::string> &displayVaultIDs();
static const std::map<uint8_t, std::string> &displayRulesIDs();
static const std::map<uint8_t, std::map<uint8_t, std::string>> &displayKeys();
Res RefundFuturesContracts(CCustomCSView &mnview,
const uint32_t height,
const uint32_t tokenID = std::numeric_limits<uint32_t>::max());
void AddTokenSplit(const uint32_t tokenID) { tokenSplits.insert(tokenID); }
private:
friend class CGovView;
bool futureUpdated{};
bool futureDUSDUpdated{};
std::set<uint32_t> tokenSplits{};
std::set<uint32_t> interestTokens{};
std::set<CAttributeType> changed;
std::map<CAttributeType, CAttributeValue> attributes;
// Defined allowed arguments
static const std::map<std::string, uint8_t> &allowedVersions();
static const std::map<std::string, uint8_t> &allowedTypes();
static const std::map<std::string, uint8_t> &allowedParamIDs();
static const std::map<std::string, uint8_t> &allowedLocksIDs();
static const std::map<std::string, uint8_t> &allowedOracleIDs();
static const std::map<std::string, uint8_t> &allowedGovernanceIDs();
static const std::map<std::string, uint8_t> &allowedTransferIDs();
static const std::map<std::string, uint8_t> &allowedEVMIDs();
static const std::map<std::string, uint8_t> &allowedVaultIDs();
static const std::map<std::string, uint8_t> &allowedRulesIDs();
static const std::map<uint8_t, std::map<std::string, uint8_t>> &allowedKeys();
static const std::map<uint8_t, std::map<uint8_t, std::function<ResVal<CAttributeValue>(const std::string &)>>>
&parseValue();
Res ProcessVariable(const std::string &key,
const std::optional<UniValue> &value,
std::function<Res(const CAttributeType &, const CAttributeValue &)> applyVariable);
Res RefundFuturesDUSD(CCustomCSView &mnview, const uint32_t height);
};
#endif // DEFI_DFI_GOVVARIABLES_ATTRIBUTES_H