4 #include <validation.h> 13 static const std::string TOKEN_ABI =
"[{\"constant\":true,\"inputs\":[],\"name\":\"name\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"approve\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"totalSupply\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_from\",\"type\":\"address\"},{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transferFrom\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"decimals\",\"outputs\":[{\"name\":\"\",\"type\":\"uint8\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"burn\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"}],\"name\":\"balanceOf\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_from\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"burnFrom\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"symbol\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_to\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"}],\"name\":\"transfer\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"_spender\",\"type\":\"address\"},{\"name\":\"_value\",\"type\":\"uint256\"},{\"name\":\"_extraData\",\"type\":\"bytes\"}],\"name\":\"approveAndCall\",\"outputs\":[{\"name\":\"success\",\"type\":\"bool\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"address\"},{\"name\":\"\",\"type\":\"address\"}],\"name\":\"allowance\",\"outputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[{\"name\":\"initialSupply\",\"type\":\"uint256\"},{\"name\":\"tokenName\",\"type\":\"string\"},{\"name\":\"decimalUnits\",\"type\":\"uint8\"},{\"name\":\"tokenSymbol\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"from\",\"type\":\"address\"},{\"indexed\":true,\"name\":\"to\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Transfer\",\"type\":\"event\"},{\"anonymous\":false,\"inputs\":[{\"indexed\":true,\"name\":\"from\",\"type\":\"address\"},{\"indexed\":false,\"name\":\"value\",\"type\":\"uint256\"}],\"name\":\"Burn\",\"type\":\"event\"}]";
15 static const QString PRC_CALL =
"callcontract";
16 static const QString PRC_SENDTO =
"sendtocontract";
17 static const QString PARAM_ADDRESS =
"address";
18 static const QString PARAM_DATAHEX =
"datahex";
19 static const QString PARAM_AMOUNT =
"amount";
20 static const QString PARAM_GASLIMIT =
"gaslimit";
21 static const QString PARAM_GASPRICE =
"gasprice";
22 static const QString PARAM_SENDER =
"sender";
23 static const QString PARAM_BROADCAST =
"broadcast";
24 static const QString PARAM_CHANGE_TO_SENDER =
"changeToSender";
68 funcApproveAndCall(-1),
75 bool ToHash160(
const std::string& strFabTokenAddress, std::string& strHash160)
93 fabTokenAddress.
Set(keyid);
95 strFabTokenAddress = fabTokenAddress.
ToString();
108 QStringList lstMandatory;
109 lstMandatory.append(PARAM_ADDRESS);
110 lstMandatory.append(PARAM_DATAHEX);
111 QStringList lstOptional;
112 lstOptional.append(PARAM_SENDER);
113 d->
call =
new ExecRPCCommand(PRC_CALL, lstMandatory, lstOptional, QMap<QString, QString>());
116 lstMandatory.clear();
117 lstMandatory.append(PARAM_ADDRESS);
118 lstMandatory.append(PARAM_DATAHEX);
120 lstOptional.append(PARAM_AMOUNT);
121 lstOptional.append(PARAM_GASLIMIT);
122 lstOptional.append(PARAM_GASPRICE);
123 lstOptional.append(PARAM_SENDER);
124 lstOptional.append(PARAM_BROADCAST);
125 lstOptional.append(PARAM_CHANGE_TO_SENDER);
126 d->
send =
new ExecRPCCommand(PRC_SENDTO, lstMandatory, lstOptional, QMap<QString, QString>());
138 if(func.
name ==
"name")
142 else if(func.
name ==
"approve")
146 else if(func.
name ==
"totalSupply")
150 else if(func.
name ==
"transferFrom")
154 else if(func.
name ==
"decimals")
158 else if(func.
name ==
"burn")
162 else if(func.
name ==
"balanceOf")
166 else if(func.
name ==
"burnFrom")
170 else if(func.
name ==
"symbol")
174 else if(func.
name ==
"transfer")
178 else if(func.
name ==
"approveAndCall")
182 else if(func.
name ==
"allowance")
186 else if(func.
name ==
"Transfer")
190 else if(func.
name ==
"Burn")
219 d->
lstParams[PARAM_ADDRESS] = QString::fromStdString(address);
224 d->
lstParams[PARAM_DATAHEX] = QString::fromStdString(datahex);
229 d->
lstParams[PARAM_AMOUNT] = QString::fromStdString(amount);
234 d->
lstParams[PARAM_GASLIMIT] = QString::fromStdString(gaslimit);
239 d->
lstParams[PARAM_GASPRICE] = QString::fromStdString(gasPrice);
244 d->
lstParams[PARAM_SENDER] = QString::fromStdString(sender);
253 setGasLimit(std::to_string(DEFAULT_GAS_LIMIT_OP_SEND));
256 d->
lstParams[PARAM_CHANGE_TO_SENDER] =
"true";
266 std::vector<std::string> input;
267 std::vector<std::string> output;
273 if(output.size() == 0)
282 bool Token::approve(
const std::string &_spender,
const std::string &_value,
bool &success,
bool sendTo)
284 std::vector<std::string> input;
285 input.push_back(_spender);
286 input.push_back(_value);
287 std::vector<std::string> output;
294 if(output.size() == 0)
297 success = output[0] ==
"true";
305 std::vector<std::string> input;
306 std::vector<std::string> output;
312 if(output.size() == 0)
321 bool Token::transferFrom(
const std::string &_from,
const std::string &_to,
const std::string &_value,
bool &success,
bool sendTo)
323 std::vector<std::string> input;
324 input.push_back(_from);
325 input.push_back(_to);
326 input.push_back(_value);
327 std::vector<std::string> output;
334 if(output.size() == 0)
337 success = output[0] ==
"true";
345 std::vector<std::string> input;
346 std::vector<std::string> output;
352 if(output.size() == 0)
361 bool Token::burn(
const std::string &_value,
bool &success,
bool sendTo)
363 std::vector<std::string> input;
364 input.push_back(_value);
365 std::vector<std::string> output;
372 if(output.size() == 0)
375 success = output[0] ==
"true";
383 std::string spender =
d->
lstParams[PARAM_SENDER].toStdString();
389 return balanceOf(spender, result, sendTo);
394 std::vector<std::string> input;
395 input.push_back(spender);
396 std::vector<std::string> output;
403 if(output.size() == 0)
412 bool Token::burnFrom(
const std::string &_from,
const std::string &_value,
bool &success,
bool sendTo)
414 std::vector<std::string> input;
415 input.push_back(_from);
416 input.push_back(_value);
417 std::vector<std::string> output;
424 if(output.size() == 0)
427 success = output[0] ==
"true";
435 std::vector<std::string> input;
436 std::vector<std::string> output;
442 if(output.size() == 0)
453 std::string to = _to;
459 std::vector<std::string> input;
461 input.push_back(_value);
462 std::vector<std::string> output;
467 bool Token::approveAndCall(
const std::string &_spender,
const std::string &_value,
const std::string &_extraData,
bool &success,
bool sendTo)
469 std::vector<std::string> input;
470 input.push_back(_spender);
471 input.push_back(_value);
472 input.push_back(_extraData);
473 std::vector<std::string> output;
480 if(output.size() == 0)
483 success = output[0] ==
"true";
489 bool Token::allowance(
const std::string &_from,
const std::string &_to, std::string &result,
bool sendTo)
491 std::vector<std::string> input;
492 input.push_back(_from);
493 input.push_back(_to);
494 std::vector<std::string> output;
501 if(output.size() == 0)
515 bool Token::burnEvents(std::vector<TokenEvent> &tokenEvents, int64_t fromBlock, int64_t toBlock)
520 bool Token::exec(
const std::vector<std::string> &input,
int func, std::vector<std::string> &output,
bool sendTo)
528 std::vector<std::vector<std::string>> values;
529 for(
size_t i = 0; i < input.size(); i++)
531 std::vector<std::string> param;
532 param.push_back(input[i]);
533 values.push_back(param);
535 std::vector<ParameterABI::ErrorType> errors;
536 if(!
function.
abiIn(values, strData, errors))
544 QString errorMessage;
551 QVariantMap variantMap = result.toMap();
552 QVariantMap executionResultMap = variantMap.value(
"executionResult").toMap();
553 std::string rawData = executionResultMap.value(
"output").toString().toStdString();
554 std::vector<std::vector<std::string>> values;
555 std::vector<ParameterABI::ErrorType> errors;
556 if(!
function.
abiOut(rawData, values, errors))
558 for(
size_t i = 0; i < values.size(); i++)
560 std::vector<std::string> param = values[i];
561 output.push_back(param.size() ? param[0] :
"");
566 QVariantMap variantMap = result.toMap();
567 d->
txid = variantMap.value(
"txid").toString().toStdString();
573 bool Token::execEvents(int64_t fromBlock, int64_t toBlock,
int func, std::vector<TokenEvent> &tokenEvents)
576 if(func == -1 || fromBlock < 0)
584 std::string eventName =
function.selector();
585 std::string contractAddress =
d->
lstParams[PARAM_ADDRESS].toStdString();
586 std::string senderAddress =
d->
lstParams[PARAM_SENDER].toStdString();
588 senderAddress =
"000000000000000000000000" + senderAddress;
593 QList<QVariant> list = result.toList();
594 for(
int i = 0; i < list.size(); i++)
597 QVariantMap variantMap = list[i].toMap();
598 QList<QVariant> listLog = variantMap.value(
"log").toList();
599 for(
int i = 0; i < listLog.size(); i++)
602 QVariantMap variantLog = listLog[0].toMap();
603 QList<QVariant> topicsList = variantLog.value(
"topics").toList();
604 if(topicsList.count() < 3)
continue;
605 if(topicsList[0].
toString().toStdString() != eventName)
continue;
609 tokenEvent.
address = variantMap.value(
"contractAddress").toString().toStdString();
610 tokenEvent.
sender = topicsList[1].toString().toStdString().substr(24);
612 tokenEvent.
receiver = topicsList[2].toString().toStdString().substr(24);
614 tokenEvent.
blockHash =
uint256S(variantMap.value(
"blockHash").toString().toStdString());
615 tokenEvent.
blockNumber = variantMap.value(
"blockNumber").toLongLong();
619 std::string
data = variantLog.value(
"data").toString().toStdString();
625 tokenEvents.push_back(tokenEvent);
QMap< QString, QString > lstParams
bool ToFabTokenAddress(const std::string &strHash160, std::string &strFabTokenAddress)
base58-encoded Fabcoin addresses.
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
bool decimals(std::string &result, bool sendTo=false)
bool burnEvents(std::vector< TokenEvent > &tokenEvents, int64_t fromBlock=0, int64_t toBlock=-1)
std::string toString(string32 const &_s)
Make normal string from fixed-length string.
bool burn(const std::string &_value, bool &success, bool sendTo=false)
std::vector< FunctionABI > functions
bool searchTokenTx(int64_t fromBlock, int64_t toBlock, std::string strContractAddress, std::string strSenderAddress, QVariant &result)
searchTokenTx Search the event log for token transactions
bool exec(const QMap< QString, QString > ¶ms, QVariant &result, QString &resultJson, QString &errorMessage)
exec Execute the RPC command
bool execEvents(int64_t fromBlock, int64_t toBlock, int func, std::vector< TokenEvent > &tokenEvents)
bytes abiIn(std::string _id, T const &..._t)
bool name(std::string &result, bool sendTo=false)
bytes fromHex(std::string const &_s, WhenError _throw=WhenError::DontThrow)
bool totalSupply(std::string &result, bool sendTo=false)
T abiOut(bytes const &_data)
uint256 uint256S(const char *str)
void setAddress(const std::string &address)
bool burnFrom(const std::string &_from, const std::string &_value, bool &success, bool sendTo=false)
std::vector< byte > bytes
bool allowance(const std::string &_from, const std::string &_to, std::string &result, bool sendTo=false)
bool exec(const std::vector< std::string > &input, int func, std::vector< std::string > &output, bool sendTo)
void setDataHex(const std::string &datahex)
std::string ToString() const
void setGasLimit(const std::string &gaslimit)
bool transferEvents(std::vector< TokenEvent > &tokenEvents, int64_t fromBlock=0, int64_t toBlock=-1)
void setGasPrice(const std::string &gasPrice)
boost::multiprecision::number< boost::multiprecision::cpp_int_backend< 256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void >> u256
std::string FormatMoney(const CAmount &n)
Money parsing/formatting utilities.
bool symbol(std::string &result, bool sendTo=false)
bool transfer(const std::string &_to, const std::string &_value, bool sendTo=false)
bool ToHash160(const std::string &strFabTokenAddress, std::string &strHash160)
bool GetKeyID(CKeyID &keyID) const
uint256 u256Touint(const dev::u256 &in)
void setAmount(const std::string &amount)
The ExecRPCCommand class Execution of RPC command line.
A reference to a CKey: the Hash160 of its serialized public key.
bool balanceOf(std::string &result, bool sendTo=false)
bool loads(const std::string &json_data)
std::vector< unsigned char > valtype
bool approveAndCall(const std::string &_spender, const std::string &_value, const std::string &_extraData, bool &success, bool sendTo=false)
struct evm_uint160be address(struct evm_env *env)
void setSender(const std::string &sender)
bool transferFrom(const std::string &_from, const std::string &_to, const std::string &_value, bool &success, bool sendTo=false)
bool approve(const std::string &_spender, const std::string &_value, bool &success, bool sendTo=false)
bool Set(const CKeyID &id)
std::vector< unsigned char > ParseHex(const char *psz)