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
#include "Staking.h"
8
#include "Data.h"
9
#include "HexCoding.h"
10
using namespace TW;
11

12
namespace TW::IoTeX {
13

14 1
std::string FromData(const Data& data) {
15 1
    auto s = std::string(data.begin(), data.end());
16 1
    s.append(data.size(), '\0');
17 1
    return s;
18
}
19

20 1
Data dataFromString(const std::string& d) {
21 1
    Data data;
22 1
    std::copy(d.c_str(), d.c_str() + d.length(), back_inserter(data));
23 1
    return data;
24
}
25

26 1
Data stakingCreate(const Data& candidate, const Data& amount, uint32_t duration, bool autoStake,
27
                   const Data& payload) {
28 1
    auto action = IoTeX::Proto::Staking_Create();
29 1
    action.set_candidatename(FromData(candidate).c_str());
30 1
    action.set_stakedamount(FromData(amount).c_str());
31 1
    action.set_stakedduration(duration);
32 1
    action.set_autostake(autoStake);
33 1
    action.set_payload(FromData(payload).c_str());
34 1
    return dataFromString(action.SerializeAsString());
35
}
36

37 1
Data stakingAddDeposit(uint64_t index, const Data& amount, const Data& payload) {
38 1
    auto action = IoTeX::Proto::Staking_AddDeposit();
39 1
    action.set_bucketindex(index);
40 1
    action.set_amount(FromData(amount).c_str());
41 1
    action.set_payload(FromData(payload).c_str());
42 1
    return dataFromString(action.SerializeAsString());
43
}
44

45 1
Data stakingUnstake(uint64_t index, const Data& payload) {
46 1
    auto action = IoTeX::Proto::Staking_Reclaim();
47 1
    action.set_bucketindex(index);
48 1
    action.set_payload(FromData(payload).c_str());
49 1
    return dataFromString(action.SerializeAsString());
50
}
51

52 1
Data stakingWithdraw(uint64_t index, const Data& payload) {
53 1
    auto action = IoTeX::Proto::Staking_Reclaim();
54 1
    action.set_bucketindex(index);
55 1
    action.set_payload(FromData(payload).c_str());
56 1
    return dataFromString(action.SerializeAsString());
57
}
58

59 1
Data stakingRestake(uint64_t index, uint32_t duration, bool autoStake, const Data& payload) {
60 1
    auto action = IoTeX::Proto::Staking_Restake();
61 1
    action.set_bucketindex(index);
62 1
    action.set_stakedduration(duration);
63 1
    action.set_autostake(autoStake);
64 1
    action.set_payload(FromData(payload).c_str());
65 1
    return dataFromString(action.SerializeAsString());
66
}
67

68 1
Data stakingChangeCandidate(uint64_t index, const Data& candidate, const Data& payload) {
69 1
    auto action = IoTeX::Proto::Staking_ChangeCandidate();
70 1
    action.set_bucketindex(index);
71 1
    action.set_candidatename(FromData(candidate).c_str());
72 1
    action.set_payload(FromData(payload).c_str());
73 1
    return dataFromString(action.SerializeAsString());
74
}
75

76 1
Data stakingTransfer(uint64_t index, const Data& voterAddress, const Data& payload) {
77 1
    auto action = IoTeX::Proto::Staking_TransferOwnership();
78 1
    action.set_bucketindex(index);
79 1
    action.set_voteraddress(FromData(voterAddress).c_str());
80 1
    action.set_payload(FromData(payload).c_str());
81 1
    return dataFromString(action.SerializeAsString());
82
}
83

84 1
Data candidateRegister(const Data& name, const Data& operatorAddress, const Data& rewardAddress,
85
                       const Data& amount, uint32_t duration, bool autoStake,
86
                       const Data& ownerAddress, const Data& payload) {
87 1
    auto cbi = new IoTeX::Proto::Staking_CandidateBasicInfo();
88 1
    cbi->set_name(FromData(name).c_str());
89 1
    cbi->set_operatoraddress(FromData(operatorAddress).c_str());
90 1
    cbi->set_rewardaddress(FromData(rewardAddress).c_str());
91

92 1
    auto action = IoTeX::Proto::Staking_CandidateRegister();
93 1
    action.set_allocated_candidate(cbi);
94 1
    action.set_stakedamount(FromData(amount).c_str());
95 1
    action.set_stakedduration(duration);
96 1
    action.set_autostake(autoStake);
97 1
    action.set_owneraddress(FromData(ownerAddress).c_str());
98 1
    action.set_payload(FromData(payload).c_str());
99 1
    return dataFromString(action.SerializeAsString());
100
}
101

102 1
Data candidateUpdate(const Data& name, const Data& operatorAddress, const Data& rewardAddress) {
103 1
    auto action = IoTeX::Proto::Staking_CandidateBasicInfo();
104 1
    action.set_name(FromData(name).c_str());
105 1
    action.set_operatoraddress(FromData(operatorAddress).c_str());
106 1
    action.set_rewardaddress(FromData(rewardAddress).c_str());
107 1
    return dataFromString(action.SerializeAsString());
108
}
109
} // namespace TW::IoTeX

Read our documentation on viewing source code .

Loading