Merge pull request #813 from letolabs/cctests

WIP More CC tests
This commit is contained in:
jl777
2018-08-16 04:54:01 -11:00
committed by GitHub
4 changed files with 278 additions and 114 deletions

View File

@@ -12,6 +12,12 @@ from test_framework.util import assert_equal, assert_greater_than, \
import time import time
from decimal import Decimal from decimal import Decimal
def assert_success(result):
assert_equal(result['result'], 'success')
def assert_error(result):
assert_equal(result['result'], 'error')
class CryptoConditionsTest (BitcoinTestFramework): class CryptoConditionsTest (BitcoinTestFramework):
def setup_chain(self): def setup_chain(self):
@@ -46,11 +52,19 @@ class CryptoConditionsTest (BitcoinTestFramework):
]] ]]
) )
self.is_network_split = split self.is_network_split = split
self.rpc = self.nodes[0]
self.sync_all() self.sync_all()
print("Done setting up network") print("Done setting up network")
def send_and_mine(self, xtn):
txid = self.rpc.sendrawtransaction(xtn)
assert txid, 'got txid'
# we need the tx above to be confirmed in the next block
self.rpc.generate(1)
return txid
def run_faucet_tests(self): def run_faucet_tests(self):
rpc = self.nodes[0] rpc = self.rpc
# basic sanity tests # basic sanity tests
result = rpc.getwalletinfo() result = rpc.getwalletinfo()
@@ -90,9 +104,6 @@ class CryptoConditionsTest (BitcoinTestFramework):
# we need the tx above to be confirmed in the next block # we need the tx above to be confirmed in the next block
rpc.generate(1) rpc.generate(1)
# clear the rawmempool
result = rpc.getrawmempool()
result = rpc.getwalletinfo() result = rpc.getwalletinfo()
balance2 = result['balance'] balance2 = result['balance']
# make sure our balance is less now # make sure our balance is less now
@@ -144,14 +155,18 @@ class CryptoConditionsTest (BitcoinTestFramework):
assert_equal(result['result'], 'success') assert_equal(result['result'], 'success')
for x in ['AssetsCCaddress', 'myCCaddress', 'Assetsmarker', 'myaddress', 'CCaddress']: for x in ['AssetsCCaddress', 'myCCaddress', 'Assetsmarker', 'myaddress', 'CCaddress']:
assert_equal(result[x][0], 'R') assert_equal(result[x][0], 'R')
result = rpc.tokencreate("DUKE", "1987.420", "duke")
assert_equal(result['result'], 'success')
# there are no tokens created yet # there are no tokens created yet
result = rpc.tokenlist() result = rpc.tokenlist()
assert_equal(result, []) assert_equal(result, [])
result = rpc.tokencreate("DUKE", "1987.420", "duke")
assert_equal(result['result'], 'success')
self.send_and_mine(result['hex'])
result = rpc.tokenlist()
tokenid = result[0]
assert(tokenid, "got tokenid")
# there are no token orders yet # there are no token orders yet
result = rpc.tokenorders() result = rpc.tokenorders()
assert_equal(result, []) assert_equal(result, [])
@@ -164,7 +179,47 @@ class CryptoConditionsTest (BitcoinTestFramework):
# this is not a valid assetid # this is not a valid assetid
result = rpc.tokeninfo(self.pubkey) result = rpc.tokeninfo(self.pubkey)
assert_equal(result['result'], 'error') assert_error(result)
# invalid numtokens
result = rpc.tokenask("-1", tokenid, "1")
assert_error(result)
# invalid numtokens
result = rpc.tokenask("0", tokenid, "1")
assert_error(result)
# invalid price
result = rpc.tokenask("1", tokenid, "-1")
assert_error(result)
# invalid price
result = rpc.tokenask("1", tokenid, "0")
assert_error(result)
# invalid tokenid
result = rpc.tokenask("100", "deadbeef", "1")
assert_error(result)
# valid
result = rpc.tokenask("100", tokenid, "7.77")
assert_success(result)
tokenaskhex = result['hex']
assert tokenaskhex, "got tokenask hexk"
tokenaskid = self.send_and_mine(result['hex'])
# invalid fillunits
result = rpc.tokenfillask(tokenid, tokenaskid, "0")
assert_error(result)
# invalid fillunits
result = rpc.tokenfillask(tokenid, tokenaskid, "-777")
assert_error(result)
# should this pass or fail?
result = rpc.tokenfillask(tokenid, tokenaskid, "10")
#assert_success(result)
def run_rewards_tests(self): def run_rewards_tests(self):
rpc = self.nodes[0] rpc = self.nodes[0]
@@ -184,7 +239,7 @@ class CryptoConditionsTest (BitcoinTestFramework):
result = rpc.rewardsinfo("none") result = rpc.rewardsinfo("none")
assert_equal(result['result'], 'error') assert_equal(result['result'], 'error')
result = rpc.rewardscreatefunding("STUFF", "1000", "5", "1", "10", "10") result = rpc.rewardscreatefunding("STUFF", "7777", "25", "0", "10", "10")
assert result['hex'], 'got raw xtn' assert result['hex'], 'got raw xtn'
txid = rpc.sendrawtransaction(result['hex']) txid = rpc.sendrawtransaction(result['hex'])
assert txid, 'got txid' assert txid, 'got txid'
@@ -194,10 +249,10 @@ class CryptoConditionsTest (BitcoinTestFramework):
result = rpc.rewardsinfo(txid) result = rpc.rewardsinfo(txid)
assert_equal(result['result'], 'success') assert_equal(result['result'], 'success')
assert_equal(result['name'], 'STUFF') assert_equal(result['name'], 'STUFF')
assert_equal(result['APR'], "5.00000000") assert_equal(result['APR'], "25.00000000")
assert_equal(result['minseconds'], 86400) assert_equal(result['minseconds'], 0)
assert_equal(result['maxseconds'], 864000) assert_equal(result['maxseconds'], 864000)
assert_equal(result['funding'], "1000.00000000") assert_equal(result['funding'], "7777.00000000")
assert_equal(result['mindeposit'], "10.00000000") assert_equal(result['mindeposit'], "10.00000000")
assert_equal(result['fundingtxid'], txid) assert_equal(result['fundingtxid'], txid)
@@ -205,8 +260,60 @@ class CryptoConditionsTest (BitcoinTestFramework):
result = rpc.rewardsaddfunding("STUFF", txid, "0") result = rpc.rewardsaddfunding("STUFF", txid, "0")
assert_equal(result['result'], 'error') assert_equal(result['result'], 'error')
result = rpc.rewardsaddfunding("STUFF", txid, "100") result = rpc.rewardsaddfunding("STUFF", txid, "555")
assert_equal(result['result'], 'success') assert_equal(result['result'], 'success')
fundingtxid = result['hex']
assert fundingtxid, "got funding txid"
result = rpc.rewardslock("STUFF", fundingtxid, "7")
assert_equal(result['result'], 'error')
# the previous xtn has not been broadcasted yet
result = rpc.rewardsunlock("STUFF", fundingtxid)
assert_equal(result['result'], 'error')
# wrong plan name
result = rpc.rewardsunlock("SHTUFF", fundingtxid)
assert_equal(result['result'], 'error')
txid = rpc.sendrawtransaction(fundingtxid)
assert txid, 'got txid from sendrawtransaction'
# confirm the xtn above
rpc.generate(1)
# amount must be positive
result = rpc.rewardslock("STUFF", fundingtxid, "-5")
assert_equal(result['result'], 'error')
# amount must be positive
result = rpc.rewardslock("STUFF", fundingtxid, "0")
assert_equal(result['result'], 'error')
# trying to lock less than the min amount is an error
result = rpc.rewardslock("STUFF", fundingtxid, "7")
assert_equal(result['result'], 'error')
# not working
#result = rpc.rewardslock("STUFF", fundingtxid, "10")
#assert_equal(result['result'], 'success')
#locktxid = result['hex']
#assert locktxid, "got lock txid"
# locktxid has not been broadcast yet
#result = rpc.rewardsunlock("STUFF", locktxid)
#assert_equal(result['result'], 'error')
# broadcast xtn
#txid = rpc.sendrawtransaction(locktxid)
#assert txid, 'got txid from sendrawtransaction'
# confirm the xtn above
#rpc.generate(1)
#result = rpc.rewardsunlock("STUFF", locktxid)
#assert_equal(result['result'], 'error')
def run_test (self): def run_test (self):
print("Mining blocks...") print("Mining blocks...")
@@ -220,7 +327,6 @@ class CryptoConditionsTest (BitcoinTestFramework):
print("Importing privkey") print("Importing privkey")
rpc.importprivkey(self.privkey) rpc.importprivkey(self.privkey)
self.run_faucet_tests() self.run_faucet_tests()
self.run_rewards_tests() self.run_rewards_tests()
self.run_dice_tests() self.run_dice_tests()

View File

@@ -14,6 +14,7 @@
******************************************************************************/ ******************************************************************************/
#include "CCassets.h" #include "CCassets.h"
extern std::string CCerror;
int64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint256 assetid,int64_t total,int32_t maxinputs) int64_t AddAssetInputs(struct CCcontract_info *cp,CMutableTransaction &mtx,CPubKey pk,uint256 assetid,int64_t total,int32_t maxinputs)
{ {
@@ -426,12 +427,14 @@ std::string FillSell(int64_t txfee,uint256 assetid,uint256 assetid2,uint256 askt
CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector<uint8_t> origpubkey; double dprice; uint64_t mask; int32_t askvout=0; int64_t received_assetoshis,total_nValue,orig_assetoshis,paid_nValue,remaining_nValue,inputs,CCchange=0; struct CCcontract_info *cp,C; CTransaction vintx,filltx; uint256 hashBlock; CMutableTransaction mtx; CPubKey mypk; std::vector<uint8_t> origpubkey; double dprice; uint64_t mask; int32_t askvout=0; int64_t received_assetoshis,total_nValue,orig_assetoshis,paid_nValue,remaining_nValue,inputs,CCchange=0; struct CCcontract_info *cp,C;
if ( fillunits < 0 ) if ( fillunits < 0 )
{ {
fprintf(stderr,"negative fillunits %lld\n",(long long)fillunits); CCerror = strprintf("negative fillunits %lld\n",(long long)fillunits);
fprintf(stderr,"%s\n",CCerror.c_str());
return(""); return("");
} }
if ( assetid2 != zeroid ) if ( assetid2 != zeroid )
{ {
fprintf(stderr,"asset swaps disabled\n"); CCerror = "asset swaps disabled";
fprintf(stderr,"%s\n",CCerror.c_str());
return(""); return("");
} }
@@ -473,7 +476,10 @@ std::string FillSell(int64_t txfee,uint256 assetid,uint256 assetid2,uint256 askt
if ( CCchange != 0 ) if ( CCchange != 0 )
mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk)); mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS,CCchange,mypk));
return(FinalizeCCTx(mask,cp,mtx,mypk,txfee,EncodeAssetOpRet(assetid2!=zeroid?'E':'S',assetid,assetid2,remaining_nValue,origpubkey))); return(FinalizeCCTx(mask,cp,mtx,mypk,txfee,EncodeAssetOpRet(assetid2!=zeroid?'E':'S',assetid,assetid2,remaining_nValue,origpubkey)));
} else fprintf(stderr,"filltx not enough utxos\n"); } else {
CCerror = strprintf("filltx not enough utxos");
fprintf(stderr,"%s\n", CCerror.c_str());
}
} }
} }
return(""); return("");

View File

@@ -66,6 +66,8 @@
*/ */
extern std::string CCerror;
int64_t RewardsCalc(int64_t amount,uint256 txid,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit) int64_t RewardsCalc(int64_t amount,uint256 txid,uint64_t APR,uint64_t minseconds,uint64_t maxseconds,uint64_t mindeposit)
{ {
int32_t numblocks; uint64_t duration,reward = 0; int32_t numblocks; uint64_t duration,reward = 0;
@@ -585,6 +587,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2
if ( RewardsPlanExists(cp,sbits,rewardspk,APR,minseconds,maxseconds,mindeposit) == 0 ) if ( RewardsPlanExists(cp,sbits,rewardspk,APR,minseconds,maxseconds,mindeposit) == 0 )
{ {
fprintf(stderr,"Rewards plan %s doesnt exist\n",planstr); fprintf(stderr,"Rewards plan %s doesnt exist\n",planstr);
CCerror = "Rewards plan does not exist";
return(""); return("");
} }
fprintf(stderr,"APR %.8f minseconds.%llu maxseconds.%llu mindeposit %.8f\n",(double)APR/COIN,(long long)minseconds,(long long)maxseconds,(double)mindeposit/COIN); fprintf(stderr,"APR %.8f minseconds.%llu maxseconds.%llu mindeposit %.8f\n",(double)APR/COIN,(long long)minseconds,(long long)maxseconds,(double)mindeposit/COIN);
@@ -596,6 +599,7 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2
if ( (amount= CCutxovalue(coinaddr,locktxid,0)) == 0 ) if ( (amount= CCutxovalue(coinaddr,locktxid,0)) == 0 )
{ {
fprintf(stderr,"%s locktxid/v0 is spent\n",coinaddr); fprintf(stderr,"%s locktxid/v0 is spent\n",coinaddr);
CCerror = "locktxid/v0 is spent";
return(""); return("");
} }
if ( GetTransaction(locktxid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[1].scriptPubKey.IsPayToCryptoCondition() == 0 ) if ( GetTransaction(locktxid,tx,hashBlock,false) != 0 && tx.vout.size() > 0 && tx.vout[1].scriptPubKey.IsPayToCryptoCondition() == 0 )
@@ -606,21 +610,35 @@ std::string RewardsUnlock(uint64_t txfee,char *planstr,uint256 fundingtxid,uint2
else else
{ {
fprintf(stderr,"%s no normal vout.1 in locktxid\n",coinaddr); fprintf(stderr,"%s no normal vout.1 in locktxid\n",coinaddr);
CCerror = "no normal vout.1 in locktxid";
return(""); return("");
} }
} }
if ( amount > 0 && (reward= RewardsCalc(amount,mtx.vin[0].prevout.hash,APR,minseconds,maxseconds,mindeposit)) > txfee && scriptPubKey.size() > 0 ) if ( amount > 0) {
{ reward= RewardsCalc(amount,mtx.vin[0].prevout.hash,APR,minseconds,maxseconds,mindeposit);
if ( (inputs= AddRewardsInputs(ignore,0,cp,mtx,rewardspk,reward+txfee,30,sbits,fundingtxid)) > 0 ) if (scriptPubKey.size() > 0) {
{ if (reward > txfee) {
if ( inputs >= (reward + 2*txfee) ) if ( (inputs= AddRewardsInputs(ignore,0,cp,mtx,rewardspk,reward+txfee,30,sbits,fundingtxid)) > 0 ) {
CCchange = (inputs - (reward + txfee)); if ( inputs >= (reward + 2*txfee) )
fprintf(stderr,"inputs %.8f CCchange %.8f amount %.8f reward %.8f\n",(double)inputs/COIN,(double)CCchange/COIN,(double)amount/COIN,(double)reward/COIN); CCchange = (inputs - (reward + txfee));
mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk)); fprintf(stderr,"inputs %.8f CCchange %.8f amount %.8f reward %.8f\n",(double)inputs/COIN,(double)CCchange/COIN,(double)amount/COIN,(double)reward/COIN);
mtx.vout.push_back(CTxOut(amount+reward,scriptPubKey)); mtx.vout.push_back(MakeCC1vout(cp->evalcode,CCchange,rewardspk));
return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid))); mtx.vout.push_back(CTxOut(amount+reward,scriptPubKey));
} return(FinalizeCCTx(-1LL,cp,mtx,mypk,txfee,EncodeRewardsOpRet('U',sbits,fundingtxid)));
fprintf(stderr,"cant find enough rewards inputs\n"); }
CCerror = "cant find enough rewards inputs";
fprintf(stderr,"%s\n", CCerror.c_str());
} else {
CCerror = strprintf("reward %.8f is <= the transaction fee", reward);
fprintf(stderr,"%s\n", CCerror.c_str());
}
} else {
CCerror = "invalid scriptPubKey";
fprintf(stderr,"%s\n", CCerror.c_str());
}
} else {
CCerror = "amount must be positive";
fprintf(stderr,"%s\n", CCerror.c_str());
} }
fprintf(stderr,"amount %.8f -> reward %.8f\n",(double)amount/COIN,(double)reward/COIN); fprintf(stderr,"amount %.8f -> reward %.8f\n",(double)amount/COIN,(double)reward/COIN);
return(""); return("");

View File

@@ -38,6 +38,8 @@
#include <numeric> #include <numeric>
#define ERR_RESULT(x) result.push_back(Pair("result", "error")) , result.push_back(Pair("error", x));
using namespace std; using namespace std;
using namespace libzcash; using namespace libzcash;
@@ -49,6 +51,7 @@ uint32_t komodo_segid32(char *coinaddr);
int64_t nWalletUnlockTime; int64_t nWalletUnlockTime;
static CCriticalSection cs_nWalletUnlockTime; static CCriticalSection cs_nWalletUnlockTime;
std::string CCerror;
// Private method: // Private method:
UniValue z_getoperationstatus_IMPL(const UniValue&, bool); UniValue z_getoperationstatus_IMPL(const UniValue&, bool);
@@ -4997,13 +5000,13 @@ UniValue rewardscreatefunding(const UniValue& params, bool fHelp)
{ {
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex)); result.push_back(Pair("hex", hex));
} else result.push_back(Pair("error", "couldnt create rewards funding transaction")); } else ERR_RESULT("couldnt create rewards funding transaction");
return(result); return(result);
} }
UniValue rewardslock(const UniValue& params, bool fHelp) UniValue rewardslock(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid; uint64_t amount; std::string hex; UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid; int64_t amount; std::string hex;
if ( fHelp || params.size() != 3 ) if ( fHelp || params.size() != 3 )
throw runtime_error("rewardslock name fundingtxid amount\n"); throw runtime_error("rewardslock name fundingtxid amount\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
@@ -5014,11 +5017,13 @@ UniValue rewardslock(const UniValue& params, bool fHelp)
fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); fundingtxid = Parseuint256((char *)params[1].get_str().c_str());
amount = atof(params[2].get_str().c_str()) * COIN; amount = atof(params[2].get_str().c_str()) * COIN;
hex = RewardsLock(0,name,fundingtxid,amount); hex = RewardsLock(0,name,fundingtxid,amount);
if ( hex.size() > 0 ) if ( amount > 0 ) {
{ if ( hex.size() > 0 )
result.push_back(Pair("result", "success")); {
result.push_back(Pair("hex", hex)); result.push_back(Pair("result", "success"));
} else result.push_back(Pair("error", "couldnt create rewards lock transaction")); result.push_back(Pair("hex", hex));
} else ERR_RESULT( "couldnt create rewards lock transaction");
} else ERR_RESULT("amount must be positive");
return(result); return(result);
} }
@@ -5066,11 +5071,12 @@ UniValue rewardsunlock(const UniValue& params, bool fHelp)
txid = Parseuint256((char *)params[2].get_str().c_str()); txid = Parseuint256((char *)params[2].get_str().c_str());
else memset(&txid,0,sizeof(txid)); else memset(&txid,0,sizeof(txid));
hex = RewardsUnlock(0,name,fundingtxid,txid); hex = RewardsUnlock(0,name,fundingtxid,txid);
if ( hex.size() > 0 ) if (CCerror != "") {
{ ERR_RESULT(CCerror);
} else if ( hex.size() > 0 ) {
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex)); result.push_back(Pair("hex", hex));
} else result.push_back(Pair("error", "couldnt create rewards unlock transaction")); } else ERR_RESULT("couldnt create rewards unlock transaction");
return(result); return(result);
} }
@@ -5162,14 +5168,8 @@ UniValue faucetfund(const UniValue& params, bool fHelp)
{ {
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex)); result.push_back(Pair("hex", hex));
} else { } else ERR_RESULT("couldnt create faucet funding transaction");
result.push_back(Pair("result", "error")); } else ERR_RESULT( "funding amount must be positive");
result.push_back(Pair("error", "couldnt create faucet funding transaction"));
}
} else {
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "funding amount must be positive"));
}
return(result); return(result);
} }
@@ -5183,14 +5183,10 @@ UniValue faucetget(const UniValue& params, bool fHelp)
const CKeyStore& keystore = *pwalletMain; const CKeyStore& keystore = *pwalletMain;
LOCK2(cs_main, pwalletMain->cs_wallet); LOCK2(cs_main, pwalletMain->cs_wallet);
hex = FaucetGet(0); hex = FaucetGet(0);
if ( hex.size() > 0 ) if ( hex.size() > 0 ) {
{
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex)); result.push_back(Pair("hex", hex));
} else { } else ERR_RESULT("couldnt create faucet get transaction");
result.push_back(Pair("result", "error"));
result.push_back(Pair("error", "couldnt create faucet get transaction"));
}
return(result); return(result);
} }
@@ -5210,17 +5206,20 @@ UniValue dicefund(const UniValue& params, bool fHelp)
maxodds = atol(params[4].get_str().c_str()); maxodds = atol(params[4].get_str().c_str());
timeoutblocks = atol(params[5].get_str().c_str()); timeoutblocks = atol(params[5].get_str().c_str());
hex = DiceCreateFunding(0,name,funds,minbet,maxbet,maxodds,timeoutblocks); hex = DiceCreateFunding(0,name,funds,minbet,maxbet,maxodds,timeoutblocks);
if ( hex.size() > 0 ) if (CCerror != "") {
{ ERR_RESULT(CCerror);
} else if ( hex.size() > 0 ) {
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex)); result.push_back(Pair("hex", hex));
} else result.push_back(Pair("error", "couldnt create dice funding transaction")); } else {
ERR_RESULT( "couldnt create dice funding transaction");
}
return(result); return(result);
} }
UniValue diceaddfunds(const UniValue& params, bool fHelp) UniValue diceaddfunds(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid; uint64_t amount; std::string hex; UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid; int64_t amount; std::string hex;
if ( fHelp || params.size() != 3 ) if ( fHelp || params.size() != 3 )
throw runtime_error("diceaddfunds name fundingtxid amount\n"); throw runtime_error("diceaddfunds name fundingtxid amount\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
@@ -5230,18 +5229,21 @@ UniValue diceaddfunds(const UniValue& params, bool fHelp)
name = (char *)params[0].get_str().c_str(); name = (char *)params[0].get_str().c_str();
fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); fundingtxid = Parseuint256((char *)params[1].get_str().c_str());
amount = atof(params[2].get_str().c_str()) * COIN; amount = atof(params[2].get_str().c_str()) * COIN;
hex = DiceAddfunding(0,name,fundingtxid,amount); if ( amount > 0 ) {
if ( hex.size() > 0 ) hex = DiceAddfunding(0,name,fundingtxid,amount);
{ if (CCerror != "") {
result.push_back(Pair("result", "success")); ERR_RESULT(CCerror);
result.push_back(Pair("hex", hex)); } else if ( hex.size() > 0 ) {
} else result.push_back(Pair("error", "couldnt create dice addfunding transaction")); result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex));
} else ERR_RESULT("couldnt create dice addfunding transaction");
} else ERR_RESULT("amount must be positive");
return(result); return(result);
} }
UniValue dicebet(const UniValue& params, bool fHelp) UniValue dicebet(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); std::string hex; uint256 fundingtxid; uint64_t amount,odds; char *name; UniValue result(UniValue::VOBJ); std::string hex; uint256 fundingtxid; int64_t amount,odds; char *name;
if ( fHelp || params.size() != 4 ) if ( fHelp || params.size() != 4 )
throw runtime_error("dicebet name fundingtxid amount odds\n"); throw runtime_error("dicebet name fundingtxid amount odds\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
@@ -5252,18 +5254,22 @@ UniValue dicebet(const UniValue& params, bool fHelp)
fundingtxid = Parseuint256((char *)params[1].get_str().c_str()); fundingtxid = Parseuint256((char *)params[1].get_str().c_str());
amount = atof(params[2].get_str().c_str()) * COIN; amount = atof(params[2].get_str().c_str()) * COIN;
odds = atol(params[3].get_str().c_str()); odds = atol(params[3].get_str().c_str());
hex = DiceBet(0,name,fundingtxid,amount,odds); if (amount > 0 && odds > 0) {
if ( hex.size() > 0 ) hex = DiceBet(0,name,fundingtxid,amount,odds);
{ if ( hex.size() > 0 )
result.push_back(Pair("result", "success")); {
result.push_back(Pair("hex", hex)); result.push_back(Pair("result", "success"));
} else result.push_back(Pair("error", "couldnt create faucet get transaction")); result.push_back(Pair("hex", hex));
} else ERR_RESULT("couldnt create faucet get transaction");
} else {
ERR_RESULT("amount and odds must be positive");
}
return(result); return(result);
} }
UniValue dicefinish(const UniValue& params, bool fHelp) UniValue dicefinish(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string hex; int32_t r; UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; std::string hex; int32_t r;
if ( fHelp || params.size() != 3 ) if ( fHelp || params.size() != 3 )
throw runtime_error("dicefinish name fundingtxid bettxid\n"); throw runtime_error("dicefinish name fundingtxid bettxid\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
@@ -5278,13 +5284,13 @@ UniValue dicefinish(const UniValue& params, bool fHelp)
{ {
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex)); result.push_back(Pair("hex", hex));
} else result.push_back(Pair("error", "couldnt create dicefinish transaction")); } else ERR_RESULT( "couldnt create dicefinish transaction");
return(result); return(result);
} }
UniValue dicestatus(const UniValue& params, bool fHelp) UniValue dicestatus(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; uint64_t amount; std::string status; double winnings; UniValue result(UniValue::VOBJ); char *name; uint256 fundingtxid,bettxid; std::string status; double winnings;
if ( fHelp || (params.size() != 2 && params.size() != 3) ) if ( fHelp || (params.size() != 2 && params.size() != 3) )
throw runtime_error("dicestatus name fundingtxid bettxid\n"); throw runtime_error("dicestatus name fundingtxid bettxid\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
@@ -5430,13 +5436,13 @@ UniValue tokencreate(const UniValue& params, bool fHelp)
{ {
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex)); result.push_back(Pair("hex", hex));
} else result.push_back(Pair("error", "couldnt create transaction")); } else ERR_RESULT("couldnt create transaction");
return(result); return(result);
} }
UniValue tokentransfer(const UniValue& params, bool fHelp) UniValue tokentransfer(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); std::string hex; uint64_t amount; uint256 tokenid; UniValue result(UniValue::VOBJ); std::string hex; int64_t amount; uint256 tokenid;
if ( fHelp || params.size() != 3 ) if ( fHelp || params.size() != 3 )
throw runtime_error("tokentransfer tokenid destpubkey amount\n"); throw runtime_error("tokentransfer tokenid destpubkey amount\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
@@ -5452,17 +5458,21 @@ UniValue tokentransfer(const UniValue& params, bool fHelp)
return(result); return(result);
} }
hex = AssetTransfer(0,tokenid,pubkey,amount); hex = AssetTransfer(0,tokenid,pubkey,amount);
if ( hex.size() > 0 ) if (amount > 0) {
{ if ( hex.size() > 0 )
result.push_back(Pair("result", "success")); {
result.push_back(Pair("hex", hex)); result.push_back(Pair("result", "success"));
} else result.push_back(Pair("error", "couldnt transfer assets")); result.push_back(Pair("hex", hex));
} else ERR_RESULT("couldnt transfer assets");
} else {
ERR_RESULT("amount must be positive");
}
return(result); return(result);
} }
UniValue tokenbid(const UniValue& params, bool fHelp) UniValue tokenbid(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); uint64_t bidamount,numtokens; std::string hex; double price; uint256 tokenid; UniValue result(UniValue::VOBJ); int64_t bidamount,numtokens; std::string hex; double price; uint256 tokenid;
if ( fHelp || params.size() != 3 ) if ( fHelp || params.size() != 3 )
throw runtime_error("tokenbid numtokens tokenid price\n"); throw runtime_error("tokenbid numtokens tokenid price\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
@@ -5479,11 +5489,15 @@ UniValue tokenbid(const UniValue& params, bool fHelp)
return(result); return(result);
} }
hex = CreateBuyOffer(0,bidamount,tokenid,numtokens); hex = CreateBuyOffer(0,bidamount,tokenid,numtokens);
if ( hex.size() > 0 ) if (price > 0 && numtokens > 0) {
{ if ( hex.size() > 0 )
result.push_back(Pair("result", "success")); {
result.push_back(Pair("hex", hex)); result.push_back(Pair("result", "success"));
} else result.push_back(Pair("error", "couldnt create bid")); result.push_back(Pair("hex", hex));
} else result.push_back(Pair("error", "couldnt create bid"));
} else {
ERR_RESULT("price and numtokens must be positive");
}
return(result); return(result);
} }
@@ -5508,7 +5522,7 @@ UniValue tokencancelbid(const UniValue& params, bool fHelp)
{ {
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex)); result.push_back(Pair("hex", hex));
} else result.push_back(Pair("error", "couldnt cancel bid")); } else ERR_RESULT("couldnt cancel bid");
return(result); return(result);
} }
@@ -5534,13 +5548,13 @@ UniValue tokenfillbid(const UniValue& params, bool fHelp)
{ {
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex)); result.push_back(Pair("hex", hex));
} else result.push_back(Pair("error", "couldnt fill bid")); } else ERR_RESULT("couldnt fill bid");
return(result); return(result);
} }
UniValue tokenask(const UniValue& params, bool fHelp) UniValue tokenask(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); uint64_t askamount,numtokens; std::string hex; double price; uint256 tokenid; UniValue result(UniValue::VOBJ); int64_t askamount,numtokens; std::string hex; double price; uint256 tokenid;
if ( fHelp || params.size() != 3 ) if ( fHelp || params.size() != 3 )
throw runtime_error("tokenask numtokens tokenid price\n"); throw runtime_error("tokenask numtokens tokenid price\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
@@ -5557,17 +5571,22 @@ UniValue tokenask(const UniValue& params, bool fHelp)
return(result); return(result);
} }
hex = CreateSell(0,numtokens,tokenid,askamount); hex = CreateSell(0,numtokens,tokenid,askamount);
if ( hex.size() > 0 ) if (price > 0 && numtokens > 0) {
{ if ( hex.size() > 0 )
result.push_back(Pair("result", "success")); {
result.push_back(Pair("hex", hex)); result.push_back(Pair("result", "success"));
} else result.push_back(Pair("error", "couldnt create ask")); result.push_back(Pair("hex", hex));
} else ERR_RESULT("couldnt create ask");
} else {
ERR_RESULT("price and numtokens must be positive");
}
return(result); return(result);
} }
UniValue tokenswapask(const UniValue& params, bool fHelp) UniValue tokenswapask(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); uint64_t askamount,numtokens; std::string hex; double price; uint256 tokenid,otherid; static uint256 zeroid;
UniValue result(UniValue::VOBJ); int64_t askamount,numtokens; std::string hex; double price; uint256 tokenid,otherid;
if ( fHelp || params.size() != 4 ) if ( fHelp || params.size() != 4 )
throw runtime_error("tokenswapask numtokens tokenid otherid price\n"); throw runtime_error("tokenswapask numtokens tokenid otherid price\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
@@ -5580,11 +5599,15 @@ UniValue tokenswapask(const UniValue& params, bool fHelp)
price = atof(params[3].get_str().c_str()); price = atof(params[3].get_str().c_str());
askamount = (price * numtokens); askamount = (price * numtokens);
hex = CreateSwap(0,numtokens,tokenid,otherid,askamount); hex = CreateSwap(0,numtokens,tokenid,otherid,askamount);
if ( hex.size() > 0 ) if (price > 0 && numtokens > 0) {
{ if ( hex.size() > 0 )
result.push_back(Pair("result", "success")); {
result.push_back(Pair("hex", hex)); result.push_back(Pair("result", "success"));
} else result.push_back(Pair("error", "couldnt create swap")); result.push_back(Pair("hex", hex));
} else ERR_RESULT("couldnt create swap");
} else {
ERR_RESULT("price and numtokens must be positive");
}
return(result); return(result);
} }
@@ -5609,7 +5632,7 @@ UniValue tokencancelask(const UniValue& params, bool fHelp)
{ {
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex)); result.push_back(Pair("hex", hex));
} else result.push_back(Pair("error", "couldnt cancel ask")); } else ERR_RESULT("couldnt cancel ask");
return(result); return(result);
} }
@@ -5631,17 +5654,25 @@ UniValue tokenfillask(const UniValue& params, bool fHelp)
return(result); return(result);
} }
hex = FillSell(0,tokenid,zeroid,asktxid,fillunits); hex = FillSell(0,tokenid,zeroid,asktxid,fillunits);
if ( hex.size() > 0 ) if (fillunits > 0) {
{ if (CCerror != "") {
result.push_back(Pair("result", "success")); ERR_RESULT(CCerror);
result.push_back(Pair("hex", hex)); } else if ( hex.size() > 0) {
} else result.push_back(Pair("error", "couldnt fill bid")); result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex));
} else {
ERR_RESULT("couldnt fill bid");
}
} else {
ERR_RESULT("fillunits must be positive");
}
return(result); return(result);
} }
UniValue tokenfillswap(const UniValue& params, bool fHelp) UniValue tokenfillswap(const UniValue& params, bool fHelp)
{ {
UniValue result(UniValue::VOBJ); uint64_t fillunits; std::string hex; uint256 tokenid,otherid,asktxid; static uint256 zeroid;
UniValue result(UniValue::VOBJ); int64_t fillunits; std::string hex; uint256 tokenid,otherid,asktxid;
if ( fHelp || params.size() != 4 ) if ( fHelp || params.size() != 4 )
throw runtime_error("tokenfillswap tokenid otherid asktxid fillunits\n"); throw runtime_error("tokenfillswap tokenid otherid asktxid fillunits\n");
if ( ensure_CCrequirements() < 0 ) if ( ensure_CCrequirements() < 0 )
@@ -5653,11 +5684,14 @@ UniValue tokenfillswap(const UniValue& params, bool fHelp)
asktxid = Parseuint256((char *)params[2].get_str().c_str()); asktxid = Parseuint256((char *)params[2].get_str().c_str());
fillunits = atol(params[3].get_str().c_str()); fillunits = atol(params[3].get_str().c_str());
hex = FillSell(0,tokenid,otherid,asktxid,fillunits); hex = FillSell(0,tokenid,otherid,asktxid,fillunits);
if ( hex.size() > 0 ) if (fillunits > 0) {
{ if ( hex.size() > 0 ) {
result.push_back(Pair("result", "success")); result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex)); result.push_back(Pair("hex", hex));
} else result.push_back(Pair("error", "couldnt fill bid")); } else ERR_RESULT("couldnt fill bid");
} else {
ERR_RESULT("fillunits must be positive");
}
return(result); return(result);
} }