From 0a76f9d8972b3a33eac9c0600c093982b85420f1 Mon Sep 17 00:00:00 2001 From: dimxy Date: Sat, 12 Jan 2019 15:20:55 +0500 Subject: [PATCH] AssetXXX() changed to GetTokenBalance, TokenInfo, TokenList added processing of incorrect tokenid to GetTokenBalance --- src/cc/CCassets.h | 6 ++-- src/cc/CCassetstx.cpp | 57 --------------------------------- src/cc/CCtokens.cpp | 68 ++++++++++++++++++++++++++++++++++++++++ src/cc/CCtokens.h | 4 +++ src/wallet/rpcwallet.cpp | 29 ++++++++++++----- 5 files changed, 96 insertions(+), 68 deletions(-) diff --git a/src/cc/CCassets.h b/src/cc/CCassets.h index 5a5ef2c82..39644efc6 100644 --- a/src/cc/CCassets.h +++ b/src/cc/CCassets.h @@ -47,12 +47,12 @@ int64_t AssetValidateSellvin(struct CCcontract_info *cp,Eval* eval,int64_t &tmpp bool AssetExactAmounts(struct CCcontract_info *cpAssets, int64_t &inputs, int64_t &outputs, Eval* eval, const CTransaction &tx, uint256 assetid); // CCassetstx -int64_t GetAssetBalance(CPubKey pk,uint256 tokenid); +//int64_t GetAssetBalance(CPubKey pk,uint256 tokenid); // --> GetTokenBalance() int64_t AddAssetInputs(struct CCcontract_info *cp, CMutableTransaction &mtx, CPubKey pk, uint256 assetid, int64_t total, int32_t maxinputs); UniValue AssetOrders(uint256 tokenid); -UniValue AssetInfo(uint256 tokenid); -UniValue AssetList(); +//UniValue AssetInfo(uint256 tokenid); +//UniValue AssetList(); //std::string CreateAsset(int64_t txfee,int64_t assetsupply,std::string name,std::string description); //std::string AssetTransfer(int64_t txfee,uint256 assetid,std::vector destpubkey,int64_t total); //std::string AssetConvert(int64_t txfee,uint256 assetid,std::vector destpubkey,int64_t total,int32_t evalcode); diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 1c2012534..3047876f4 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -66,63 +66,6 @@ int64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubK } -int64_t GetAssetBalance(CPubKey pk,uint256 tokenid) -{ - CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); - struct CCcontract_info *cp,C; - cp = CCinit(&C,EVAL_TOKENS); - return(AddTokenCCInputs(cp,mtx,pk,tokenid,0,0)); -} - -UniValue AssetInfo(uint256 assetid) -{ - UniValue result(UniValue::VOBJ); uint256 hashBlock; CTransaction vintx; std::vector origpubkey; std::string name,description; char str[67],numstr[65]; - if ( GetTransaction(assetid,vintx,hashBlock,false) == 0 ) - { - fprintf(stderr,"cant find assetid\n"); - result.push_back(Pair("result","error")); - result.push_back(Pair("error","cant find assetid")); - return(result); - } - if ( vintx.vout.size() > 0 && DecodeTokenCreateOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,origpubkey,name,description) == 0 ) - { - fprintf(stderr,"assetid isnt token creation txid\n"); - result.push_back(Pair("result","error")); - result.push_back(Pair("error","assetid isnt token creation txid")); - } - result.push_back(Pair("result","success")); - result.push_back(Pair("tokenid",uint256_str(str,assetid))); - result.push_back(Pair("owner",pubkey33_str(str,origpubkey.data()))); - result.push_back(Pair("name",name)); - result.push_back(Pair("supply",vintx.vout[0].nValue)); - result.push_back(Pair("description",description)); - return(result); -} - -UniValue AssetList() -{ - UniValue result(UniValue::VARR); - std::vector > addressIndex; - struct CCcontract_info *cp,C; uint256 txid,hashBlock; - CTransaction vintx; std::vector origpubkey; - std::string name,description; char str[65]; - - cp = CCinit(&C,EVAL_TOKENS); - SetCCtxids(addressIndex,cp->normaladdr); - for (std::vector >::const_iterator it=addressIndex.begin(); it!=addressIndex.end(); it++) - { - txid = it->first.txhash; - if ( GetTransaction(txid,vintx,hashBlock,false) != 0 ) - { - if ( vintx.vout.size() > 0 && DecodeTokenCreateOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey,origpubkey,name,description) != 0 ) - { - result.push_back(uint256_str(str,txid)); - } - } - } - return(result); -} - UniValue AssetOrders(uint256 refassetid) { static uint256 zero; diff --git a/src/cc/CCtokens.cpp b/src/cc/CCtokens.cpp index 5c848b081..613d4452f 100644 --- a/src/cc/CCtokens.cpp +++ b/src/cc/CCtokens.cpp @@ -623,4 +623,72 @@ std::string TokenTransfer(int64_t txfee, uint256 assetid, std::vector d fprintf(stderr, "not enough normal inputs for txfee\n"); } return(""); +} + + +int64_t GetTokenBalance(CPubKey pk, uint256 tokenid) +{ + uint256 hashBlock; + CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight()); + CTransaction tokentx; + + if (GetTransaction(tokenid, tokentx, hashBlock, false) == 0) + { + fprintf(stderr, "cant find tokenid\n"); + CCerror = strprintf("cant find tokenid"); + return 0; + } + + struct CCcontract_info *cp, C; + cp = CCinit(&C, EVAL_TOKENS); + return(AddTokenCCInputs(cp, mtx, pk, tokenid, 0, 0)); +} + +UniValue TokenInfo(uint256 tokenid) +{ + UniValue result(UniValue::VOBJ); uint256 hashBlock; CTransaction vintx; std::vector origpubkey; std::string name, description; char str[67], numstr[65]; + if (GetTransaction(tokenid, vintx, hashBlock, false) == 0) + { + fprintf(stderr, "cant find assetid\n"); + result.push_back(Pair("result", "error")); + result.push_back(Pair("error", "cant find tokenid")); + return(result); + } + if (vintx.vout.size() > 0 && DecodeTokenCreateOpRet(vintx.vout[vintx.vout.size() - 1].scriptPubKey, origpubkey, name, description) == 0) + { + fprintf(stderr, "assetid isnt token creation txid\n"); + result.push_back(Pair("result", "error")); + result.push_back(Pair("error", "assetid isnt token creation txid")); + } + result.push_back(Pair("result", "success")); + result.push_back(Pair("tokenid", uint256_str(str, tokenid))); + result.push_back(Pair("owner", pubkey33_str(str, origpubkey.data()))); + result.push_back(Pair("name", name)); + result.push_back(Pair("supply", vintx.vout[0].nValue)); + result.push_back(Pair("description", description)); + return(result); +} + +UniValue TokenList() +{ + UniValue result(UniValue::VARR); + std::vector > addressIndex; + struct CCcontract_info *cp, C; uint256 txid, hashBlock; + CTransaction vintx; std::vector origpubkey; + std::string name, description; char str[65]; + + cp = CCinit(&C, EVAL_TOKENS); + SetCCtxids(addressIndex, cp->normaladdr); + for (std::vector >::const_iterator it = addressIndex.begin(); it != addressIndex.end(); it++) + { + txid = it->first.txhash; + if (GetTransaction(txid, vintx, hashBlock, false) != 0) + { + if (vintx.vout.size() > 0 && DecodeTokenCreateOpRet(vintx.vout[vintx.vout.size() - 1].scriptPubKey, origpubkey, name, description) != 0) + { + result.push_back(uint256_str(str, txid)); + } + } + } + return(result); } \ No newline at end of file diff --git a/src/cc/CCtokens.h b/src/cc/CCtokens.h index 34223bc0b..e7bb62101 100644 --- a/src/cc/CCtokens.h +++ b/src/cc/CCtokens.h @@ -32,6 +32,10 @@ bool TokensExactAmounts(bool goDeeper, struct CCcontract_info *cpTokens, int64_t std::string CreateToken(int64_t txfee, int64_t assetsupply, std::string name, std::string description); std::string TokenTransfer(int64_t txfee, uint256 assetid, std::vector destpubkey, int64_t total); +int64_t GetTokenBalance(CPubKey pk, uint256 tokenid); +UniValue TokenInfo(uint256 tokenid); +UniValue TokenList(); + //this is in CCinclude.h int64_t AddTokenCCInputs(struct CCcontract_info *cp, CMutableTransaction &mtx, CPubKey pk, uint256 tokenid, int64_t total, int32_t maxinputs); //this is in CCinclude.h uint8_t DecodeTokenCreateOpRet(const CScript &scriptPubKey,std::vector &origpubkey,std::string &name,std::string &description); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index b39a19668..2cf195661 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -6947,7 +6947,7 @@ UniValue tokenlist(const UniValue& params, bool fHelp) throw runtime_error("tokenlist\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"); - return(AssetList()); + return(TokenList()); } UniValue tokeninfo(const UniValue& params, bool fHelp) @@ -6958,7 +6958,7 @@ UniValue tokeninfo(const UniValue& params, bool fHelp) 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"); tokenid = Parseuint256((char *)params[0].get_str().c_str()); - return(AssetInfo(tokenid)); + return(TokenInfo(tokenid)); } UniValue tokenorders(const UniValue& params, bool fHelp) @@ -6977,22 +6977,35 @@ UniValue tokenorders(const UniValue& params, bool fHelp) UniValue tokenbalance(const UniValue& params, bool fHelp) { UniValue result(UniValue::VOBJ); char destaddr[64]; uint256 tokenid; uint64_t balance; std::vector pubkey; struct CCcontract_info *cp,C; + CCerror.clear(); + cp = CCinit(&C,EVAL_ASSETS); if ( fHelp || params.size() > 2 ) throw runtime_error("tokenbalance tokenid [pubkey]\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"); - LOCK(cs_main); + + LOCK(cs_main); + tokenid = Parseuint256((char *)params[0].get_str().c_str()); if ( params.size() == 2 ) pubkey = ParseHex(params[1].get_str().c_str()); - else pubkey = Mypubkey(); + else + pubkey = Mypubkey(); result.push_back(Pair("result", "success")); - if ( GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0 ) + if (GetCCaddress(cp,destaddr,pubkey2pk(pubkey)) != 0) result.push_back(Pair("CCaddress",destaddr)); - balance = GetAssetBalance(pubkey2pk(pubkey),tokenid); - result.push_back(Pair("tokenid", params[0].get_str())); - result.push_back(Pair("balance", (int64_t)balance)); + + balance = GetTokenBalance(pubkey2pk(pubkey),tokenid); + + if (CCerror.empty()) { + result.push_back(Pair("tokenid", params[0].get_str())); + result.push_back(Pair("balance", (int64_t)balance)); + } + else { + ERR_RESULT(CCerror); + } + return(result); }