AssetXXX() changed to GetTokenBalance, TokenInfo, TokenList
added processing of incorrect tokenid to GetTokenBalance
This commit is contained in:
@@ -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<uint8_t> destpubkey,int64_t total);
|
||||
//std::string AssetConvert(int64_t txfee,uint256 assetid,std::vector<uint8_t> destpubkey,int64_t total,int32_t evalcode);
|
||||
|
||||
@@ -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<uint8_t> 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<std::pair<CAddressIndexKey, CAmount> > addressIndex;
|
||||
struct CCcontract_info *cp,C; uint256 txid,hashBlock;
|
||||
CTransaction vintx; std::vector<uint8_t> origpubkey;
|
||||
std::string name,description; char str[65];
|
||||
|
||||
cp = CCinit(&C,EVAL_TOKENS);
|
||||
SetCCtxids(addressIndex,cp->normaladdr);
|
||||
for (std::vector<std::pair<CAddressIndexKey, CAmount> >::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;
|
||||
|
||||
@@ -623,4 +623,72 @@ std::string TokenTransfer(int64_t txfee, uint256 assetid, std::vector<uint8_t> 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<uint8_t> 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<std::pair<CAddressIndexKey, CAmount> > addressIndex;
|
||||
struct CCcontract_info *cp, C; uint256 txid, hashBlock;
|
||||
CTransaction vintx; std::vector<uint8_t> origpubkey;
|
||||
std::string name, description; char str[65];
|
||||
|
||||
cp = CCinit(&C, EVAL_TOKENS);
|
||||
SetCCtxids(addressIndex, cp->normaladdr);
|
||||
for (std::vector<std::pair<CAddressIndexKey, CAmount> >::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);
|
||||
}
|
||||
@@ -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<uint8_t> 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<uint8_t> &origpubkey,std::string &name,std::string &description);
|
||||
|
||||
@@ -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<unsigned char> 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);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user