Fabcoin Core  0.16.2
P2P Digital Currency
contractabi.h
Go to the documentation of this file.
1 #ifndef CONTRACTABI_H
2 #define CONTRACTABI_H
3 #include <string>
4 #include <vector>
5 #include <map>
6 #include <QRegularExpression>
7 #include <QStringList>
8 
9 #define paternUint "^[0-9]{1,77}$"
10 #define paternInt "^\\-{0,1}[0-9]{1,76}$"
11 #define paternAddress "^[a-fA-F0-9]{40,40}$"
12 #define paternBool "^true$|^false$"
13 #define paternHex "^[a-fA-F0-9]{1,}$"
14 #define paternBytes paternHex
15 #define paternBytes32 "^[a-fA-F0-9]{%1,%1}$"
16 
17 
23 {
24 public:
28  enum Type
29  {
40  };
41 
45  ParameterType(const std::string& _type = "");
46 
52  bool determine(const std::string& _type);
53 
58  size_t wholeBits() const;
59 
64  size_t decimalBits() const;
65 
70  size_t totalBytes() const;
71 
76  size_t length() const;
77 
82  bool isList() const;
83 
89  bool isDynamic() const;
90 
95  bool isValid() const;
96 
101  const std::string& canonical() const;
102 
107  Type type() const;
108 
109 private:
113  void clean();
114 
116  int m_whole;
118  int m_length;
119  bool m_isList;
120  bool m_valid;
121  std::string m_canonical;
122 };
123 
125 {
126 public:
128  {
129  Ok = 0,
132  DecodingError
133  };
134 
135  ParameterABI(const std::string& _name = "", const std::string& _type = "", bool _indexed = false);
136  ~ParameterABI();
137  bool abiIn(const std::vector<std::string> &value, std::string &data, std::map<int, std::string>& mapDynamic) const;
138  bool abiOut(const std::string &data, size_t& pos, std::vector<std::string> &value) const;
139  const ParameterType &decodeType() const;
140  static bool getRegularExpession(const ParameterType &paramType, QRegularExpression &regEx);
141 
142  std::string name; // The name of the parameter;
143  std::string type; // The canonical type of the parameter.
144  bool indexed; // True if the field is part of the log's topics, false if it one of the log's data segment.
145  // Indexed is only used with event function
146 
147  ErrorType lastError() const;
148 
149 private:
150  bool abiInBasic(ParameterType::Type abiType, std::string value, std::string &data) const;
151  bool abiOutBasic(ParameterType::Type abiType, const std::string &data, size_t &pos, std::string &value) const;
152  void addDynamic(const std::string& paramData, std::string &data, std::map<int, std::string>& mapDynamic) const;
153 
154 
157 };
158 
160 {
161 public:
162  FunctionABI(const std::string& _name = "",
163  const std::string& _type = "function",
164  const std::vector<ParameterABI>& _inputs = std::vector<ParameterABI>(),
165  const std::vector<ParameterABI>& _outputs = std::vector<ParameterABI>(),
166  bool _payable = false,
167  bool _constant = false,
168  bool _anonymous = false);
169 
170  bool abiIn(const std::vector<std::vector<std::string>>& values, std::string& data, std::vector<ParameterABI::ErrorType>& errors) const;
171 
172  bool abiOut(const std::string& data, std::vector<std::vector<std::string>>& values, std::vector<ParameterABI::ErrorType>& errors) const;
173 
174  std::string selector() const;
175 
176  static std::string defaultSelector();
177 
178  QString errorMessage(std::vector<ParameterABI::ErrorType>& errors, bool in) const;
179 
180  std::string name; // The name of the function;
181  std::string type; // Function types: "function", "constructor", "fallback" or "event"
182  std::vector<ParameterABI> inputs; // Array of input parameters
183  std::vector<ParameterABI> outputs; // Array of output parameters, can be omitted if function doesn't return
184  bool payable; // True if function accepts ether, defaults to false.
185  bool constant; // True if function is specified to not modify blockchain state.
186  bool anonymous; // True if the event was declared as anonymous.
187 
188  // Constructor and fallback function never have name or outputs.
189  // Fallback function doesn't have inputs either.
190  // Event function is the only one that have anonymous.
191  // Sending non-zero ether to non-payable function will throw.
192  // Type can be omitted, defaulting to "function".
193 
194 private:
195  void processDynamicParams(const std::map<int, std::string>& mapDynamic, std::string& data) const;
196 };
197 
199 {
200 public:
201  ContractABI();
202  bool loads(const std::string& json_data);
203  void clean();
204 
205  std::vector<FunctionABI> functions;
206 };
207 
208 #endif // CONTRACTABI_H
std::vector< ParameterABI > outputs
Definition: contractabi.h:183
void clean()
clean Set the value to the defaults
size_t decimalBits() const
decimalBits Get the number of bits for the decimal part of the number
size_t length() const
length Length of the list, applicable for list only
ErrorType m_lastError
Definition: contractabi.h:156
bool determine(const std::string &_type)
determine Determine the type from the string
bool isList() const
isList Is the data type list
bool anonymous
Definition: contractabi.h:186
bool isDynamic() const
isDynamic Check if the type is dynamic.
std::vector< FunctionABI > functions
Definition: contractabi.h:205
std::string m_canonical
Definition: contractabi.h:121
bytes abiIn(std::string _id, T const &..._t)
Definition: ABI.h:64
std::string name
Definition: contractabi.h:142
T abiOut(bytes const &_data)
Definition: ABI.h:88
The ParameterType class Decode the api parameter type, provide more informations about the data type ...
Definition: contractabi.h:22
std::string type
Definition: contractabi.h:181
std::vector< ParameterABI > inputs
Definition: contractabi.h:182
std::string name
Definition: contractabi.h:180
const std::string & canonical() const
canonical Get the canonical type
std::string type
Definition: contractabi.h:143
ParameterType m_decodeType
Definition: contractabi.h:155
size_t wholeBits() const
wholeBits Get the number of bits for the whole part of the number
ParameterType(const std::string &_type="")
ParameterType Constructor.
size_t totalBytes() const
totalBytes Get the total size in bytes
Type
The Type enum ABI data types.
Definition: contractabi.h:28
bool isValid() const
isValid Check if the type is valid
uint8_t const * data
Definition: sha3.h:19
Type type() const
type Get the type