From 9c526cce7d780ff18ec158e277553b162e3cfdff Mon Sep 17 00:00:00 2001 From: dimxy Date: Mon, 21 Jan 2019 21:04:56 +0500 Subject: [PATCH] corr heir rpc coins -> satoshi conversion --- src/cc/CCHeir.h | 8 ++--- src/cc/heir.cpp | 72 +++++++++++++++++++++++++++++----------- src/wallet/rpcwallet.cpp | 46 ++++++++++++++++--------- 3 files changed, 87 insertions(+), 39 deletions(-) diff --git a/src/cc/CCHeir.h b/src/cc/CCHeir.h index ecaff9cdb..30334f6e1 100644 --- a/src/cc/CCHeir.h +++ b/src/cc/CCHeir.h @@ -27,10 +27,10 @@ bool HeirValidate(struct CCcontract_info *cp,Eval* eval,const CTransaction &tx, class CoinHelper; class TokenHelper; -UniValue HeirFundCoinCaller(uint64_t txfee, int64_t funds, std::string heirName, CPubKey heirPubkey, int64_t inactivityTimeSec, uint256 tokenid); -UniValue HeirFundTokenCaller(uint64_t txfee, int64_t funds, std::string heirName, CPubKey heirPubkey, int64_t inactivityTimeSec, uint256 tokenid); -UniValue HeirClaimCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount); -UniValue HeirAddCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount); +UniValue HeirFundCoinCaller(int64_t txfee, int64_t satoshis, std::string heirName, CPubKey heirPubkey, int64_t inactivityTimeSec, uint256 tokenid); +UniValue HeirFundTokenCaller(int64_t txfee, int64_t satoshis, std::string heirName, CPubKey heirPubkey, int64_t inactivityTimeSec, uint256 tokenid); +UniValue HeirClaimCaller(uint256 fundingtxid, int64_t txfee, std::string amount); +UniValue HeirAddCaller(uint256 fundingtxid, int64_t txfee, std::string amount); UniValue HeirInfo(uint256 fundingtxid); UniValue HeirList(); diff --git a/src/cc/heir.cpp b/src/cc/heir.cpp index ccf9da1bf..4ef143271 100644 --- a/src/cc/heir.cpp +++ b/src/cc/heir.cpp @@ -615,7 +615,7 @@ template int64_t LifetimeHeirContractFunds(struct CCcontract_info * and also for setting spending plan for the funds' owner and heir * @return fundingtxid handle for subsequent references to this heir funding plan */ -template UniValue HeirFund(uint64_t txfee, int64_t amount, std::string heirName, CPubKey heirPubkey, int64_t inactivityTimeSec, uint256 tokenid) +template UniValue HeirFund(int64_t txfee, int64_t amount, std::string heirName, CPubKey heirPubkey, int64_t inactivityTimeSec, uint256 tokenid) { UniValue result(UniValue::VOBJ); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); @@ -629,7 +629,8 @@ template UniValue HeirFund(uint64_t txfee, int64_t amount, std if (!heirPubkey.IsValid()) { std::cerr << "HeirFund() heirPubkey is not valid!" << std::endl; - return std::string(""); + result.push_back(Pair("result", "error")); + result.push_back(Pair("error", "invalid heir pubkey")); } CPubKey myPubkey = pubkey2pk(Mypubkey()); @@ -689,12 +690,12 @@ template UniValue HeirFund(uint64_t txfee, int64_t amount, std } // if no these callers - it could not link -UniValue HeirFundCoinCaller(uint64_t txfee, int64_t funds, std::string heirName, CPubKey heirPubkey, int64_t inactivityTimeSec, uint256 tokenid){ - return HeirFund(txfee, funds, heirName, heirPubkey, inactivityTimeSec, tokenid); +UniValue HeirFundCoinCaller(int64_t txfee, int64_t satoshis, std::string heirName, CPubKey heirPubkey, int64_t inactivityTimeSec, uint256 tokenid){ + return HeirFund(txfee, satoshis, heirName, heirPubkey, inactivityTimeSec, tokenid); } -UniValue HeirFundTokenCaller(uint64_t txfee, int64_t funds, std::string heirName, CPubKey heirPubkey, int64_t inactivityTimeSec, uint256 tokenid) { - return HeirFund(txfee, funds, heirName, heirPubkey, inactivityTimeSec, tokenid); +UniValue HeirFundTokenCaller(int64_t txfee, int64_t satoshis, std::string heirName, CPubKey heirPubkey, int64_t inactivityTimeSec, uint256 tokenid) { + return HeirFund(txfee, satoshis, heirName, heirPubkey, inactivityTimeSec, tokenid); } /** @@ -702,7 +703,7 @@ UniValue HeirFundTokenCaller(uint64_t txfee, int64_t funds, std::string heirName * creates tx to add more funds to cryptocondition address for spending by either funds' owner or heir * @return result object with raw tx or error text */ -template UniValue _HeirAdd(uint256 fundingtxid, uint64_t txfee, int64_t amount, uint256 latesttxid, uint8_t funcId, uint256 tokenid, CPubKey ownerPubkey, CPubKey heirPubkey, int64_t inactivityTimeSec, std::string heirName, uint8_t hasHeirSpendingBegun) +template UniValue _HeirAdd(uint256 fundingtxid, int64_t txfee, int64_t amount, uint256 latesttxid, uint8_t funcId, uint256 tokenid, CPubKey ownerPubkey, CPubKey heirPubkey, int64_t inactivityTimeSec, std::string heirName, uint8_t hasHeirSpendingBegun) { UniValue result(UniValue::VOBJ); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); @@ -781,13 +782,11 @@ template UniValue _HeirAdd(uint256 fundingtxid, uint64_t txfee, i result.push_back(Pair("error", "can't find normal inputs for tx fee")); } - - return result; } -UniValue HeirAddCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount) { +UniValue HeirAddCaller(uint256 fundingtxid, int64_t txfee, std::string strAmount) { CPubKey ownerPubkey, heirPubkey; int64_t inactivityTimeSec; @@ -798,10 +797,27 @@ UniValue HeirAddCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount) { uint8_t hasHeirSpendingBegun = 0; if ((latesttxid = FindLatestFundingTx(fundingtxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun)) != zeroid) { - if (tokenid == zeroid) - return _HeirAdd(fundingtxid, txfee, amount, latesttxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun); - else - return _HeirAdd(fundingtxid, txfee, amount, latesttxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun); + if (tokenid == zeroid) { + int64_t amount = (int64_t)(atof(strAmount.c_str()) * COIN); + if (amount <= 0) { + UniValue result(UniValue::VOBJ); + result.push_back(Pair("result", "error")); + result.push_back(Pair("error", "invalid amount")); + return result; + } + + return _HeirAdd(fundingtxid, txfee, amount, latesttxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun); + } + else { + int64_t amount = atoll(strAmount.c_str()); + if (amount <= 0) { + UniValue result(UniValue::VOBJ); + result.push_back(Pair("result", "error")); + result.push_back(Pair("error", "invalid amount")); + return result; + } + return _HeirAdd(fundingtxid, txfee, amount, latesttxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun); + } } else { UniValue result(UniValue::VOBJ); @@ -819,7 +835,7 @@ UniValue HeirAddCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount) { * creates tx to spend funds from cryptocondition address by either funds' owner or heir * @return result object with raw tx or error text */ -template UniValue _HeirClaim(uint256 fundingtxid, uint64_t txfee, int64_t amount, uint256 latesttxid, uint8_t funcId, uint256 tokenid, CPubKey ownerPubkey, CPubKey heirPubkey, int64_t inactivityTimeSec, std::string heirName, uint8_t hasHeirSpendingBegun) +template UniValue _HeirClaim(uint256 fundingtxid, int64_t txfee, int64_t amount, uint256 latesttxid, uint8_t funcId, uint256 tokenid, CPubKey ownerPubkey, CPubKey heirPubkey, int64_t inactivityTimeSec, std::string heirName, uint8_t hasHeirSpendingBegun) { UniValue result(UniValue::VOBJ); CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); @@ -940,7 +956,7 @@ template UniValue _HeirClaim(uint256 fundingtxid, uint64_t txfe return result; } -UniValue HeirClaimCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount) { +UniValue HeirClaimCaller(uint256 fundingtxid, int64_t txfee, std::string strAmount) { CPubKey ownerPubkey, heirPubkey; int64_t inactivityTimeSec; @@ -951,10 +967,26 @@ UniValue HeirClaimCaller(uint256 fundingtxid, uint64_t txfee, int64_t amount) { uint8_t hasHeirSpendingBegun = 0; if ((latesttxid = FindLatestFundingTx(fundingtxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun)) != zeroid) { - if( tokenid == zeroid ) - return _HeirClaim(fundingtxid, txfee, amount, latesttxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun); - else - return _HeirClaim(fundingtxid, txfee, amount, latesttxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun); + if (tokenid == zeroid) { + int64_t amount = (int64_t)(atof(strAmount.c_str()) * COIN); + if (amount < 0) { + UniValue result(UniValue::VOBJ); + result.push_back(Pair("result", "error")); + result.push_back(Pair("error", "invalid amount")); + return result; + } + return _HeirClaim(fundingtxid, txfee, amount, latesttxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun); + } + else { + int64_t amount = atoll(strAmount.c_str()); + if (amount <= 0) { + UniValue result(UniValue::VOBJ); + result.push_back(Pair("result", "error")); + result.push_back(Pair("error", "invalid amount")); + return result; + } + return _HeirClaim(fundingtxid, txfee, amount, latesttxid, funcId, tokenid, ownerPubkey, heirPubkey, inactivityTimeSec, heirName, hasHeirSpendingBegun); + } } else { diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 8e304c167..35325e1e0 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -7362,7 +7362,7 @@ UniValue heirfund(const UniValue& params, bool fHelp) { UniValue result(UniValue::VOBJ); uint256 tokenid = zeroid; - uint64_t txfee; + int64_t txfee; int64_t amount; int64_t inactivitytime; std::string hex; @@ -7373,21 +7373,34 @@ UniValue heirfund(const UniValue& params, bool fHelp) return NullUniValue; if (fHelp || params.size() != 5 && params.size() != 6) - throw runtime_error("heirfundtokens fee funds heirname heirpubkey inactivitytime [tokenid]\n"); + throw runtime_error("heirfund txfee funds heirname heirpubkey inactivitytime [tokenid]\n"); if (ensure_CCrequirements() < 0) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); const CKeyStore& keystore = *pwalletMain; LOCK2(cs_main, pwalletMain->cs_wallet); - txfee = atoll((char*)params[0].get_str().c_str()); - amount = atoll((char*)params[1].get_str().c_str()); + txfee = atoll(params[0].get_str().c_str()); + if (txfee < 0) + throw runtime_error("incorrect txfee param\n"); + + if(params.size() == 6) // tokens in satoshis: + amount = atoll(params[1].get_str().c_str()); + else // coins: + amount = atof(params[1].get_str().c_str()) * COIN; + + if( amount <= 0 ) + throw runtime_error("incorrect amount\n"); + name = params[2].get_str(); pubkey = ParseHex(params[3].get_str().c_str()); if( !pubkey2pk(pubkey).IsValid() ) throw runtime_error("incorrect pubkey\n"); - inactivitytime = atof((char*)params[4].get_str().c_str()); + inactivitytime = atoll(params[4].get_str().c_str()); + if (inactivitytime <= 0) + throw runtime_error("incorrect inactivity time param\n"); + if (params.size() == 6) { tokenid = Parseuint256((char*)params[5].get_str().c_str()); if(tokenid == zeroid) @@ -7406,7 +7419,7 @@ UniValue heiradd(const UniValue& params, bool fHelp) { UniValue result; uint256 fundingtxid; - uint64_t txfee; + int64_t txfee; int64_t amount; int64_t inactivitytime; std::string hex; @@ -7417,18 +7430,20 @@ UniValue heiradd(const UniValue& params, bool fHelp) return NullUniValue; if (fHelp || params.size() != 3) - throw runtime_error("heiraddtokens fee funds fundingtxid\n"); + throw runtime_error("heiradd txfee funds fundingtxid\n"); if (ensure_CCrequirements() < 0) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); const CKeyStore& keystore = *pwalletMain; LOCK2(cs_main, pwalletMain->cs_wallet); - txfee = atoll((char*)params[0].get_str().c_str()); - amount = atoll((char*)params[1].get_str().c_str()); + txfee = atoll(params[0].get_str().c_str()); + if (txfee < 0) + throw runtime_error("incorrect txfee param\n"); + fundingtxid = Parseuint256((char*)params[2].get_str().c_str()); - result = HeirAddCaller(fundingtxid, txfee, amount); + result = HeirAddCaller(fundingtxid, txfee, params[1].get_str()); return result; } @@ -7437,7 +7452,6 @@ UniValue heirclaim(const UniValue& params, bool fHelp) UniValue result; // result(UniValue::VOBJ); uint256 fundingtxid; int64_t txfee; - int64_t amount; int64_t inactivitytime; std::string hex; std::vector pubkey; @@ -7448,18 +7462,20 @@ UniValue heirclaim(const UniValue& params, bool fHelp) return NullUniValue; if (fHelp || params.size() != 3) - throw runtime_error("heirclaimtokens fee funds fundingtxid\n"); + throw runtime_error("heirclaim txfee funds fundingtxid\n"); if (ensure_CCrequirements() < 0) throw runtime_error("to use CC contracts, you need to launch daemon with valid -pubkey= for an address in your wallet\n"); const CKeyStore& keystore = *pwalletMain; LOCK2(cs_main, pwalletMain->cs_wallet); - txfee = atoll((char*)params[0].get_str().c_str()); - amount = atoll((char*)params[1].get_str().c_str()); + txfee = atoll(params[0].get_str().c_str()); + if (txfee < 0) + throw runtime_error("incorrect txfee param\n"); + fundingtxid = Parseuint256((char*)params[2].get_str().c_str()); - result = HeirClaimCaller(fundingtxid, txfee, amount); + result = HeirClaimCaller(fundingtxid, txfee, params[1].get_str()); return result; }