@@ -42,12 +42,12 @@ namespace ledger {
42
42
namespace core {
43
43
44
44
std::size_t BitcoinLikeUTXODatabaseHelper::UTXOcount (soci::session &sql, const std::string &accountUid, int64_t dustAmount, const std::function<bool (const std::string &address)> &filter) {
45
- rowset<row> rows = (sql.prepare << " SELECT o.address FROM bitcoin_outputs AS o "
46
- " LEFT OUTER JOIN bitcoin_inputs AS i ON i.previous_tx_uid = o.transaction_uid "
47
- " AND i.previous_output_idx = o.idx"
48
- " WHERE i.previous_tx_uid IS NULL AND o.account_uid = :uid AND o.amount > :dustAmount" ,
49
- use (accountUid), use (dustAmount));
50
- std::size_t count = 0 ;
45
+ const rowset<row> rows = (sql.prepare << " SELECT o.address FROM bitcoin_outputs AS o "
46
+ " LEFT OUTER JOIN bitcoin_inputs AS i ON i.previous_tx_uid = o.transaction_uid "
47
+ " AND i.previous_output_idx = o.idx"
48
+ " WHERE i.previous_tx_uid IS NULL AND o.account_uid = :uid AND o.amount > :dustAmount" ,
49
+ use (accountUid), use (dustAmount));
50
+ std::size_t count = 0 ;
51
51
for (auto &row : rows) {
52
52
if (row.get_indicator (0 ) != i_null && filter (row.get <std::string>(0 ))) {
53
53
count += 1 ;
@@ -58,14 +58,14 @@ namespace ledger {
58
58
59
59
std::size_t
60
60
BitcoinLikeUTXODatabaseHelper::queryUTXO (soci::session &sql, const std::string &accountUid, int32_t offset, int32_t count, int64_t dustAmount, std::vector<BitcoinLikeBlockchainExplorerOutput> &out, const std::function<bool (const std::string &address)> &filter) {
61
- rowset<row> rows = (sql.prepare << " SELECT o.address, o.idx, o.transaction_hash, o.amount, o.script, o.block_height,"
62
- " replaceable"
63
- " FROM bitcoin_outputs AS o "
64
- " LEFT OUTER JOIN bitcoin_inputs AS i ON i.previous_tx_uid = o.transaction_uid "
65
- " AND i.previous_output_idx = o.idx"
66
- " WHERE i.previous_tx_uid IS NULL AND o.account_uid = :uid AND o.amount > :dustAmount"
67
- " ORDER BY block_height LIMIT :count OFFSET :off" ,
68
- use (accountUid), use (dustAmount), use (count), use (offset));
61
+ const rowset<row> rows = (sql.prepare << " SELECT o.address, o.idx, o.transaction_hash, o.amount, o.script, o.block_height,"
62
+ " replaceable"
63
+ " FROM bitcoin_outputs AS o "
64
+ " LEFT OUTER JOIN bitcoin_inputs AS i ON i.previous_tx_uid = o.transaction_uid "
65
+ " AND i.previous_output_idx = o.idx"
66
+ " WHERE i.previous_tx_uid IS NULL AND o.account_uid = :uid AND o.amount > :dustAmount"
67
+ " ORDER BY block_height LIMIT :count OFFSET :off" ,
68
+ use (accountUid), use (dustAmount), use (count), use (offset));
69
69
70
70
for (auto &row : rows) {
71
71
if (row.get_indicator (0 ) != i_null && filter (row.get <std::string>(0 ))) {
@@ -86,23 +86,39 @@ namespace ledger {
86
86
return out.size ();
87
87
}
88
88
89
+ BigInt BitcoinLikeUTXODatabaseHelper::sumUTXO (soci::session &sql, const std::string &accountUid, int64_t dustAmount) {
90
+ const rowset<row> rows = (sql.prepare << " SELECT sum(o.amount)::bigint"
91
+ " FROM bitcoin_outputs AS o "
92
+ " LEFT OUTER JOIN bitcoin_inputs AS i ON i.previous_tx_uid = o.transaction_uid "
93
+ " AND i.previous_output_idx = o.idx"
94
+ " WHERE i.previous_tx_uid IS NULL AND o.account_uid = :uid AND o.amount > :dustAmount" ,
95
+ use (accountUid), use (dustAmount));
96
+
97
+ for (auto &row : rows) {
98
+ if (row.get_indicator (0 ) != i_null) {
99
+ return row.get <BigInt>(0 );
100
+ }
101
+ }
102
+ return BigInt (0 );
103
+ }
104
+
89
105
std::vector<BitcoinLikeUtxo> BitcoinLikeUTXODatabaseHelper::queryAllUtxos (
90
106
soci::session &session,
91
107
std::string const &accountUid,
92
108
api::Currency const ¤cy,
93
109
int64_t dustAmount) {
94
- soci::rowset<soci::row> rows = (session.prepare << " SELECT o.address, o.idx, o.transaction_hash, o.amount, o.script, o.block_height "
95
- " FROM bitcoin_outputs AS o "
96
- " LEFT OUTER JOIN bitcoin_inputs AS i ON i.previous_tx_uid = o.transaction_uid AND i.previous_output_idx = o.idx "
97
- " WHERE i.previous_tx_uid IS NULL AND o.account_uid = :uid AND o.amount > :dustAmount "
98
- " ORDER BY o.block_height" ,
99
- use (accountUid), use (dustAmount));
110
+ const soci::rowset<soci::row> rows = (session.prepare << " SELECT o.address, o.idx, o.transaction_hash, o.amount, o.script, o.block_height "
111
+ " FROM bitcoin_outputs AS o "
112
+ " LEFT OUTER JOIN bitcoin_inputs AS i ON i.previous_tx_uid = o.transaction_uid AND i.previous_output_idx = o.idx "
113
+ " WHERE i.previous_tx_uid IS NULL AND o.account_uid = :uid AND o.amount > :dustAmount "
114
+ " ORDER BY o.block_height" ,
115
+ use (accountUid), use (dustAmount));
100
116
101
117
std::vector<BitcoinLikeUtxo> utxos;
102
118
103
119
for (auto &row : rows) {
104
120
if (row.get_indicator (0 ) != i_null) {
105
- BitcoinLikeUtxo output{
121
+ const BitcoinLikeUtxo output{
106
122
get_number<uint64_t >(row, 1 ),
107
123
row.get <std::string>(2 ),
108
124
Amount (currency, 0 , row.get <BigInt>(3 )),
0 commit comments