diff --git a/src/cc/CCassets.h b/src/cc/CCassets.h index 82fbcb961..036ba1340 100644 --- a/src/cc/CCassets.h +++ b/src/cc/CCassets.h @@ -47,7 +47,7 @@ bool AssetCalcAmounts(struct CCcontract_info *cpAssets, int64_t &inputs, int64_t //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 AssetOrders(uint256 tokenid, CPubKey pubkey); //UniValue AssetInfo(uint256 tokenid); //UniValue AssetList(); //std::string CreateAsset(int64_t txfee,int64_t assetsupply,std::string name,std::string description); diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 7673724f1..2b77e99bb 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -66,100 +66,102 @@ int64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubK } */ -UniValue AssetOrders(uint256 refassetid) +UniValue AssetOrders(uint256 refassetid, CPubKey pk) { static uint256 zero; UniValue result(UniValue::VARR); + struct CCcontract_info *cpAssets, assetsC; + struct CCcontract_info *cpTokens, tokensC; + + cpAssets = CCinit(&assetsC, EVAL_ASSETS); + cpTokens = CCinit(&assetsC, EVAL_TOKENS); + auto addOrders = [&](struct CCcontract_info *cp, std::vector >::const_iterator it) { uint256 txid, hashBlock, assetid, assetid2; int64_t price; std::vector origpubkey; - CTransaction vintx; + CTransaction ordertx; uint8_t funcid, evalCode; - char numstr[32], funcidstr[16], origaddr[64], assetidstr[65]; + char numstr[32], funcidstr[16], origaddr[64], origtokenaddr[64], assetidstr[65]; txid = it->first.txhash; //std::cerr << "addOrders() txid=" << txid.GetHex() << std::endl; - if ( GetTransaction(txid, vintx, hashBlock, false) != 0 ) + if ( GetTransaction(txid, ordertx, hashBlock, false) != 0 ) { // for logging: funcid = DecodeAssetOpRet(vintx.vout[vintx.vout.size() - 1].scriptPubKey, evalCode, assetid, assetid2, price, origpubkey); - //std::cerr << "addOrders() vintx.vout.size()=" << vintx.vout.size() << " funcid=" << (char)(funcid ? funcid : ' ') << " assetid=" << assetid.GetHex() << std::endl; - if (vintx.vout.size() > 0 && (funcid = DecodeAssetTokenOpRet(vintx.vout[vintx.vout.size()-1].scriptPubKey, evalCode, assetid, assetid2, price, origpubkey)) != 0) + if (ordertx.vout.size() > 0 && (funcid = DecodeAssetTokenOpRet(ordertx.vout[ordertx.vout.size()-1].scriptPubKey, evalCode, assetid, assetid2, price, origpubkey)) != 0) { - if (refassetid != zero && assetid != refassetid) - { - //int32_t z; - //for (z=31; z>=0; z--) fprintf(stderr,"%02x",((uint8_t *)&txid)[z]); - //fprintf(stderr," txid\n"); - //for (z=31; z>=0; z--) fprintf(stderr,"%02x",((uint8_t *)&assetid)[z]); - //fprintf(stderr," assetid\n"); - //for (z=31; z>=0; z--) fprintf(stderr,"%02x",((uint8_t *)&refassetid)[z]); - //fprintf(stderr," refassetid\n"); - return; - } + LOGSTREAM("ccassets", CCLOG_DEBUG2, stream << "addOrders() checking ordertx.vout.size()=" << ordertx.vout.size() << " funcid=" << (char)(funcid ? funcid : ' ') << " assetid=" << assetid.GetHex() << std::endl); - //std::cerr << "addOrders() it->first.index=" << it->first.index << " vintx.vout[it->first.index].nValue=" << vintx.vout[it->first.index].nValue << std::endl; - if (vintx.vout[it->first.index].nValue == 0) - return; + if (refassetid != zero && assetid != refassetid || + pk.IsValid() && pk == pubkey2pk(origpubkey) && (funcid == 'S' || funcid == 's')) + { - UniValue item(UniValue::VOBJ); + LOGSTREAM("ccassets", CCLOG_DEBUG2, stream << "addOrders() it->first.index=" << it->first.index << " ordertx.vout[it->first.index].nValue=" << ordertx.vout[it->first.index].nValue << std::endl); + if (ordertx.vout[it->first.index].nValue == 0) { + LOGSTREAM("ccassets", CCLOG_DEBUG2, stream << "addOrders() order with value=0 skipped" << std::endl); + return; + } - funcidstr[0] = funcid; - funcidstr[1] = 0; - item.push_back(Pair("funcid", funcidstr)); - item.push_back(Pair("txid", uint256_str(assetidstr,txid))); - item.push_back(Pair("vout", (int64_t)it->first.index)); - if ( funcid == 'b' || funcid == 'B' ) - { - sprintf(numstr,"%.8f",(double)vintx.vout[it->first.index].nValue/COIN); - item.push_back(Pair("amount",numstr)); - sprintf(numstr,"%.8f",(double)vintx.vout[0].nValue/COIN); - item.push_back(Pair("bidamount",numstr)); - } - else - { - sprintf(numstr,"%llu",(long long)vintx.vout[it->first.index].nValue); - item.push_back(Pair("amount",numstr)); - sprintf(numstr,"%llu",(long long)vintx.vout[0].nValue); - item.push_back(Pair("askamount",numstr)); - } - if ( origpubkey.size() == 33 ) - { - GetCCaddress(cp, origaddr, pubkey2pk(origpubkey)); // TODO: what is this? is it asset or token?? - item.push_back(Pair("origaddress", origaddr)); - } - if ( assetid != zeroid ) - item.push_back(Pair("tokenid",uint256_str(assetidstr,assetid))); - if ( assetid2 != zeroid ) - item.push_back(Pair("otherid",uint256_str(assetidstr,assetid2))); - if ( price > 0 ) - { - if ( funcid == 's' || funcid == 'S' || funcid == 'e' || funcid == 'e' ) + UniValue item(UniValue::VOBJ); + + funcidstr[0] = funcid; + funcidstr[1] = 0; + item.push_back(Pair("funcid", funcidstr)); + item.push_back(Pair("txid", uint256_str(assetidstr, txid))); + item.push_back(Pair("vout", (int64_t)it->first.index)); + if (funcid == 'b' || funcid == 'B') { - sprintf(numstr,"%.8f",(double)price / COIN); - item.push_back(Pair("totalrequired", numstr)); - sprintf(numstr,"%.8f",(double)price / (COIN * vintx.vout[0].nValue)); - item.push_back(Pair("price", numstr)); + sprintf(numstr, "%.8f", (double)ordertx.vout[it->first.index].nValue / COIN); + item.push_back(Pair("amount", numstr)); + sprintf(numstr, "%.8f", (double)ordertx.vout[0].nValue / COIN); + item.push_back(Pair("bidamount", numstr)); } else { - item.push_back(Pair("totalrequired", (int64_t)price)); - sprintf(numstr,"%.8f",(double)vintx.vout[0].nValue / (price * COIN)); - item.push_back(Pair("price",numstr)); + sprintf(numstr, "%llu", (long long)ordertx.vout[it->first.index].nValue); + item.push_back(Pair("amount", numstr)); + sprintf(numstr, "%llu", (long long)ordertx.vout[0].nValue); + item.push_back(Pair("askamount", numstr)); } + if (origpubkey.size() == 33) + { + GetCCaddress(cp, origaddr, pubkey2pk(origpubkey)); + item.push_back(Pair("origaddress", origaddr)); + GetTokensCCaddress(cpTokens, origtokenaddr, pubkey2pk(origpubkey)); + item.push_back(Pair("origtokenaddress", origtokenaddr)); + + } + if (assetid != zeroid) + item.push_back(Pair("tokenid", uint256_str(assetidstr, assetid))); + if (assetid2 != zeroid) + item.push_back(Pair("otherid", uint256_str(assetidstr, assetid2))); + if (price > 0) + { + if (funcid == 's' || funcid == 'S' || funcid == 'e' || funcid == 'e') + { + sprintf(numstr, "%.8f", (double)price / COIN); + item.push_back(Pair("totalrequired", numstr)); + sprintf(numstr, "%.8f", (double)price / (COIN * ordertx.vout[0].nValue)); + item.push_back(Pair("price", numstr)); + } + else + { + item.push_back(Pair("totalrequired", (int64_t)price)); + sprintf(numstr, "%.8f", (double)ordertx.vout[0].nValue / (price * COIN)); + item.push_back(Pair("price", numstr)); + } + } + result.push_back(item); + LOGSTREAM("ccassets", CCLOG_DEBUG1, stream << "addOrders() added order funcId=" << (char)(funcid ? funcid : ' ') << " it->first.index=" << it->first.index << " ordertx.vout[it->first.index].nValue=" << ordertx.vout[it->first.index].nValue << " tokenid=" << assetid.GetHex() << std::endl); } - result.push_back(item); - //fprintf(stderr,"addOrders() func.(%c) %s/v%d %.8f\n",funcid,uint256_str(assetidstr,txid),(int32_t)it->first.index,(double)vintx.vout[it->first.index].nValue/COIN); } } }; std::vector > unspentOutputsTokens, unspentOutputsCoins; - struct CCcontract_info *cpAssets, assetsC; - - cpAssets = CCinit(&assetsC, EVAL_ASSETS); char assetsUnspendableAddr[64]; GetCCaddress(cpAssets, assetsUnspendableAddr, GetUnspendable(cpAssets, NULL)); diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index d870e627c..98b5deccc 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -499,6 +499,7 @@ static const CRPCCommand vRPCCommands[] = { "tokens", "tokeninfo", &tokeninfo, true }, { "tokens", "tokenlist", &tokenlist, true }, { "tokens", "tokenorders", &tokenorders, true }, + { "tokens", "mytokenorders", &mytokenorders, true }, { "tokens", "tokenaddress", &tokenaddress, true }, { "tokens", "tokenbalance", &tokenbalance, true }, { "tokens", "tokencreate", &tokencreate, true }, diff --git a/src/rpc/server.h b/src/rpc/server.h index a2aba895b..9cacce8ac 100644 --- a/src/rpc/server.h +++ b/src/rpc/server.h @@ -241,6 +241,7 @@ extern UniValue coinsupply(const UniValue& params, bool fHelp); extern UniValue tokeninfo(const UniValue& params, bool fHelp); extern UniValue tokenlist(const UniValue& params, bool fHelp); extern UniValue tokenorders(const UniValue& params, bool fHelp); +extern UniValue mytokenorders(const UniValue& params, bool fHelp); extern UniValue tokenbalance(const UniValue& params, bool fHelp); extern UniValue assetsaddress(const UniValue& params, bool fHelp); extern UniValue tokenaddress(const UniValue& params, bool fHelp); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index c0f9dc297..643381648 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -7038,8 +7038,8 @@ UniValue tokenorders(const UniValue& params, bool fHelp) { uint256 tokenid; if ( fHelp || params.size() > 1 ) - throw runtime_error("tokenorders [tokenid]\n"); - if ( ensure_CCrequirements(EVAL_ASSETS) < 0 ) + throw runtime_error("tokenorders tokenid\n"); + if (ensure_CCrequirements(EVAL_ASSETS) < 0 || ensure_CCrequirements(EVAL_TOKENS) < 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); @@ -7048,9 +7048,25 @@ UniValue tokenorders(const UniValue& params, bool fHelp) if (tokenid == zeroid) throw runtime_error("incorrect tokenid\n"); } - else - memset(&tokenid,0,sizeof(tokenid)); - return(AssetOrders(tokenid)); + else { + // memset(&tokenid, 0, sizeof(tokenid)); + throw runtime_error("no tokenid\n"); + } + return(AssetOrders(tokenid, CPubKey())); +} + + +UniValue mytokenorders(const UniValue& params, bool fHelp) +{ + uint256 tokenid; + if (fHelp || params.size() > 1) + throw runtime_error("mytokenorders\n"); + if (ensure_CCrequirements(EVAL_ASSETS) < 0 || ensure_CCrequirements(EVAL_TOKENS) < 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); + + return(AssetOrders(zeroid, Mypubkey())); } UniValue tokenbalance(const UniValue& params, bool fHelp) @@ -7235,7 +7251,7 @@ UniValue tokenbid(const UniValue& params, bool fHelp) UniValue result(UniValue::VOBJ); int64_t bidamount,numtokens; std::string hex; double price; uint256 tokenid; if ( fHelp || params.size() != 3 ) throw runtime_error("tokenbid numtokens tokenid price\n"); - if ( ensure_CCrequirements(EVAL_ASSETS) < 0 ) + if (ensure_CCrequirements(EVAL_ASSETS) < 0 || ensure_CCrequirements(EVAL_TOKENS) < 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); @@ -7277,7 +7293,7 @@ UniValue tokencancelbid(const UniValue& params, bool fHelp) UniValue result(UniValue::VOBJ); std::string hex; int32_t i; uint256 tokenid,bidtxid; if ( fHelp || params.size() != 2 ) throw runtime_error("tokencancelbid tokenid bidtxid\n"); - if ( ensure_CCrequirements(EVAL_ASSETS) < 0 ) + if (ensure_CCrequirements(EVAL_ASSETS) < 0 || ensure_CCrequirements(EVAL_TOKENS) < 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); @@ -7302,7 +7318,7 @@ UniValue tokenfillbid(const UniValue& params, bool fHelp) UniValue result(UniValue::VOBJ); int64_t fillamount; std::string hex; uint256 tokenid,bidtxid; if ( fHelp || params.size() != 3 ) throw runtime_error("tokenfillbid tokenid bidtxid fillamount\n"); - if ( ensure_CCrequirements(EVAL_ASSETS) < 0 ) + if (ensure_CCrequirements(EVAL_ASSETS) < 0 || ensure_CCrequirements(EVAL_TOKENS) < 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); @@ -7334,7 +7350,7 @@ UniValue tokenask(const UniValue& params, bool fHelp) UniValue result(UniValue::VOBJ); int64_t askamount,numtokens; std::string hex; double price; uint256 tokenid; if ( fHelp || params.size() != 3 ) throw runtime_error("tokenask numtokens tokenid price\n"); - if ( ensure_CCrequirements(EVAL_ASSETS) < 0 ) + if (ensure_CCrequirements(EVAL_ASSETS) < 0 || ensure_CCrequirements(EVAL_TOKENS) < 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); @@ -7396,7 +7412,7 @@ UniValue tokencancelask(const UniValue& params, bool fHelp) UniValue result(UniValue::VOBJ); std::string hex; int32_t i; uint256 tokenid,asktxid; if ( fHelp || params.size() != 2 ) throw runtime_error("tokencancelask tokenid asktxid\n"); - if ( ensure_CCrequirements(EVAL_ASSETS) < 0 ) + if (ensure_CCrequirements(EVAL_ASSETS) < 0 || ensure_CCrequirements(EVAL_TOKENS) < 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); @@ -7421,7 +7437,7 @@ UniValue tokenfillask(const UniValue& params, bool fHelp) UniValue result(UniValue::VOBJ); int64_t fillunits; std::string hex; uint256 tokenid,asktxid; if ( fHelp || params.size() != 3 ) throw runtime_error("tokenfillask tokenid asktxid fillunits\n"); - if ( ensure_CCrequirements(EVAL_ASSETS) < 0 ) + if (ensure_CCrequirements(EVAL_ASSETS) < 0 || ensure_CCrequirements(EVAL_TOKENS) < 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);