From fff7c5d24989259c62ba7097128489a54029e607 Mon Sep 17 00:00:00 2001 From: jl777 Date: Thu, 26 Jul 2018 06:42:17 -1100 Subject: [PATCH] Swap functions --- src/cc/CCassets.h | 3 ++- src/cc/CCassetstx.cpp | 35 +++++++++++++++++++++++++++++++++-- src/wallet/rpcwallet.cpp | 7 +++---- 3 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/cc/CCassets.h b/src/cc/CCassets.h index 3a3df3680..187dc4219 100644 --- a/src/cc/CCassets.h +++ b/src/cc/CCassets.h @@ -54,7 +54,8 @@ std::string AssetTransfer(uint64_t txfee,uint256 assetid,std::vector de std::string CreateBuyOffer(uint64_t txfee,uint64_t bidamount,uint256 assetid,uint64_t pricetotal); std::string CancelBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid); std::string FillBuyOffer(uint64_t txfee,uint256 assetid,uint256 bidtxid,uint64_t fillamount); -std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 assetid2,uint64_t pricetotal); +std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint64_t pricetotal); +std::string CreateSwap(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 assetid2,uint64_t pricetotal); std::string CancelSell(uint64_t txfee,uint256 assetid,uint256 asktxid); std::string FillSell(uint64_t txfee,uint256 assetid,uint256 assetid2,uint256 asktxid,uint64_t fillamount); diff --git a/src/cc/CCassetstx.cpp b/src/cc/CCassetstx.cpp index efdf30299..dce1efd53 100644 --- a/src/cc/CCassetstx.cpp +++ b/src/cc/CCassetstx.cpp @@ -198,7 +198,7 @@ std::string CreateBuyOffer(uint64_t txfee,uint64_t bidamount,uint256 assetid,uin return(0); } -std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 assetid2,uint64_t pricetotal) +std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint64_t pricetotal) { CMutableTransaction mtx; CPubKey mypk; uint64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; cp = CCinit(&C,EVAL_ASSETS); @@ -209,6 +209,34 @@ std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 { if ( (inputs= AddAssetInputs(cp,mtx,mypk,assetid,askamount,60)) > 0 ) { + if ( inputs < askamount ) + askamount = inputs; + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount,GetUnspendable(cp,0))); + if ( inputs > askamount ) + CCchange = (inputs - askamount); + if ( CCchange != 0 ) + mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); + opret = EncodeAssetOpRet('s',assetid,zeroid,pricetotal,Mypubkey()); + return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); + } else fprintf(stderr,"need some assets to place ask\n"); + } + fprintf(stderr,"need some native coins to place ask\n"); + return(0); +} + +std::string CreateSwap(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 assetid2,uint64_t pricetotal) +{ + CMutableTransaction mtx; CPubKey mypk; uint64_t inputs,CCchange; CScript opret; struct CCcontract_info *cp,C; + 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,askamount,60)) > 0 ) + { + if ( inputs < askamount ) + askamount = inputs; mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,askamount,GetUnspendable(cp,0))); if ( inputs > askamount ) CCchange = (inputs - askamount); @@ -216,7 +244,10 @@ std::string CreateSell(uint64_t txfee,uint64_t askamount,uint256 assetid,uint256 mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); if ( assetid2 == zeroid ) opret = EncodeAssetOpRet('s',assetid,zeroid,pricetotal,Mypubkey()); - else opret = EncodeAssetOpRet('e',assetid,assetid2,pricetotal,Mypubkey()); + else + { + opret = EncodeAssetOpRet('e',assetid,assetid2,pricetotal,Mypubkey()); + } return(FinalizeCCTx(cp,mtx,mypk,txfee,opret)); } else fprintf(stderr,"need some assets to place ask\n"); } diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 210055d42..f2811260b 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -5254,7 +5254,6 @@ UniValue tokenfillbid(const UniValue& params, bool fHelp) UniValue tokenask(const UniValue& params, bool fHelp) { - static uint256 zeroid; UniValue result(UniValue::VOBJ); uint64_t askamount,numtokens; std::string hex; double price; uint256 tokenid; if ( fHelp || params.size() != 3 ) throw runtime_error("tokenask numtokens tokenid price\n"); @@ -5264,7 +5263,7 @@ UniValue tokenask(const UniValue& params, bool fHelp) tokenid = Parseuint256((char *)params[1].get_str().c_str()); price = atof(params[2].get_str().c_str()); askamount = (price * numtokens) * COIN + 0.0000000049999; - hex = CreateSell(0,numtokens,tokenid,zeroid,askamount); + hex = CreateSell(0,numtokens,tokenid,askamount); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success")); @@ -5285,8 +5284,8 @@ UniValue tokenswapask(const UniValue& params, bool fHelp) tokenid = Parseuint256((char *)params[1].get_str().c_str()); otherid = Parseuint256((char *)params[2].get_str().c_str()); price = atof(params[3].get_str().c_str()); - askamount = (price * numtokens) * COIN + 0.0000000049999; - hex = CreateSell(0,numtokens,tokenid,otherid,askamount); + askamount = (price * numtokens); + hex = CreateSwap(0,numtokens,tokenid,otherid,askamount); if ( hex.size() > 0 ) { result.push_back(Pair("result", "success"));