Split Asset contract into Tokens and Assets

This commit is contained in:
dimxy
2019-01-07 23:19:47 +05:00
parent 4582a08461
commit 8e8a94404f
17 changed files with 3591 additions and 591 deletions

View File

@@ -5417,27 +5417,145 @@ UniValue gatewaysaddress(const UniValue& params, bool fHelp)
UniValue heiraddress(const UniValue& params, bool fHelp)
{
struct CCcontract_info *cp,C; std::vector<unsigned char> destPubkey;
/*
struct CCcontract_info *cp,C; std::vector<unsigned char> pubkey;
cp = CCinit(&C,EVAL_HEIR);
if ( fHelp || params.size() > 1 )
throw runtime_error("heiraddress [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");
if ( params.size() == 1 )
pubkey = ParseHex(params[0].get_str().c_str());
return(CCaddress(cp,(char *)"Heir",pubkey));
*/
cp = CCinit(&C,EVAL_HEIR);
if ( fHelp || (params.size() != 4 && params.size() != 3))
throw runtime_error("heiraddress func txid amount [destpubkey]\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");
//if ( params.size() == 1 )
// pubkey = ParseHex(params[0].get_str().c_str());
char funcid = ((char *)params[0].get_str().c_str())[0];
uint256 assetid = Parseuint256((char *)params[1].get_str().c_str());
int64_t funds = atof(params[2].get_str().c_str()) * COIN ;
if(params.size() == 4)
destPubkey = ParseHex(params[3].get_str().c_str());
// make fake token tx:
struct CCcontract_info *cp, C;
//return HeirFundBad(funcid, assetid, funds, destPubkey);
if (fHelp || (params.size() < 1))
throw runtime_error("heiraddress A|G|H|T|R assetid|destpubkey amountcoins [heirpubkey] [fundingtxid]\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(CCaddress(cp,(char *)"Heir",destPubkey));
char badKind = ((char *)params[0].get_str().c_str())[0];
if (badKind == 'A') {
std::vector<unsigned char> destPubkey;
if (params.size() == 2) {
cp = CCinit(&C, EVAL_HEIR);
destPubkey = ParseHex(params[1].get_str().c_str());
return(CCaddress(cp, (char *)"Heir", destPubkey));
}
else
return std::string("bad params for A");
}
CPubKey myPubkey = pubkey2pk(Mypubkey());
CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), komodo_nextheight());;
if (badKind != 'R') {
if (badKind == 'G' && params.size() != 3)
return std::string("incorrect params for G");
if (badKind == 'H' && params.size() != 5)
return std::string("incorrect params for H, = 5");
if (badKind == 'T' && params.size() != 3)
return std::string("incorrect params for T, = 3");
uint256 assetid = Parseuint256((char *)params[1].get_str().c_str());
int64_t amount = atof(params[2].get_str().c_str()) * COIN;
uint256 fundingtxid;
CPubKey heirPubkey;
if (badKind == 'H') {
std::vector<unsigned char> heirPubkeyStr = ParseHex(params[3].get_str().c_str());
heirPubkey = pubkey2pk(heirPubkeyStr);
fundingtxid = Parseuint256((char *)params[4].get_str().c_str());
}
int64_t txfee = 10000;
uint8_t evalCodeInOpret = (badKind == 'H') ? EVAL_HEIR : EVAL_GATEWAYS;
cp = CCinit(&C, EVAL_ASSETS);
int64_t normalInputs = AddNormalinputs(mtx, myPubkey, txfee + amount, 60);
// int64_t ccInputs = 0;
if (badKind == 'T') {
// just empty fake token
mtx.vout.push_back(MakeCC1vout(EVAL_ASSETS, amount, myPubkey)); //note you need destPubkey for sending to gateways
}
else if (badKind == 'H') {
// heir add funding tx
mtx.vout.push_back(MakeCC1of2vout(EVAL_ASSETS, amount, myPubkey, heirPubkey)); // add cryptocondition to spend amount for either pk
}
else { // if (badKind == 'G')
CPubKey gatewayContractPubKey = GetUnspendable(cp, 0);
mtx.vout.push_back(CTxOut(txfee, CScript() << ParseHex(HexStr(gatewayContractPubKey)) << OP_CHECKSIG));
}
int64_t change = (normalInputs - amount);
if (change != 0) {
mtx.vout.push_back(CTxOut(change, CScript() << ParseHex(HexStr(myPubkey)) << OP_CHECKSIG));
}
std::cerr << "make fake token for contract=" << badKind << " added normalInputs=" << normalInputs << " change=" << change << std::endl;
// note: it sets eval=EVAL_ASSETS in opreturn both for tokens and gateways cc addr
//script = EncodeAssetOpRet('t', assetid, zeroid, 0, (badKind == 'A' ? Mypubkey() : destPubkey)); // dimxy: are we sure about destPubkey here? it may be just pubkey of the author...
CScript opret;
assetid = revuint256(assetid);
fundingtxid = revuint256(fundingtxid);
if (badKind == 'T')
opret << OP_RETURN << E_MARSHAL(ss << (uint8_t)evalCodeInOpret << (uint8_t)'t' << assetid );
else if (badKind == 'H')
opret << OP_RETURN << E_MARSHAL(ss << (uint8_t)evalCodeInOpret << (uint8_t)'t' << assetid << (uint8_t)'A' << myPubkey << heirPubkey << (int64_t)120 << fundingtxid);
else
opret << OP_RETURN << E_MARSHAL(ss << (uint8_t)evalCodeInOpret << (uint8_t)'t' << assetid); // EVAL_GATEWAYS
return(FinalizeCCTx(0, cp, mtx, myPubkey, txfee, opret)); // normal change added here
}
else
{
// move vout from user cc addr to gateways unspendable
CTransaction srctx;
uint256 hashBlock;
const bool allowSlow = false;
uint256 srctxid = Parseuint256((char *)params[1].get_str().c_str());
struct CCcontract_info *cp, C;
cp = CCinit(&C, EVAL_GATEWAYS);
uint64_t txfee = 10000;
CPubKey gatewayspk = GetUnspendable(cp, 0);
if (myGetTransaction(srctxid, srctx, hashBlock) && srctx.vout.size() > 0) {
int64_t normalInputs = AddNormalinputs(mtx, myPubkey, 2 * txfee, 4);
mtx.vin.push_back(CTxIn(srctxid, 2, CScript())); // repaired src tx
mtx.vout.push_back(MakeCC1vout(EVAL_GATEWAYS, srctx.vout[2].nValue, gatewayspk));
CScript script = srctx.vout[srctx.vout.size() - 1].scriptPubKey;
//mtx.fOverwintered = true;
return(FinalizeCCTx(0, cp, mtx, myPubkey, txfee, script)); // normal change added here
}
}
return std::string("there has been some error");
}
UniValue lottoaddress(const UniValue& params, bool fHelp)
{
struct CCcontract_info *cp,C; std::vector<unsigned char> pubkey;
@@ -5518,17 +5636,30 @@ UniValue rewardsaddress(const UniValue& params, bool fHelp)
return(CCaddress(cp,(char *)"Rewards",pubkey));
}
UniValue assetsaddress(const UniValue& params, bool fHelp)
{
struct CCcontract_info *cp, C; std::vector<unsigned char> pubkey;
cp = CCinit(&C, EVAL_ASSETS);
if (fHelp || params.size() > 1)
throw runtime_error("assetsaddress [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");
if (params.size() == 1)
pubkey = ParseHex(params[0].get_str().c_str());
return(CCaddress(cp, (char *)"Assets", pubkey));
}
UniValue tokenaddress(const UniValue& params, bool fHelp)
{
struct CCcontract_info *cp,C; std::vector<unsigned char> pubkey;
cp = CCinit(&C,EVAL_ASSETS);
cp = CCinit(&C,EVAL_TOKENS);
if ( fHelp || params.size() > 1 )
throw runtime_error("tokenaddress [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");
if ( params.size() == 1 )
pubkey = ParseHex(params[0].get_str().c_str());
return(CCaddress(cp,(char *)"Assets",pubkey));
return(CCaddress(cp,(char *)"Tokens", pubkey));
}
UniValue marmara_poolpayout(const UniValue& params, bool fHelp)
@@ -6895,7 +7026,7 @@ UniValue tokencreate(const UniValue& params, bool fHelp)
return(result);
}
}
hex = CreateAsset(0,supply,name,description);
hex = CreateToken(0,supply,name,description);
if ( hex.size() > 0 )
{
result.push_back(Pair("result", "success"));
@@ -6927,7 +7058,7 @@ UniValue tokentransfer(const UniValue& params, bool fHelp)
ERR_RESULT("amount must be positive");
return(result);
}
hex = AssetTransfer(0,tokenid,pubkey,amount);
hex = TokenTransfer(0,tokenid,pubkey,amount);
if (amount > 0) {
if ( hex.size() > 0 )
{
@@ -6964,7 +7095,11 @@ UniValue tokenconvert(const UniValue& params, bool fHelp)
ERR_RESULT("amount must be positive");
return(result);
}
hex = AssetConvert(0,tokenid,pubkey,amount,evalcode);
ERR_RESULT("deprecated");
return(result);
/* hex = AssetConvert(0,tokenid,pubkey,amount,evalcode);
if (amount > 0) {
if ( hex.size() > 0 )
{
@@ -6974,7 +7109,7 @@ UniValue tokenconvert(const UniValue& params, bool fHelp)
} else {
ERR_RESULT("amount must be positive");
}
return(result);
return(result); */
}
UniValue tokenbid(const UniValue& params, bool fHelp)
@@ -7271,6 +7406,270 @@ UniValue getbalance64(const UniValue& params, bool fHelp)
return ret;
}
// heir contract functions for coins
UniValue heirfund(const UniValue& params, bool fHelp)
{
UniValue result(UniValue::VOBJ);
//uint256 txid;
int64_t txfee;
int64_t amount;
int64_t inactivitytime;
std::string hex;
std::vector<unsigned char> pubkey;
std::string name;
//TODO: do we need this?
if (!EnsureWalletIsAvailable(fHelp))
return NullUniValue;
if (fHelp || params.size() != 5)
throw runtime_error("heirfund fee funds heirname heirpubkey inactivitytime\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);
txfee = atof((char*)params[0].get_str().c_str()) * COIN;
amount = atof((char*)params[1].get_str().c_str()) * COIN;
name = params[2].get_str();
pubkey = ParseHex(params[3].get_str().c_str());
inactivitytime = atof((char*)params[4].get_str().c_str());
hex = HeirFundCoinCaller(txfee, amount, name, pubkey2pk(pubkey), inactivitytime, zeroid);
if (hex.size() > 0) {
result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex));
}
else
ERR_RESULT("couldn't create heir fund");
return result;
}
UniValue heiradd(const UniValue& params, bool fHelp)
{
UniValue result; // UniValue result(UniValue::VOBJ);
uint256 fundingtxid;
uint64_t txfee;
int64_t amount;
int64_t inactivitytime;
std::string hex;
std::vector<unsigned char> pubkey;
std::string name;
// TODO: do we need this?
if (!EnsureWalletIsAvailable(fHelp))
return NullUniValue;
if (fHelp || params.size() != 3)
throw runtime_error("heiradd fee funds fundingtxid\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);
txfee = atof((char*)params[0].get_str().c_str()) * COIN;
amount = atof((char*)params[1].get_str().c_str()) * COIN;
fundingtxid = Parseuint256((char*)params[2].get_str().c_str());
result = HeirAddCoinCaller(fundingtxid, txfee, amount);
/* if ( hex.size() > 0 )
{
result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex));
} else ERR_RESULT("couldn't claim heir fund"); */
return result;
}
UniValue heirclaim(const UniValue& params, bool fHelp)
{
UniValue result; // result(UniValue::VOBJ);
uint256 fundingtxid;
uint64_t txfee;
int64_t amount;
int64_t inactivitytime;
std::string hex;
std::vector<unsigned char> pubkey;
std::string name;
// do we need this?
if (!EnsureWalletIsAvailable(fHelp))
return NullUniValue;
if (fHelp || params.size() != 3)
throw runtime_error("heirclaim fee funds fundingtxid\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);
txfee = atof((char*)params[0].get_str().c_str()) * COIN;
amount = atof((char*)params[1].get_str().c_str()) * COIN;
fundingtxid = Parseuint256((char*)params[2].get_str().c_str());
result = HeirClaimCoinCaller(fundingtxid, txfee, amount);
/* if ( hex.size() > 0 )
{
result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex));
} else ERR_RESULT("couldn't claim heir fund"); */
return result;
}
// same heir contract functions for tokens
UniValue heirfundtokens(const UniValue& params, bool fHelp)
{
UniValue result(UniValue::VOBJ);
uint256 assetid;
uint64_t txfee;
int64_t amount;
int64_t inactivitytime;
std::string hex;
std::vector<unsigned char> pubkey;
std::string name;
//TODO: do we need this (dimxy)?
if (!EnsureWalletIsAvailable(fHelp))
return NullUniValue;
if (fHelp || params.size() != 6)
throw runtime_error("heirfundtokens fee funds heirname heirpubkey inactivitytime assetid\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);
txfee = atoll((char*)params[0].get_str().c_str());
amount = atoll((char*)params[1].get_str().c_str());
name = params[2].get_str();
pubkey = ParseHex(params[3].get_str().c_str());
inactivitytime = atof((char*)params[4].get_str().c_str());
assetid = Parseuint256((char*)params[5].get_str().c_str());
hex = HeirFundTokenCaller(txfee, amount, name, pubkey2pk(pubkey), inactivitytime, assetid);
if (hex.size() > 0) {
result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex));
}
else
ERR_RESULT("couldn't create heir fund");
return result;
}
UniValue heiraddtokens(const UniValue& params, bool fHelp)
{
UniValue result; // UniValue result(UniValue::VOBJ);
uint256 fundingtxid;
uint64_t txfee;
int64_t amount;
int64_t inactivitytime;
std::string hex;
std::vector<unsigned char> pubkey;
std::string name;
// TODO: do we need this?
if (!EnsureWalletIsAvailable(fHelp))
return NullUniValue;
if (fHelp || params.size() != 3)
throw runtime_error("heiraddtokens fee funds fundingtxid\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);
txfee = atoll((char*)params[0].get_str().c_str());
amount = atoll((char*)params[1].get_str().c_str());
fundingtxid = Parseuint256((char*)params[2].get_str().c_str());
result = HeirAddTokenCaller(fundingtxid, txfee, amount);
/* if ( hex.size() > 0 )
{
result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex));
} else ERR_RESULT("couldn't claim heir fund"); */
return result;
}
UniValue heirclaimtokens(const UniValue& params, bool fHelp)
{
UniValue result; // result(UniValue::VOBJ);
uint256 fundingtxid;
int64_t txfee;
int64_t amount;
int64_t inactivitytime;
std::string hex;
std::vector<unsigned char> pubkey;
std::string name;
// do we need this?
if (!EnsureWalletIsAvailable(fHelp))
return NullUniValue;
if (fHelp || params.size() != 3)
throw runtime_error("heirclaimtokens fee funds fundingtxid\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);
txfee = atoll((char*)params[0].get_str().c_str());
amount = atoll((char*)params[1].get_str().c_str());
fundingtxid = Parseuint256((char*)params[2].get_str().c_str());
result = HeirClaimTokenCaller(fundingtxid, txfee, amount);
/* if ( hex.size() > 0 )
{
result.push_back(Pair("result", "success"));
result.push_back(Pair("hex", hex));
} else ERR_RESULT("couldn't claim heir fund"); */
return result;
}
UniValue heirinfo(const UniValue& params, bool fHelp)
{
uint256 fundingtxid;
if (fHelp || params.size() != 1) // or 0?
throw runtime_error("heirinfo fundingtxid\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");
fundingtxid = Parseuint256((char*)params[0].get_str().c_str());
return (HeirInfo(fundingtxid));
}
UniValue heirlist(const UniValue& params, bool fHelp)
{
if (fHelp || params.size() != 0) // or 0?
throw runtime_error("heirlist\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 (HeirList());
}
extern UniValue dumpprivkey(const UniValue& params, bool fHelp); // in rpcdump.cpp
extern UniValue importprivkey(const UniValue& params, bool fHelp);
extern UniValue importaddress(const UniValue& params, bool fHelp);