1
// Copyright © 2017-2020 Trust Wallet.
2
//
3
// This file is part of Trust. The full Trust copyright notice, including
4
// terms governing use, modification, and redistribution, is contained in the
5
// file LICENSE at the root of the source code distribution tree.
6

7
#pragma once
8

9
#include <boost/algorithm/string.hpp>
10
#include <cstdint>
11
#include <math.h>
12
#include <stdint.h>
13
#include <stdio.h>
14
#include <string>
15
#include <vector>
16
#include <optional>
17

18
#include "Address.h"
19
#include "../uint256.h"
20
using namespace std;
21

22
namespace TW::Harmony {
23

24
template <typename Directive>
25 1
class Staking {
26
  public:
27
    uint8_t directive;
28
    Directive stakeMsg;
29
    uint256_t nonce;
30
    uint256_t gasPrice;
31
    uint256_t gasLimit;
32

33
    // Signature values
34
    uint256_t v = uint256_t();
35
    uint256_t r = uint256_t();
36
    uint256_t s = uint256_t();
37

38 1
    Staking(uint8_t directive, Directive stakeMsg, uint256_t nonce, uint256_t gasPrice,
39
            uint256_t gasLimit, uint256_t v, uint256_t r, uint256_t s)
40 1
        : directive(move(directive))
41 1
        , stakeMsg(move(stakeMsg))
42 1
        , nonce(move(nonce))
43 1
        , gasPrice(move(gasPrice))
44 1
        , gasLimit(move(gasLimit))
45 1
        , v(move(v))
46 1
        , r(move(r))
47 1
        , s(move(s)) {}
48
};
49

50
enum Directive : uint8_t {
51
    DirectiveCreateValidator,
52
    DirectiveEditValidator,
53
    DirectiveDelegate,
54
    DirectiveUndelegate,
55
    DirectiveCollectRewards
56
};
57

58 1
class Description {
59
  public:
60
    string name;
61
    string identity;
62
    string website;
63
    string securityContact;
64
    string details;
65

66 1
    Description(string name, string identity, string website, string securityContact,
67
                string details)
68 1
        : name(move(name))
69 1
        , identity(move(identity))
70 1
        , website(move(website))
71 1
        , securityContact(move(securityContact))
72 1
        , details(move(details)) {}
73
};
74

75
const uint256_t MAX_PRECISION = 18;
76

77 1
class Decimal {
78
  public:
79
    uint256_t value;
80

81 1
    Decimal(uint256_t value, uint256_t precision) {
82 1
        if (precision < 1 || precision > MAX_PRECISION) {
83 0
            throw std::invalid_argument("invalid precision, must be between 1 and 17");
84
        }
85 1
        int zerosToAdd = static_cast<int>(MAX_PRECISION - precision);
86 1
        uint256_t multiplier = (uint256_t)pow(10, zerosToAdd);
87 1
        this->value = value * multiplier;
88
    }
89
};
90

91 1
class CommissionRate {
92
  public:
93
    Decimal rate;
94
    Decimal maxRate;
95
    Decimal maxChangeRate;
96

97 1
    CommissionRate(Decimal rate, Decimal maxRate, Decimal maxChangeRate)
98 1
        : rate(move(rate)), maxRate(move(maxRate)), maxChangeRate(move(maxChangeRate)) {}
99
};
100

101 1
class CreateValidator {
102
  public:
103
    Address validatorAddress;
104
    Description description;
105
    CommissionRate commissionRates;
106
    uint256_t minSelfDelegation;
107
    uint256_t maxTotalDelegation;
108
    uint256_t amount;
109
    vector<vector<uint8_t>> slotPubKeys;
110
    vector<vector<uint8_t>> slotKeySigs;
111

112 1
    CreateValidator(Address validatorAddress, Description description,
113
                    CommissionRate commissionRates, uint256_t minSelfDelegation,
114
                    uint256_t maxTotalDelegation, vector<vector<uint8_t>> slotPubKeys,
115
                    vector<vector<uint8_t>> slotKeySigs,uint256_t amount)
116 1
        : validatorAddress(move(validatorAddress))
117 1
        , description(move(description))
118 1
        , commissionRates(move(commissionRates))
119 1
        , minSelfDelegation(move(minSelfDelegation))
120 1
        , maxTotalDelegation(move(maxTotalDelegation))
121 1
        , amount(move(amount))
122 1
        , slotPubKeys(move(slotPubKeys))
123 1
        , slotKeySigs(move(slotKeySigs)) {}
124
};
125

126 1
class EditValidator {
127
  public:
128
    Address validatorAddress;
129
    Description description;
130
    std::optional<Decimal> commissionRate;
131
    uint256_t minSelfDelegation;
132
    uint256_t maxTotalDelegation;
133
    vector<uint8_t> slotKeyToRemove;
134
    vector<uint8_t> slotKeyToAdd;
135
    vector<uint8_t> slotKeyToAddSig;
136
    uint256_t        active;
137

138 1
    EditValidator(Address validatorAddress, Description description, std::optional<Decimal>& commissionRate,
139
                  uint256_t minSelfDelegation, uint256_t maxTotalDelegation,
140
                  vector<uint8_t> slotKeyToRemove, vector<uint8_t> slotKeyToAdd,
141
                  vector<uint8_t> slotKeyToAddSig, uint256_t  active)
142 1
        : validatorAddress(move(validatorAddress))
143 1
        , description(move(description))
144 1
        , commissionRate(move(commissionRate))
145 1
        , minSelfDelegation(move(minSelfDelegation))
146 1
        , maxTotalDelegation(move(maxTotalDelegation))
147 1
        , slotKeyToRemove(move(slotKeyToRemove))
148 1
        , slotKeyToAdd(move(slotKeyToAdd))
149 1
        , slotKeyToAddSig(move(slotKeyToAddSig))
150 1
        , active(move(active)){}
151
};
152

153 1
class Delegate {
154
  public:
155
    Address delegatorAddress;
156
    Address validatorAddress;
157
    uint256_t amount;
158

159 1
    Delegate(Address delegatorAddress, Address validatorAddress, uint256_t amount)
160 1
        : delegatorAddress(move(delegatorAddress))
161 1
        , validatorAddress(move(validatorAddress))
162 1
        , amount(move(amount)) {}
163
};
164

165 1
class Undelegate {
166
  public:
167
    Address delegatorAddress;
168
    Address validatorAddress;
169
    uint256_t amount;
170

171 1
    Undelegate(Address delegatorAddress, Address validatorAddress, uint256_t amount)
172 1
        : delegatorAddress(move(delegatorAddress))
173 1
        , validatorAddress(move(validatorAddress))
174 1
        , amount(move(amount)) {}
175
};
176

177 1
class CollectRewards {
178
  public:
179
    Address delegatorAddress;
180

181 1
    CollectRewards(Address delegatorAddress) : delegatorAddress(move(delegatorAddress)) {}
182
};
183

184
} // namespace TW::Harmony

Read our documentation on viewing source code .

Loading