@@ -1255,8 +1255,20 @@ export const getWalletBalances = async (mysql: ServerlessMysql, walletId: string
1255
1255
params . push ( tokenIds ) ;
1256
1256
}
1257
1257
1258
- // use LEFT JOIN as HTR token ('00') won't be on the token table, so INNER JOIN would never match it
1259
- const query = `SELECT * FROM (${ subquery } ) w LEFT JOIN token ON w.token_id = token.id;` ;
1258
+ const query = `
1259
+ SELECT NULL AS total_received,
1260
+ w.unlocked_balance AS unlocked_balance,
1261
+ w.locked_balance AS locked_balance,
1262
+ w.unlocked_authorities AS unlocked_authorities,
1263
+ w.locked_authorities AS locked_authorities,
1264
+ w.timelock_expires AS timelock_expires,
1265
+ w.transactions AS transactions,
1266
+ w.token_id AS token_id,
1267
+ token.name AS name,
1268
+ token.symbol AS symbol
1269
+ FROM (${ subquery } ) w
1270
+ INNER JOIN token ON w.token_id = token.id
1271
+ ` ;
1260
1272
1261
1273
const results : DbSelectResult = await mysql . query ( query , params ) ;
1262
1274
for ( const result of results ) {
@@ -2109,6 +2121,7 @@ export const rebuildAddressBalancesFromUtxos = async (
2109
2121
2110
2122
const addressTransactionCount : StringMap < number > = await getAffectedAddressTxCountFromTxList ( mysql , txList ) ;
2111
2123
const addressTotalReceived : StringMap < number > = await getAffectedAddressTotalReceivedFromTxList ( mysql , txList ) ;
2124
+ const tokenTransactionCount : StringMap < number > = await getAffectedTokenTxCountFromTxList ( mysql , txList ) ;
2112
2125
2113
2126
const finalValues = oldAddressTokenTransactions . map ( ( { address, tokenId, transactions, totalReceived } ) => {
2114
2127
const diffTransactions = addressTransactionCount [ `${ address } _${ tokenId } ` ] || 0 ;
@@ -2129,6 +2142,15 @@ export const rebuildAddressBalancesFromUtxos = async (
2129
2142
AND \`token_id\` = ?
2130
2143
` , item ) ;
2131
2144
}
2145
+
2146
+ // update token table with the correct amount of transactions
2147
+ for ( const token of Object . keys ( tokenTransactionCount ) ) {
2148
+ await mysql . query ( `
2149
+ UPDATE \`token\`
2150
+ SET \`transactions\` = \`transactions\` - ?
2151
+ WHERE \`id\` = ?
2152
+ ` , [ tokenTransactionCount [ token ] , token ] ) ;
2153
+ }
2132
2154
} ;
2133
2155
2134
2156
/**
@@ -2546,6 +2568,39 @@ export const getAffectedAddressTxCountFromTxList = async (
2546
2568
return addressTransactions as StringMap < number > ;
2547
2569
} ;
2548
2570
2571
+ /**
2572
+ * Get the number of affected transactions for each token from the address_tx_history table
2573
+ * given a list of transactions
2574
+ *
2575
+ * @param mysql - Database connection
2576
+ * @param txList - A list of affected transactions to get the token tx count
2577
+
2578
+ * @returns A Map with tokenId as key and the transaction count as values
2579
+ */
2580
+ export const getAffectedTokenTxCountFromTxList = async (
2581
+ mysql : ServerlessMysql ,
2582
+ txList : string [ ] ,
2583
+ ) : Promise < StringMap < number > > => {
2584
+ const results : DbSelectResult = await mysql . query ( `
2585
+ SELECT token_id AS tokenId, COUNT(DISTINCT(tx_id)) AS txCount
2586
+ FROM address_tx_history
2587
+ WHERE tx_id IN (?)
2588
+ AND voided = TRUE
2589
+ GROUP BY token_id
2590
+ ` , [ txList ] ) ;
2591
+
2592
+ const tokenTransactions = results . reduce ( ( acc , result ) => {
2593
+ const tokenId = result . tokenId as string ;
2594
+ const txCount = result . txCount as number ;
2595
+
2596
+ acc [ tokenId ] = txCount ;
2597
+
2598
+ return acc ;
2599
+ } , { } ) ;
2600
+
2601
+ return tokenTransactions as StringMap < number > ;
2602
+ } ;
2603
+
2549
2604
/**
2550
2605
* Get the affected total_received for each address/token pair given a list of transactions
2551
2606
*
@@ -2560,9 +2615,10 @@ export const getAffectedAddressTotalReceivedFromTxList = async (
2560
2615
) : Promise < StringMap < number > > => {
2561
2616
const results : DbSelectResult = await mysql . query ( `
2562
2617
SELECT address, token_id as tokenId, SUM(value) as total
2563
- FROM tx_output
2564
- WHERE tx_id IN (?) AND voided = TRUE
2565
- GROUP BY address, token_id
2618
+ FROM tx_output
2619
+ WHERE tx_id IN (?)
2620
+ AND voided = TRUE
2621
+ GROUP BY address, token_id
2566
2622
` , [ txList ] ) ;
2567
2623
2568
2624
const addressTotalReceivedMap = results . reduce ( ( acc , result ) => {
@@ -2577,3 +2633,20 @@ export const getAffectedAddressTotalReceivedFromTxList = async (
2577
2633
2578
2634
return addressTotalReceivedMap as StringMap < number > ;
2579
2635
} ;
2636
+
2637
+ /**
2638
+ * Increment a list of tokens transactions count
2639
+ *
2640
+ * @param mysql - Database connection
2641
+ * @param tokenList - The list of tokens to increment
2642
+ */
2643
+ export const incrementTokensTxCount = async (
2644
+ mysql : ServerlessMysql ,
2645
+ tokenList : string [ ] ,
2646
+ ) : Promise < void > => {
2647
+ await mysql . query ( `
2648
+ UPDATE \`token\`
2649
+ SET \`transactions\` = \`transactions\` + 1
2650
+ WHERE \`id\` IN (?)
2651
+ ` , [ tokenList ] ) ;
2652
+ } ;
0 commit comments