Fabcoin Core  0.16.2
P2P Digital Currency
eventlog.cpp
Go to the documentation of this file.
1 #include <eventlog.h>
2 #include <execrpccommand.h>
3 #include <QJsonObject>
4 #include <QJsonArray>
5 #include <QJsonDocument>
6 #include <uint256.h>
7 
8 namespace EventLog_NS
9 {
10 static const QString RPC_SERACH_LOGS = "searchlogs";
11 static const QString PARAM_FROM_BLOCK = "fromBlock";
12 static const QString PARAM_TO_BLOCK = "toBlock";
13 static const QString PARAM_ADDRESSES = "address";
14 static const QString PARAM_TOPICS = "topics";
15 }
16 using namespace EventLog_NS;
17 
18 QString createJsonString(std::string key, const std::vector<std::string> value)
19 {
20  QJsonObject json;
21  QJsonArray array;
22  for(size_t i = 0; i < value.size(); i++)
23  {
24  array.append(QJsonValue(QString::fromStdString(value[i])));
25  }
26  json.insert(QString::fromStdString(key), array);
27 
28  QJsonDocument doc(json);
29  QString retString(doc.toJson(QJsonDocument::Compact));
30  retString.insert(0, "'");
31  retString.append("'");
32 
33  return retString;
34 }
35 
37  m_RPCCommand(0)
38 {
39  // Create new searchlogs command line interface
40  QStringList lstMandatory;
41  lstMandatory.append(PARAM_FROM_BLOCK);
42  lstMandatory.append(PARAM_TO_BLOCK);
43  QStringList lstOptional;
44  lstOptional.append(PARAM_ADDRESSES);
45  lstOptional.append(PARAM_TOPICS);
46  m_RPCCommand = new ExecRPCCommand(RPC_SERACH_LOGS, lstMandatory, lstOptional, QMap<QString, QString>());
47 }
48 
50 {
51  if(m_RPCCommand)
52  {
53  delete m_RPCCommand;
54  m_RPCCommand = 0;
55  }
56 }
57 
58 bool EventLog::searchTokenTx(int64_t fromBlock, int64_t toBlock, std::string strContractAddress, std::string strSenderAddress, QVariant &result)
59 {
60  std::vector<std::string> addresses;
61  addresses.push_back(strContractAddress);
62 
63  std::vector<std::string> topics;
64  // Skip the event type check
65  static std::string nullRecord = uint256().ToString();
66  topics.push_back(nullRecord);
67  // Match the log with sender address
68  topics.push_back(strSenderAddress);
69  // Match the log with receiver address
70  topics.push_back(strSenderAddress);
71 
72  return search(fromBlock, toBlock, addresses, topics, result);
73 }
74 
75 bool EventLog::search(int64_t fromBlock, int64_t toBlock, const std::vector<std::string> addresses, const std::vector<std::string> topics, QVariant &result)
76 {
77  setStartBlock(fromBlock);
78  setEndBlock(toBlock);
79  setAddresses(addresses);
80  setTopics(topics);
81 
82  QString resultJson;
83  QString errorMessage;
84  if(!m_RPCCommand->exec(m_lstParams, result, resultJson, errorMessage))
85  return false;
86  return true;
87 }
88 
89 void EventLog::setStartBlock(int64_t fromBlock)
90 {
91  m_lstParams[PARAM_FROM_BLOCK] = QString::number(fromBlock);
92 }
93 
94 void EventLog::setEndBlock(int64_t toBlock)
95 {
96  m_lstParams[PARAM_TO_BLOCK] = QString::number(toBlock);
97 }
98 
99 void EventLog::setAddresses(const std::vector<std::string> addresses)
100 {
101  m_lstParams[PARAM_ADDRESSES] = createJsonString("addresses", addresses);
102 }
103 
104 void EventLog::setTopics(const std::vector<std::string> topics)
105 {
106  m_lstParams[PARAM_TOPICS] = createJsonString("topics", topics);
107 }
void setEndBlock(int64_t toBlock)
Definition: eventlog.cpp:94
void setTopics(const std::vector< std::string > topics)
Definition: eventlog.cpp:104
EventLog()
EventLog Constructor.
Definition: eventlog.cpp:36
~EventLog()
~EventLog Destructor
Definition: eventlog.cpp:49
bool searchTokenTx(int64_t fromBlock, int64_t toBlock, std::string strContractAddress, std::string strSenderAddress, QVariant &result)
searchTokenTx Search the event log for token transactions
Definition: eventlog.cpp:58
bool exec(const QMap< QString, QString > &params, QVariant &result, QString &resultJson, QString &errorMessage)
exec Execute the RPC command
std::string ToString() const
Definition: uint256.cpp:95
ExecRPCCommand * m_RPCCommand
Definition: eventlog.h:52
QMap< QString, QString > m_lstParams
Definition: eventlog.h:53
QString createJsonString(std::string key, const std::vector< std::string > value)
Definition: eventlog.cpp:18
256-bit opaque blob.
Definition: uint256.h:132
The ExecRPCCommand class Execution of RPC command line.
bool search(int64_t fromBlock, int64_t toBlock, const std::vector< std::string > addresses, const std::vector< std::string > topics, QVariant &result)
search Search for log events
Definition: eventlog.cpp:75
void setAddresses(const std::vector< std::string > addresses)
Definition: eventlog.cpp:99
void setStartBlock(int64_t fromBlock)
Definition: eventlog.cpp:89