Merge pull request #5156

3d2772a0 wallet-rpc: get balance for all accounts and subaddresses (stoffu)
This commit is contained in:
Riccardo Spagni 2019-03-04 21:33:07 +02:00
commit 2f7108f9d7
No known key found for this signature in database
GPG Key ID: 55432DF31CCD4FCD
2 changed files with 48 additions and 20 deletions

View File

@ -363,30 +363,54 @@ namespace tools
if (!m_wallet) return not_open(er); if (!m_wallet) return not_open(er);
try try
{ {
res.balance = m_wallet->balance(req.account_index); res.balance = req.all_accounts ? m_wallet->balance_all() : m_wallet->balance(req.account_index);
res.unlocked_balance = m_wallet->unlocked_balance(req.account_index); res.unlocked_balance = req.all_accounts ? m_wallet->unlocked_balance_all() : m_wallet->unlocked_balance(req.account_index);
res.multisig_import_needed = m_wallet->multisig() && m_wallet->has_multisig_partial_key_images(); res.multisig_import_needed = m_wallet->multisig() && m_wallet->has_multisig_partial_key_images();
std::map<uint32_t, uint64_t> balance_per_subaddress = m_wallet->balance_per_subaddress(req.account_index); std::map<uint32_t, std::map<uint32_t, uint64_t>> balance_per_subaddress_per_account;
std::map<uint32_t, uint64_t> unlocked_balance_per_subaddress = m_wallet->unlocked_balance_per_subaddress(req.account_index); std::map<uint32_t, std::map<uint32_t, uint64_t>> unlocked_balance_per_subaddress_per_account;
if (req.all_accounts)
{
for (uint32_t account_index = 0; account_index < m_wallet->get_num_subaddress_accounts(); ++account_index)
{
balance_per_subaddress_per_account[account_index] = m_wallet->balance_per_subaddress(account_index);
unlocked_balance_per_subaddress_per_account[account_index] = m_wallet->unlocked_balance_per_subaddress(account_index);
}
}
else
{
balance_per_subaddress_per_account[req.account_index] = m_wallet->balance_per_subaddress(req.account_index);
unlocked_balance_per_subaddress_per_account[req.account_index] = m_wallet->unlocked_balance_per_subaddress(req.account_index);
}
std::vector<tools::wallet2::transfer_details> transfers; std::vector<tools::wallet2::transfer_details> transfers;
m_wallet->get_transfers(transfers); m_wallet->get_transfers(transfers);
std::set<uint32_t> address_indices = req.address_indices; for (const auto& p : balance_per_subaddress_per_account)
if (address_indices.empty())
{ {
for (const auto& i : balance_per_subaddress) uint32_t account_index = p.first;
address_indices.insert(i.first); std::map<uint32_t, uint64_t> balance_per_subaddress = p.second;
} std::map<uint32_t, uint64_t> unlocked_balance_per_subaddress = unlocked_balance_per_subaddress_per_account[account_index];
for (uint32_t i : address_indices) std::set<uint32_t> address_indices;
{ if (!req.all_accounts && !req.address_indices.empty())
wallet_rpc::COMMAND_RPC_GET_BALANCE::per_subaddress_info info; {
info.address_index = i; address_indices = req.address_indices;
cryptonote::subaddress_index index = {req.account_index, info.address_index}; }
info.address = m_wallet->get_subaddress_as_str(index); else
info.balance = balance_per_subaddress[i]; {
info.unlocked_balance = unlocked_balance_per_subaddress[i]; for (const auto& i : balance_per_subaddress)
info.label = m_wallet->get_subaddress_label(index); address_indices.insert(i.first);
info.num_unspent_outputs = std::count_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return !td.m_spent && td.m_subaddr_index == index; }); }
res.per_subaddress.push_back(info); for (uint32_t i : address_indices)
{
wallet_rpc::COMMAND_RPC_GET_BALANCE::per_subaddress_info info;
info.account_index = account_index;
info.address_index = i;
cryptonote::subaddress_index index = {info.account_index, info.address_index};
info.address = m_wallet->get_subaddress_as_str(index);
info.balance = balance_per_subaddress[i];
info.unlocked_balance = unlocked_balance_per_subaddress[i];
info.label = m_wallet->get_subaddress_label(index);
info.num_unspent_outputs = std::count_if(transfers.begin(), transfers.end(), [&](const tools::wallet2::transfer_details& td) { return !td.m_spent && td.m_subaddr_index == index; });
res.per_subaddress.emplace_back(std::move(info));
}
} }
} }
catch (const std::exception& e) catch (const std::exception& e)

View File

@ -63,14 +63,17 @@ namespace wallet_rpc
{ {
uint32_t account_index; uint32_t account_index;
std::set<uint32_t> address_indices; std::set<uint32_t> address_indices;
bool all_accounts;
BEGIN_KV_SERIALIZE_MAP() BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(account_index) KV_SERIALIZE(account_index)
KV_SERIALIZE(address_indices) KV_SERIALIZE(address_indices)
KV_SERIALIZE_OPT(all_accounts, false);
END_KV_SERIALIZE_MAP() END_KV_SERIALIZE_MAP()
}; };
struct per_subaddress_info struct per_subaddress_info
{ {
uint32_t account_index;
uint32_t address_index; uint32_t address_index;
std::string address; std::string address;
uint64_t balance; uint64_t balance;
@ -79,6 +82,7 @@ namespace wallet_rpc
uint64_t num_unspent_outputs; uint64_t num_unspent_outputs;
BEGIN_KV_SERIALIZE_MAP() BEGIN_KV_SERIALIZE_MAP()
KV_SERIALIZE(account_index)
KV_SERIALIZE(address_index) KV_SERIALIZE(address_index)
KV_SERIALIZE(address) KV_SERIALIZE(address)
KV_SERIALIZE(balance) KV_SERIALIZE(balance)