Skip to content

Commit a1c1946

Browse files
committed
[BDAP] Add getcredits RPC command to check available balances
1 parent 8c6026b commit a1c1946

File tree

3 files changed

+134
-10
lines changed

3 files changed

+134
-10
lines changed

src/bdap/rpcdomainentry.cpp

Lines changed: 89 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#include "primitives/transaction.h"
1414
#include "spork.h"
1515
#include "wallet/wallet.h"
16+
#include "utilmoneystr.h"
1617
#include "validation.h"
1718
#include "dynode-sync.h"
1819

@@ -148,7 +149,7 @@ UniValue adduser(const JSONRPCRequest& request)
148149
"3. registration months (int, optional) Number of months to register account\n"
149150
"\nAdds a new bdap.io public name account entry to the blockchain directory.\n"
150151
"\nResult:\n"
151-
"{(json object)\n"
152+
"{(json objects)\n"
152153
" \"oid\" (string) Account OID\n"
153154
" \"version\" (int) Recipient's BDAP full path\n"
154155
" \"domain_component\" (string) Account domain name\n"
@@ -167,7 +168,7 @@ UniValue adduser(const JSONRPCRequest& request)
167168
" \"height\" (int) Last transaction height for account data\n"
168169
" \"expires_on\" (int) Account expiration epoch time\n"
169170
" \"expired\" (boolean) Account expired\n"
170-
" }\n"
171+
" },...n \n"
171172
"\nExamples\n" +
172173
HelpExampleCli("adduser", "Alice \"Wonderland, Alice\"") +
173174
"\nAs a JSON-RPC call\n" +
@@ -924,27 +925,27 @@ UniValue mybdapaccounts(const JSONRPCRequest& request)
924925
return result;
925926
}
926927

927-
UniValue colorcoin(const JSONRPCRequest& request)
928+
UniValue makecredits(const JSONRPCRequest& request)
928929
{
929930
if (request.fHelp || request.params.size() < 2 || request.params.size() > 3)
930931
throw std::runtime_error(
931-
"colorcoin \"dynamicaddress\" \"amount\" \"utxo list\"\n"
932-
"\nConvert your DYN to BDAP colored credits\n"
932+
"makecredits \"dynamicaddress\" \"amount\"\n"
933+
"\nConvert your Dynamic (DYN) to BDAP colored credits\n"
933934
+ HelpRequiringPassphrase() +
934935
"\nArguments:\n"
935936
"1. \"dynamicaddress\" (string) The destination wallet address\n"
936937
"2. \"amount\" (int) The amount in " + CURRENCY_UNIT + " to color. eg 0.1\n"
937938
"\nResult:\n"
938939
" \"tx id\" (string) The transaction id for the coin coloring\n"
939940
"\nExamples:\n"
940-
+ HelpExampleCli("colorcoin", "\"DKkDJn9bjoXJiiPysSVEeUc3ve6SaWLzVv\" 100.98 \"utxo1,utxo2\"") +
941+
+ HelpExampleCli("makecredits", "\"DKkDJn9bjoXJiiPysSVEeUc3ve6SaWLzVv\" 100.98") +
941942
"\nAs a JSON-RPC call\n"
942-
+ HelpExampleRpc("colorcoin", "\"DKkDJn9bjoXJiiPysSVEeUc3ve6SaWLzVv\" 100.98 \"utxo1,utxo2\""));
943+
+ HelpExampleRpc("makecredits", "\"DKkDJn9bjoXJiiPysSVEeUc3ve6SaWLzVv\" 100.98"));
943944

944945
EnsureWalletIsUnlocked();
945946

946947
if (!sporkManager.IsSporkActive(SPORK_30_ACTIVATE_BDAP))
947-
throw JSONRPCError(RPC_BDAP_SPORK_INACTIVE, strprintf("Can not use the colorcoin RPC command until the BDAP spork is active."));
948+
throw JSONRPCError(RPC_BDAP_SPORK_INACTIVE, strprintf("Can not use the makecredits RPC command until the BDAP spork is active."));
948949

949950
if (!pwalletMain)
950951
throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Error accessing wallet."));
@@ -975,6 +976,84 @@ UniValue colorcoin(const JSONRPCRequest& request)
975976
return wtx.GetHash().GetHex();
976977
}
977978

979+
UniValue getcredits(const JSONRPCRequest& request)
980+
{
981+
if (request.fHelp || request.params.size() != 0)
982+
throw std::runtime_error(
983+
"getcredits\n"
984+
"\nGet available BDAP credit balance\n"
985+
+ HelpRequiringPassphrase() +
986+
"\nResult:\n"
987+
"{(json objects)\n"
988+
" \"type\" (string) The credit type.\n"
989+
" \"operation\" (string) The operation code used in the tx output\n"
990+
" \"address\" (string) The address holding the unspent BDAP credits\n"
991+
" \"dynamic_amount\" (int) The unspent BDAP amount int DYN\n"
992+
" \"credits\" (int) The unspent BDAP credits\n"
993+
" },...n \n"
994+
"\"total_credits\" (int) The total credits available.\n"
995+
"\"total_deposits\" (int) The total deposits available.\n"
996+
"\"total_dynamic\" (int) The total Dynamic available that are BDAP colored.\n"
997+
"\nExamples:\n"
998+
+ HelpExampleCli("getcredits", "") +
999+
"\nAs a JSON-RPC call\n"
1000+
+ HelpExampleRpc("getcredits", ""));
1001+
1002+
if (!pwalletMain)
1003+
throw JSONRPCError(RPC_WALLET_ERROR, strprintf("Error accessing wallet."));
1004+
1005+
std::vector<std::pair<CTxOut, COutPoint>> vCredits;
1006+
pwalletMain->AvailableBDAPCredits(vCredits);
1007+
1008+
CAmount nTotalAmount = 0;
1009+
CAmount nTotalCredits = 0;
1010+
CAmount nTotalDeposits = 0;
1011+
UniValue result(UniValue::VOBJ);
1012+
for (const std::pair<CTxOut, COutPoint>& credit : vCredits) {
1013+
UniValue oCredit(UniValue::VOBJ);
1014+
int opCode1 = -1; int opCode2 = -1;
1015+
std::vector<std::vector<unsigned char>> vvch;
1016+
credit.first.GetBDAPOpCodes(opCode1, opCode2, vvch);
1017+
std::string strOpType = GetBDAPOpTypeString(opCode1, opCode2);
1018+
const CDynamicAddress address = GetScriptAddress(credit.first.scriptPubKey);
1019+
std::string strType = "unknown";
1020+
if (strOpType == "bdap_new_account" || strOpType == "bdap_update_account") {
1021+
strType = "account deposit";
1022+
nTotalDeposits += credit.first.nValue;
1023+
} else if (strOpType == "bdap_new_link_request" || strOpType == "bdap_new_link_accept" ||
1024+
strOpType == "bdap_delete_link_request" || strOpType == "bdap_delete_link_accept") {
1025+
strType = "link deposit";
1026+
nTotalDeposits += credit.first.nValue;
1027+
} else if (strOpType == "bdap_move_asset") {
1028+
if (vvch.size() > 1) {
1029+
std::string strMoveSource = stringFromVch(vvch[0]);
1030+
std::string strMoveDestination = stringFromVch(vvch[1]);
1031+
strType = strprintf("credit (%s to %s)", strMoveSource, strMoveDestination);
1032+
if (strMoveSource == "DYN" && strMoveDestination == "BDAP")
1033+
nTotalCredits += credit.first.nValue;
1034+
} else {
1035+
strType = strprintf("credit (unknown)");
1036+
}
1037+
}
1038+
oCredit.push_back(Pair("type", strType));
1039+
oCredit.push_back(Pair("operation", strOpType));
1040+
oCredit.push_back(Pair("address", address.ToString()));
1041+
oCredit.push_back(Pair("dynamic_amount", FormatMoney(credit.first.nValue)));
1042+
oCredit.push_back(Pair("credits", credit.first.nValue/BDAP_CREDIT));
1043+
std::string strOutput = credit.second.hash.ToString() + "-" + std::to_string(credit.second.n);
1044+
result.push_back(Pair(strOutput, oCredit));
1045+
nTotalAmount += credit.first.nValue;
1046+
}
1047+
// Add total amounts and credits
1048+
1049+
result.push_back(Pair("total_credits", nTotalCredits/BDAP_CREDIT));
1050+
result.push_back(Pair("total_deposits", nTotalDeposits/BDAP_CREDIT));
1051+
result.push_back(Pair("total_dynamic", FormatMoney(nTotalAmount)));
1052+
1053+
return result;
1054+
}
1055+
1056+
9781057
static const CRPCCommand commands[] =
9791058
{ // category name actor (function) okSafe argNames
9801059
// --------------------- ------------------------ ----------------------- ------ --------------------
@@ -991,7 +1070,8 @@ static const CRPCCommand commands[] =
9911070
{ "bdap", "addgroup", &addgroup, true, {"account id", "common name", "registration days"} },
9921071
{ "bdap", "getgroupinfo", &getgroupinfo, true, {"account id"} },
9931072
{ "bdap", "mybdapaccounts", &mybdapaccounts, true, {} },
994-
{ "bdap", "colorcoin", &colorcoin, true, {"dynamicaddress", "amount"} },
1073+
{ "bdap", "makecredits", &makecredits, true, {"dynamicaddress", "amount"} },
1074+
{ "bdap", "getcredits", &getcredits, true, {} },
9951075
#endif //ENABLE_WALLET
9961076
{ "bdap", "makekeypair", &makekeypair, true, {"prefix"} },
9971077
};

src/wallet/wallet.cpp

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2931,6 +2931,46 @@ void CWallet::AvailableCoins(std::vector<COutput>& vCoins, bool fOnlyConfirmed,
29312931
}
29322932
}
29332933

2934+
void CWallet::AvailableBDAPCredits(std::vector<std::pair<CTxOut, COutPoint>>& vCredits, bool fOnlyConfirmed) const
2935+
{
2936+
vCredits.clear();
2937+
2938+
{
2939+
LOCK2(cs_main, cs_wallet);
2940+
2941+
for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it) {
2942+
const uint256& wtxid = it->first;
2943+
const CWalletTx* pcoin = &(*it).second;
2944+
2945+
if (!CheckFinalTx(*pcoin))
2946+
continue;
2947+
2948+
if (fOnlyConfirmed && !pcoin->IsTrusted())
2949+
continue;
2950+
2951+
if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
2952+
continue;
2953+
2954+
int nDepth = pcoin->GetDepthInMainChain();
2955+
// We should not consider coins which aren't at least in our mempool
2956+
// It's possible for these to be conflicted via ancestors which we may never be able to detect
2957+
if (nDepth == 0 && !pcoin->InMempool())
2958+
continue;
2959+
2960+
for (unsigned int i = 0; i < pcoin->tx->vout.size(); i++) {
2961+
if (!pcoin->tx->vout[i].IsBDAP())
2962+
continue;
2963+
2964+
if (fOnlyConfirmed && pcoin->tx->vout[i].nValue == 0)
2965+
continue;
2966+
2967+
isminetype mine = IsMine(pcoin->tx->vout[i]);
2968+
if (!(IsSpent(wtxid, i)) && mine != ISMINE_NO)
2969+
vCredits.push_back(std::make_pair(pcoin->tx->vout[i], COutPoint(pcoin->tx->GetHash(), i)));
2970+
}
2971+
}
2972+
}
2973+
}
29342974
static void ApproximateBestSubset(std::vector<std::pair<CAmount, std::pair<const CWalletTx*, unsigned int> > > vValue, const CAmount& nTotalLower, const CAmount& nTargetValue, std::vector<char>& vfBest, CAmount& nBest, bool fUseInstantSend = false, int iterations = 1000)
29352975
{
29362976
std::vector<char> vfIncluded;

src/wallet/wallet.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -965,7 +965,11 @@ class CWallet : public CCryptoKeyStore, public CValidationInterface
965965
*/
966966
void AvailableCoins(std::vector<COutput>& vCoins, bool fOnlyConfirmed = true, const CCoinControl* coinControl = NULL, bool fIncludeZeroValue = false, AvailableCoinsType nCoinType = ALL_COINS, bool fUseInstantSend = false, bool fUseBDAP = false) const;
967967
/**
968-
* populate vCoins with vector of available COutputs for a BDAP transaction.
968+
* populate vCoins with vector of available BDAP credits.
969+
*/
970+
void AvailableBDAPCredits(std::vector<std::pair<CTxOut, COutPoint>>& vCredits, bool fOnlyConfirmed = true) const;
971+
/**
972+
* populate vCoins with vector of available COutputs for the specified address.
969973
*/
970974
void GetBDAPCoins(std::vector<COutput>& vCoins, const CScript& prevScriptPubKey) const;
971975
/**

0 commit comments

Comments
 (0)