IoTeX client
abi.h
1#ifndef IOTEX_ABI_H
2#define IOTEX_ABI_H
3
4#include "extern/cjson/cJSON.h"
5#include "helpers/client_helper.h"
6#include "helpers/json_helper.h"
7#include "IoTeXConstants.h"
8#include "IoTeXResultCodes.h"
9#include <vector>
10
11namespace iotex
12{
13namespace abi
14{
19enum EthereumTypeName
20{
21 UINT,
22 INT,
23 ADDRESS,
24 BOOL,
25 FIXED,
26 UFIXED,
27 BYTES_STATIC,
28 STRING,
29 BYTES_DYNAMIC,
30 ARRAY_STATIC,
31 ARRAY_DYNAMIC,
32 TUPLE_STATIC,
33 TUPLE_DYNAMIC,
34 UNKNOWN
35};
36
41static const char* EthereumTypeNameToStringLT[] = {"uint", "int", "address", "bool", "fixed",
42 "ufixed", "bytes", "string", "bytes", "",
43 "", "", ""};
44
45// Json objects
52{
53 public:
54 IotexString name;
55 EthereumTypeName type;
56 uint32_t sizeBytes;
57 EthereumTypeName arrayType;
58 uint32_t arrayTypeSizeBytes = 0;
59 uint32_t arraySize;
60
69 static ResultCode getTypeAndSizeFromString(IotexString& str, InputOutputAbi& out);
70
77 bool IsDynamic()
78 {
79 bool isDynamic = false;
80 if(type == EthereumTypeName::BYTES_DYNAMIC)
81 isDynamic = true;
82 else if(type == EthereumTypeName::STRING)
83 isDynamic = true;
84 else if(type == EthereumTypeName::ARRAY_DYNAMIC)
85 isDynamic = true;
86 else if(type == EthereumTypeName::TUPLE_DYNAMIC)
87 isDynamic = true;
88 else if(type == EthereumTypeName::ARRAY_STATIC)
89 {
90 if(arrayType == EthereumTypeName::BYTES_DYNAMIC ||
91 arrayType == EthereumTypeName::STRING ||
92 arrayType == EthereumTypeName::ARRAY_DYNAMIC ||
93 arrayType == EthereumTypeName::TUPLE_DYNAMIC)
94 isDynamic = true;
95 }
96 return isDynamic;
97 }
98
106 bool isInteger() const
107 {
108 return (type == EthereumTypeName::UINT || type == EthereumTypeName::INT);
109 }
110
111 private:
112 static ResultCode getSizeFromStringAndCheckIfArray(IotexString& str, InputOutputAbi& out);
113};
114
120{
121 public:
122 bool constant;
123 std::vector<InputOutputAbi> inputs; // Not present in fallback
124 IotexString name;
125 std::vector<InputOutputAbi> outputs; // Not present in constructor or fallback
126 IotexString stateMutability;
127 bool payable;
128
134 void getSignature(IotexString& out);
135};
136
137// Parsing
145ResultCode parseContract(const IotexString& contractAbi, std::vector<FunctionAbi>& out);
146
154ResultCode parseFunction(const cJSON* data, FunctionAbi& out);
155
163ResultCode parseInputOutput(const cJSON* data, InputOutputAbi& out);
164
165// Utils
172{
173 public:
174 union
175 {
176 uint8_t uint8;
177 uint16_t uint16;
178 uint32_t uint32;
179 uint64_t uint64;
180 int8_t int8;
181 int16_t int16;
182 int32_t int32;
183 int64_t int64;
184 IotexString* uint256;
185 bool boolean;
186 IotexString* string;
187 uint8_t* bytes;
188 ParameterValue* elements; // Array or tuple elements
189 } value;
190 int32_t size = -1;
191 bool isBigInt = false;
192 EthereumTypeName type = EthereumTypeName::UNKNOWN;
193
201 {
202 return (type == EthereumTypeName::BYTES_DYNAMIC || type == EthereumTypeName::STRING ||
203 type == EthereumTypeName::ARRAY_DYNAMIC || type == EthereumTypeName::TUPLE_DYNAMIC);
204 }
205};
206
213ParameterValue MakeParamUint(uint64_t value);
214
221ParameterValue MakeParamInt(int64_t value);
222
229ParameterValue MakeParamString(IotexString& value);
230
237ParameterValue MakeParamAddress(uint8_t value[ETH_ADDRESS_SIZE]);
238
247ParameterValue MakeParamBytes(uint8_t value[], size_t size, bool dynamic);
248} // namespace abi
249} // namespace iotex
250
251#endif
A class that represents the ABI for a contract function.
Definition: abi.h:120
void getSignature(IotexString &out)
Gets the funciton signature as a string.
Definition: abi.cpp:295
A class that represents the ABI for an input or output field of a contract funciton.
Definition: abi.h:52
static ResultCode getTypeAndSizeFromString(IotexString &str, InputOutputAbi &out)
Parses the size and type from the input/output "type" property in the ABI JSON.
Definition: abi.cpp:154
bool IsDynamic()
Checks whether the type of this input or output is dynamic or static.
Definition: abi.h:77
bool isInteger() const
Checks if the type of this input or output is an integer (int or uint) type of any size.
Definition: abi.h:106
Class that represents the value of a contract function input or output parameter.
Definition: abi.h:172
bool isDynamic()
Checks if this parameter is dynamic.
Definition: abi.h:200
Definition: abi.h:12