Skip to content

Commit e8dbe03

Browse files
committed
[BDAP] Add colorcoin RPC command
1 parent 255773f commit e8dbe03

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed

src/bdap/rpcdomainentry.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
#include <univalue.h>
2020

2121
extern void SendBDAPTransaction(const CScript& bdapDataScript, const CScript& bdapOPScript, CWalletTx& wtxNew, const CAmount& nRegFee, const CAmount& nDepositFee, const bool fUseInstantSend);
22+
extern void SendColorTransaction(const CScript& scriptColorCoins, CWalletTx& wtxNew, const CAmount& nColorAmount, const CCoinControl* coinControl, const bool fUseInstantSend, const bool fUsePrivateSend);
2223

2324
static constexpr bool fPrintDebug = true;
2425

@@ -925,6 +926,55 @@ UniValue mybdapaccounts(const JSONRPCRequest& request)
925926
return result;
926927
}
927928

929+
UniValue colorcoin(const JSONRPCRequest& request)
930+
{
931+
if (request.fHelp || request.params.size() < 2 || request.params.size() > 3)
932+
throw std::runtime_error(
933+
"colorcoin \"dynamicaddress\" \"amount\" \"utxo list\"\n"
934+
"\nConvert your DYN to BDAP colored credits\n"
935+
+ HelpRequiringPassphrase() +
936+
"\nArguments:\n"
937+
"1. \"dynamicaddress\" (string) The destination wallet address\n"
938+
"2. \"amount\" (int) The amount in " + CURRENCY_UNIT + " to color. eg 0.1\n"
939+
"3. \"utxo list\" (string, optional) UTXO txids list seperated by commas\n"
940+
"\nResult:\n"
941+
" \"tx id\" (string) The transaction id for the coin coloring\n"
942+
"\nExamples:\n"
943+
+ HelpExampleCli("colorcoin", "\"DKkDJn9bjoXJiiPysSVEeUc3ve6SaWLzVv\" 100.98 \"utxo1,utxo2\"") +
944+
"\nAs a JSON-RPC call\n"
945+
+ HelpExampleRpc("colorcoin", "\"DKkDJn9bjoXJiiPysSVEeUc3ve6SaWLzVv\" 100.98 \"utxo1,utxo2\""));
946+
947+
EnsureWalletIsUnlocked();
948+
949+
if (!pwalletMain)
950+
throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Error accessing wallet."));
951+
952+
CTxDestination dest = DecodeDestination(request.params[0].get_str());
953+
if (!IsValidDestination(dest))
954+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
955+
956+
CAmount nColorAmount = AmountFromValue(request.params[1]);
957+
if (nColorAmount <= 0)
958+
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for coloring");
959+
960+
std::vector<unsigned char> vchMoveSource = vchFromString(std::string("DYN"));
961+
std::vector<unsigned char> vchMoveDestination = vchFromString(std::string("BDAP"));
962+
963+
// Create BDAP move asset operation script
964+
CScript scriptColorCoins;
965+
scriptColorCoins << CScript::EncodeOP_N(OP_BDAP_MODIFY_RDN) << CScript::EncodeOP_N(OP_BDAP_ASSET)
966+
<< vchMoveSource << vchMoveDestination << OP_2DROP << OP_2DROP;
967+
968+
CScript scriptDestination;
969+
scriptDestination = GetScriptForDestination(dest);
970+
scriptColorCoins += scriptDestination;
971+
972+
CWalletTx wtx;
973+
SendColorTransaction(scriptColorCoins, wtx, nColorAmount, NULL, false, false);
974+
975+
return wtx.GetHash().GetHex();
976+
}
977+
928978
static const CRPCCommand commands[] =
929979
{ // category name actor (function) okSafe argNames
930980
// --------------------- ------------------------ ----------------------- ------ --------------------
@@ -941,6 +991,7 @@ static const CRPCCommand commands[] =
941991
{ "bdap", "addgroup", &addgroup, true, {"account id", "common name", "registration days"} },
942992
{ "bdap", "getgroupinfo", &getgroupinfo, true, {"account id"} },
943993
{ "bdap", "mybdapaccounts", &mybdapaccounts, true, {} },
994+
{ "bdap", "colorcoin", &colorcoin, true, {"dynamicaddress", "amount", "utxo list"} },
944995
#endif //ENABLE_WALLET
945996
{ "bdap", "makekeypair", &makekeypair, true, {"prefix"} },
946997
};

src/wallet/rpcwallet.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,42 @@ void SendLinkingTransaction(const CScript& bdapDataScript, const CScript& bdapOP
548548
}
549549
}
550550

551+
void SendColorTransaction(const CScript& scriptColorCoins, CWalletTx& wtxNew, const CAmount& nColorAmount, const CCoinControl* coinControl, const bool fUseInstantSend, const bool fUsePrivateSend)
552+
{
553+
CAmount curBalance = pwalletMain->GetBalance();
554+
555+
// Check amount
556+
if (nColorAmount <= 0)
557+
throw JSONRPCError(RPC_INVALID_PARAMETER, strprintf("%s invalid amount", __func__));
558+
559+
if (nColorAmount > curBalance)
560+
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, strprintf("%s insufficient funds", __func__));
561+
562+
// Create and send the transaction
563+
CReserveKey reservekey(pwalletMain);
564+
CAmount nFeeRequired;
565+
std::string strError;
566+
std::vector<CRecipient> vecSend;
567+
int nChangePosInOut = 0;
568+
569+
LogPrintf("Sending color coin script: %s\n", ScriptToAsmStr(scriptColorCoins));
570+
571+
CRecipient recScript = {scriptColorCoins, nColorAmount, false};
572+
vecSend.push_back(recScript);
573+
//
574+
if (!pwalletMain->CreateTransaction(vecSend, wtxNew, reservekey, nFeeRequired, nChangePosInOut,
575+
strError, coinControl, true, fUsePrivateSend ? ONLY_DENOMINATED : ALL_COINS, fUseInstantSend, true)) {
576+
if (nColorAmount + nFeeRequired > pwalletMain->GetBalance())
577+
strError = strprintf("Error: This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds!", FormatMoney(nFeeRequired));
578+
throw JSONRPCError(RPC_WALLET_ERROR, strError);
579+
}
580+
CValidationState state;
581+
if (!pwalletMain->CommitTransaction(wtxNew, reservekey, g_connman.get(), state, NetMsgType::TX)) {
582+
strError = strprintf("Error: The transaction was rejected! Reason given: %s", state.GetRejectReason());
583+
throw JSONRPCError(RPC_WALLET_ERROR, strError);
584+
}
585+
}
586+
551587
void SendCustomTransaction(const CScript& generatedScript, CWalletTx& wtxNew, CAmount nValue, bool fUseInstantSend = false)
552588
{
553589
CAmount curBalance = pwalletMain->GetBalance();

0 commit comments

Comments
 (0)