Skip to content

Commit a46d566

Browse files
authored
Merge pull request #642 from nervosnetwork/tx-count-no-failed
chore: only include pending and success tx count in address
2 parents 37c4f35 + 122d057 commit a46d566

File tree

3 files changed

+23
-27
lines changed

3 files changed

+23
-27
lines changed

packages/neuron-wallet/src/database/address/dao.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getConnection } from './ormconfig'
55
import TransactionsService, { OutputStatus } from '../../services/transactions'
66
import CellsService from '../../services/cells'
77
import LockUtils from '../../models/lock-utils'
8+
import { TransactionStatus } from '../../types/cell-types'
89

910
export interface Address {
1011
walletId: string
@@ -55,7 +56,10 @@ export default class AddressDao {
5556
address,
5657
})
5758

58-
const txCount: number = await TransactionsService.getCountByAddress(address)
59+
const txCount: number = await TransactionsService.getCountByAddressAndStatus(address, [
60+
TransactionStatus.Pending,
61+
TransactionStatus.Success,
62+
])
5963
const entities = await Promise.all(
6064
addressEntities.map(async entity => {
6165
const addressEntity = entity

packages/neuron-wallet/src/database/chain/ormconfig.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ export const initConnection = async (genesisBlockHash: string) => {
3939
try {
4040
await getConnection().close()
4141
} catch (err) {
42-
logger.log({ level: 'error', message: err.message })
42+
// do nothing
4343
}
4444
const connectionOptions = await connectOptions(genesisBlockHash)
4545

packages/neuron-wallet/src/services/transactions.ts

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -585,36 +585,28 @@ export default class TransactionsService {
585585
return [...new Set(inputBlake160s.concat(outputBlake160s))]
586586
}
587587

588-
// tx count with one lockHash
589-
public static getCountByLockHash = async (lockHash: string): Promise<number> => {
590-
const outputs: OutputEntity[] = await getConnection()
591-
.getRepository(OutputEntity)
592-
.createQueryBuilder('output')
593-
.where(`output.lockHash = :lockHash`, { lockHash })
594-
.select('DISTINCT output.outPointTxHash', 'outPointTxHash')
595-
.getRawMany()
596-
597-
const outputTxHashes: string[] = outputs.map(output => output.outPointTxHash)
598-
599-
const inputs: InputEntity[] = await getConnection()
600-
.getRepository(InputEntity)
601-
.createQueryBuilder('input')
602-
.where(`input.lockHash = :lockHash`, { lockHash })
603-
.select(`DISTINCT input.transactionHash`, 'transactionHash')
604-
.getRawMany()
605-
606-
const inputTxHashes: string[] = inputs.map((input: any) => input.transactionHash)
607-
608-
const hashes: string[] = [...new Set(outputTxHashes.concat(inputTxHashes))]
609-
610-
const count: number = hashes.length
588+
// tx count with one lockHash and status
589+
public static getCountByLockHashAndStatus = async (
590+
lockHash: string,
591+
status: TransactionStatus[]
592+
): Promise<number> => {
593+
const count: number = await getConnection()
594+
.getRepository(TransactionEntity)
595+
.createQueryBuilder('tx')
596+
.leftJoinAndSelect('tx.inputs', 'input')
597+
.leftJoinAndSelect('tx.outputs', 'output')
598+
.where(`(input.lockHash = :lockHash OR output.lockHash = :lockHash) AND tx.status IN (:...status)`, {
599+
lockHash,
600+
status,
601+
})
602+
.getCount()
611603

612604
return count
613605
}
614606

615-
public static getCountByAddress = async (address: string): Promise<number> => {
607+
public static getCountByAddressAndStatus = async (address: string, status: TransactionStatus[]): Promise<number> => {
616608
const lockHash: string = await LockUtils.addressToLockHash(address)
617-
return TransactionsService.getCountByLockHash(lockHash)
609+
return TransactionsService.getCountByLockHashAndStatus(lockHash, status)
618610
}
619611

620612
public static pendings = async (): Promise<TransactionEntity[]> => {

0 commit comments

Comments
 (0)