diff --git a/src/cc/CCassets.h b/src/cc/CCassets.h index afb3579cf..0fb4ae6f7 100644 --- a/src/cc/CCassets.h +++ b/src/cc/CCassets.h @@ -54,6 +54,8 @@ 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); + std::string CreateBuyOffer(int64_t txfee,int64_t bidamount,uint256 assetid,int64_t pricetotal); std::string CancelBuyOffer(int64_t txfee,uint256 assetid,uint256 bidtxid); std::string FillBuyOffer(int64_t txfee,uint256 assetid,uint256 bidtxid,int64_t fillamount); diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index 63d1a1089..2ce13a1ff 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -243,6 +243,33 @@ std::string AssetTransfer(int64_t txfee,uint256 assetid,std::vector des return(""); } +std::string AssetConvert(int64_t txfee,uint256 assetid,std::vector destpubkey,int64_t total,int32_t evalcode) +{ + CMutableTransaction mtx; CPubKey mypk; int64_t CCchange=0,inputs=0; struct CCcontract_info *cp,C; + if ( total < 0 ) + { + fprintf(stderr,"negative total %lld\n",(long long)total); + return(""); + } + cp = CCinit(&C,EVAL_ASSETS); + if ( txfee == 0 ) + txfee = 10000; + mypk = pubkey2pk(Mypubkey()); + if ( AddNormalinputs(mtx,mypk,txfee,1) > 0 ) + { + if ( (inputs= AddAssetInputs(cp,mtx,mypk,assetid,total,60)) > 0 ) + { + if ( inputs > total ) + CCchange = (inputs - total); + mtx.vout.push_back(MakeCC1vout(evalcode,total,pubkey2pk(destpubkey))); + if ( CCchange != 0 ) + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); + return(FinalizeCCTx(0,cp,mtx,mypk,txfee,EncodeAssetOpRet('t',assetid,zeroid,0,Mypubkey()))); + } else fprintf(stderr,"not enough CC asset inputs for %.8f\n",(double)total/COIN); + } + return(""); +} + std::string CreateBuyOffer(int64_t txfee,int64_t bidamount,uint256 assetid,int64_t pricetotal) { CMutableTransaction mtx; CPubKey mypk; struct CCcontract_info *cp,C; uint256 hashBlock; CTransaction vintx; std::vector origpubkey; std::string name,description; diff --git a/src/cc/gateways.cpp b/src/cc/gateways.cpp index 9146a864b..3acc69027 100644 --- a/src/cc/gateways.cpp +++ b/src/cc/gateways.cpp @@ -474,7 +474,7 @@ std::string GatewaysBind(uint64_t txfee,std::string coin,uint256 tokenid,int64_t txfee = 10000; mypk = pubkey2pk(Mypubkey()); gatewayspk = GetUnspendable(cp,0); - if ( _GetCCaddress(destaddr,EVAL_ASSETS,gatewayspk) == 0 ) + if ( _GetCCaddress(destaddr,EVAL_GATEWAYS,gatewayspk) == 0 ) { fprintf(stderr,"Gateway bind.%s (%s) cant create globaladdr\n",coin.c_str(),uint256_str(str,tokenid)); return(""); diff --git a/src/rpcserver.cpp b/src/rpcserver.cpp index 77e53f7a1..8934ae717 100644 --- a/src/rpcserver.cpp +++ b/src/rpcserver.cpp @@ -454,6 +454,7 @@ static const CRPCCommand vRPCCommands[] = { "tokens", "tokencancelask", &tokencancelask, true }, { "tokens", "tokenfillask", &tokenfillask, true }, //{ "tokens", "tokenfillswap", &tokenfillswap, true }, + { "tokens", "tokenconvert", &tokenconvert, true }, /* Address index */ { "addressindex", "getaddressmempool", &getaddressmempool, true }, diff --git a/src/rpcserver.h b/src/rpcserver.h index e0c87756f..f4c502d63 100644 --- a/src/rpcserver.h +++ b/src/rpcserver.h @@ -221,6 +221,7 @@ extern UniValue tokenfillbid(const UniValue& params, bool fHelp); extern UniValue tokenask(const UniValue& params, bool fHelp); extern UniValue tokencancelask(const UniValue& params, bool fHelp); extern UniValue tokenfillask(const UniValue& params, bool fHelp); +extern UniValue tokenconvert(const UniValue& params, bool fHelp); extern UniValue mofnaddress(const UniValue& params, bool fHelp); extern UniValue channelsaddress(const UniValue& params, bool fHelp); extern UniValue oraclesaddress(const UniValue& params, bool fHelp); diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index c1dd1c3fb..e8fff8904 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -6279,6 +6279,42 @@ UniValue tokentransfer(const UniValue& params, bool fHelp) return(result); } +UniValue tokenconvert(const UniValue& params, bool fHelp) +{ + UniValue result(UniValue::VOBJ); std::string hex; int32_t evalcode; int64_t amount; uint256 tokenid; + if ( fHelp || params.size() != 3 ) + throw runtime_error("tokenconvert evalcode tokenid pubkey amount\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); + evalcode = atoi(params[0].get_str().c_str()); + tokenid = Parseuint256((char *)params[1].get_str().c_str()); + std::vector pubkey(ParseHex(params[2].get_str().c_str())); + amount = atol(params[3].get_str().c_str()); + if ( tokenid == zeroid ) + { + ERR_RESULT("invalid tokenid"); + return(result); + } + if ( amount <= 0 ) + { + ERR_RESULT("amount must be positive"); + return(result); + } + hex = AssetConvert(0,tokenid,pubkey,amount,evalcode); + if (amount > 0) { + if ( hex.size() > 0 ) + { + result.push_back(Pair("result", "success")); + result.push_back(Pair("hex", hex)); + } else ERR_RESULT("couldnt convert tokens"); + } else { + ERR_RESULT("amount must be positive"); + } + return(result); +} + UniValue tokenbid(const UniValue& params, bool fHelp) { UniValue result(UniValue::VOBJ); int64_t bidamount,numtokens; std::string hex; double price; uint256 tokenid;