Skip to content

Commit 96f87f4

Browse files
committed
Simplify balance summation from utxos
1 parent e272eec commit 96f87f4

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

src/ga_session.cpp

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,28 +2145,19 @@ namespace sdk {
21452145
const amount::value_type satoshi = strtoull(satoshi_str.c_str(), nullptr, 10);
21462146
return { { "btc", satoshi } };
21472147
}
2148-
const auto utxos = get_unspent_outputs(
2148+
const auto utxos_by_asset = get_unspent_outputs(
21492149
{ { "subaccount", subaccount }, { "num_confs", num_confs }, { "confidential", confidential } });
21502150

2151-
const auto accumulate_if = [](const auto& utxos, auto pred) {
2152-
return std::accumulate(
2153-
std::begin(utxos), std::end(utxos), int64_t{ 0 }, [pred](int64_t init, const nlohmann::json& utxo) {
2154-
amount::value_type satoshi{ 0 };
2155-
if (pred(utxo)) {
2156-
satoshi = utxo.at("satoshi");
2157-
}
2158-
return init + satoshi;
2159-
});
2160-
};
2161-
21622151
nlohmann::json balance({ { m_net_params.policy_asset(), 0 } });
2163-
for (const auto& item : utxos.items()) {
2164-
const auto& key = item.key();
2165-
if (key != "error") {
2166-
const auto& item_utxos = item.value();
2167-
const amount::value_type satoshi
2168-
= accumulate_if(item_utxos, [](const auto& utxo) { return !utxo.contains("error"); });
2169-
balance[key] = satoshi;
2152+
for (const auto& asset_utxo : utxos_by_asset.items()) {
2153+
if (asset_utxo.key() != "error") {
2154+
amount::value_type satoshi = 0;
2155+
for (const auto& utxo : asset_utxo.value()) {
2156+
if (!utxo.contains("error")) {
2157+
satoshi += amount::value_type(utxo.at("satoshi"));
2158+
}
2159+
}
2160+
balance[asset_utxo.key()] = satoshi;
21702161
}
21712162
}
21722163

0 commit comments

Comments
 (0)