forked from trustwallet/wallet-core
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOperation.cpp
More file actions
61 lines (48 loc) · 2 KB
/
Copy pathOperation.cpp
File metadata and controls
61 lines (48 loc) · 2 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
#include "Operation.h"
#include <stdexcept>
using namespace TW::Bravo;
using json = nlohmann::json;
/// Transfer Operation
TransferOperation::TransferOperation(const std::string& from, const std::string& to,
const Asset& asset, const std::string& memo)
: from(from), to(to), memo(memo), asset(asset) {
validate();
}
TransferOperation::TransferOperation(const std::string& from, const std::string& to, int64_t amount,
bool isTestNet, const std::string& memo)
: TransferOperation(from, to, Asset(amount, isTestNet), memo) {}
TransferOperation::TransferOperation(const std::string& from, const std::string& to,
const std::string& asset, const std::string& memo)
: TransferOperation(from, to, Asset::fromString(asset), memo) {}
void TransferOperation::validate() {
if (from.size() > MaxAccountNameSize) {
throw std::invalid_argument("\"from\" cannot be greater than " +
std::to_string(MaxAccountNameSize));
}
if (to.size() > MaxAccountNameSize) {
throw std::invalid_argument("\"to\" cannot be greater than " +
std::to_string(MaxAccountNameSize));
}
if (memo.size() > MaxMemoSize) {
throw std::invalid_argument("\"memo\" cannot be greater than " +
std::to_string(MaxMemoSize));
}
if (asset.amount <= 0) {
throw std::invalid_argument("Cannot transfer a negative amount (aka: stealing)");
}
}
void TransferOperation::serialize(Data& os) const noexcept {
encodeVarInt32(TransferOperation::OperationId, os);
encodeString(from, os);
encodeString(to, os);
asset.serialize(os);
encodeString(memo, os);
}
json TransferOperation::serialize() const noexcept {
json data;
data["from"] = from;
data["to"] = to;
data["amount"] = asset.string();
data["memo"] = memo;
return json::array({"transfer", data});
}