Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions src/interfaces/wallet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,18 +69,27 @@ WalletTx MakeWalletTx(CWallet& wallet, const CWalletTx& wtx)
WalletTx result;
result.tx = wtx.tx;
result.txin_is_mine.reserve(wtx.tx->vin.size());
result.txin_color_id.reserve(wtx.tx->vin.size());
result.txin_amount.reserve(wtx.tx->vin.size());
for (const auto& txin : wtx.tx->vin) {
result.txin_is_mine.emplace_back(wallet.IsMine(txin));
ColorIdentifier colorId;
CAmount val = wallet.GetDebit(txin, ISMINE_ALL, colorId);
result.txin_color_id.emplace_back(colorId.type != TokenTypes::NONE ? colorId.toHexString() : std::string());
result.txin_amount.emplace_back(val);
}
result.txout_is_mine.reserve(wtx.tx->vout.size());
result.txout_address.reserve(wtx.tx->vout.size());
result.txout_address_is_mine.reserve(wtx.tx->vout.size());
result.txout_color_id.reserve(wtx.tx->vout.size());
for (const auto& txout : wtx.tx->vout) {
result.txout_is_mine.emplace_back(wallet.IsMine(txout));
result.txout_address.emplace_back();
result.txout_address_is_mine.emplace_back(ExtractDestination(txout.scriptPubKey, result.txout_address.back()) ?
IsMine(wallet, result.txout_address.back()) :
ISMINE_NO);
ColorIdentifier cid = GetColorIdFromScript(txout.scriptPubKey);
result.txout_color_id.emplace_back(cid.type != TokenTypes::NONE ? cid.toHexString() : std::string());
}
result.credits = wallet.GetCredit(*wtx.tx, ISMINE_ALL);
result.debits = wallet.GetDebit(*wtx.tx, ISMINE_ALL);
Expand Down Expand Up @@ -364,11 +373,7 @@ class WalletImpl : public Wallet
num_blocks = ::chainActive.Height();
return true;
}
CAmount getBalance() override { return m_wallet.GetBalance()[ColorIdentifier()]; }
CAmount getAvailableBalance(const CCoinControl& coin_control) override
{
return m_wallet.GetAvailableBalance(&coin_control)[ColorIdentifier()];
}
CAmount getBalance(ColorIdentifier colorId) override { return m_wallet.GetBalance()[colorId]; }
CAmount getAvailableBalance(const CCoinControl& coin_control, const ColorIdentifier& colorId) override
{
return m_wallet.GetAvailableBalance(&coin_control)[colorId];
Expand Down
78 changes: 67 additions & 11 deletions src/interfaces/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -200,11 +200,10 @@ class Wallet
virtual bool tryGetBalances(WalletBalances& balances, int& num_blocks) = 0;

//! Get balance.
virtual CAmount getBalance() = 0;
virtual CAmount getBalance(ColorIdentifier colorId = ColorIdentifier()) = 0;

//! Get available balance.
virtual CAmount getAvailableBalance(const CCoinControl& coin_control) = 0;
virtual CAmount getAvailableBalance(const CCoinControl& coin_control, const ColorIdentifier& colorId) = 0;
virtual CAmount getAvailableBalance(const CCoinControl& coin_control, const ColorIdentifier& colorId = ColorIdentifier()) = 0;

//! Issue a new REISSUABLE token; generates address and script internally.
virtual TokenIssuanceResult issueNewReissuableToken(CAmount value) = 0;
Expand Down Expand Up @@ -333,32 +332,35 @@ struct WalletBalances
bool have_watch_only;
TxColoredCoinBalancesMap watch_only_balances;
TxColoredCoinBalancesMap unconfirmed_watch_only_balances;
std::set<ColorIdentifier> tokens;
std::set<ColorIdentifier>::iterator tokenIndex;

WalletBalances(){
have_watch_only = false;
tokenIndex = tokens.begin();
}

CAmount getBalance(const ColorIdentifier& colorId = ColorIdentifier()) const
CAmount getBalance() const
{
auto it = balances.find(colorId);
auto it = balances.find(*tokenIndex);
return it != balances.end() ? it->second : 0;
}

CAmount getUnconfirmedBalance(const ColorIdentifier& colorId = ColorIdentifier()) const
CAmount getUnconfirmedBalance() const
{
auto it = unconfirmed_balances.find(colorId);
auto it = unconfirmed_balances.find(*tokenIndex);
return it != unconfirmed_balances.end() ? it->second : 0;
}

CAmount getWatchOnlyBalance(const ColorIdentifier& colorId = ColorIdentifier()) const
CAmount getWatchOnlyBalance() const
{
auto it = watch_only_balances.find(colorId);
auto it = watch_only_balances.find(*tokenIndex);
return it != watch_only_balances.end() ? it->second : 0;
}

CAmount getUnconfirmedWatchOnlyBalance(const ColorIdentifier& colorId = ColorIdentifier()) const
CAmount getUnconfirmedWatchOnlyBalance() const
{
auto it = unconfirmed_watch_only_balances.find(colorId);
auto it = unconfirmed_watch_only_balances.find(*tokenIndex);
return it != unconfirmed_watch_only_balances.end() ? it->second : 0;
}

Expand All @@ -368,6 +370,57 @@ struct WalletBalances
watch_only_balances != prev.watch_only_balances ||
unconfirmed_watch_only_balances != prev.unconfirmed_watch_only_balances;
}

//collect all tokens in the wallet from all the balance lists
void refreshTokens() {
tokens.clear();

for(auto pair:balances)
tokens.insert(pair.first);
for(auto pair:unconfirmed_balances)
tokens.insert(pair.first);
for(auto pair:watch_only_balances)
tokens.insert(pair.first);
for(auto pair:unconfirmed_watch_only_balances)
tokens.insert(pair.first);

tokenIndex = tokens.begin();
}

void prev()
{
Comment thread
Naviabheeman marked this conversation as resolved.
if (tokens.empty()) return;
if(tokenIndex != tokens.begin())
tokenIndex--;
else
{
tokenIndex = tokens.end();
tokenIndex--;
}
}

void next()
{
Comment thread
Naviabheeman marked this conversation as resolved.
if (tokens.empty()) return;
if(tokenIndex != tokens.end())
{
tokenIndex++;
if(tokenIndex == tokens.end())
tokenIndex = tokens.begin();
}
}

bool isToken()
{
Comment thread
Naviabheeman marked this conversation as resolved.
if (tokens.empty()) return false;
return (*tokenIndex).type != TokenTypes::NONE;
}

std::string getTokenName()
{
Comment thread
Naviabheeman marked this conversation as resolved.
if (tokens.empty()) return "";
return (*tokenIndex).toHexString();
}
};

// Wallet transaction information.
Expand All @@ -378,6 +431,9 @@ struct WalletTx
std::vector<isminetype> txout_is_mine;
std::vector<CTxDestination> txout_address;
std::vector<isminetype> txout_address_is_mine;
std::vector<std::string> txout_color_id; // hex color ID per output, empty string for TPC outputs
std::vector<std::string> txin_color_id; // hex color ID per input's prev out (empty for TPC/unknown)
std::vector<CAmount> txin_amount; // nValue per input's prev out (0 if not mine)
TxColoredCoinBalancesMap credits;
TxColoredCoinBalancesMap debits;
TxColoredCoinBalancesMap changes;
Expand Down
16 changes: 16 additions & 0 deletions src/key_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,19 @@ bool IsValidDestinationString(const std::string& str)
{
return IsValidDestinationString(str, Params());
}

bool IsColoredDestination(const std::string& str, ColorIdentifier* colorId)
{
CTxDestination dest = DecodeDestination(str, Params());
if (const CColorKeyID* p = std::get_if<CColorKeyID>(&dest)) {
if (colorId)
*colorId = p->color;
return true;
}
if (const CColorScriptID* p = std::get_if<CColorScriptID>(&dest)) {
if (colorId)
*colorId = p->color;
return true;
}
return false;
}
2 changes: 2 additions & 0 deletions src/key_io.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define BITCOIN_KEY_IO_H

#include <chainparams.h>
#include <coloridentifier.h>
#include <key.h>
#include <pubkey.h>
#include <script/standard.h>
Expand All @@ -25,5 +26,6 @@ std::string EncodeDestination(const CTxDestination& dest);
CTxDestination DecodeDestination(const std::string& str);
bool IsValidDestinationString(const std::string& str);
bool IsValidDestinationString(const std::string& str, const CChainParams& params);
bool IsColoredDestination(const std::string& str, ColorIdentifier* colorId = nullptr);

#endif // BITCOIN_KEY_IO_H
Loading
Loading