Skip to content

Commit

Permalink
Merge pull request #642 from nervosnetwork/tx-count-no-failed
Browse files Browse the repository at this point in the history
chore: only include pending and success tx count in address
  • Loading branch information
classicalliu authored Jul 12, 2019
2 parents 37c4f35 + 122d057 commit a46d566
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
6 changes: 5 additions & 1 deletion packages/neuron-wallet/src/database/address/dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { getConnection } from './ormconfig'
import TransactionsService, { OutputStatus } from '../../services/transactions'
import CellsService from '../../services/cells'
import LockUtils from '../../models/lock-utils'
import { TransactionStatus } from '../../types/cell-types'

export interface Address {
walletId: string
Expand Down Expand Up @@ -55,7 +56,10 @@ export default class AddressDao {
address,
})

const txCount: number = await TransactionsService.getCountByAddress(address)
const txCount: number = await TransactionsService.getCountByAddressAndStatus(address, [
TransactionStatus.Pending,
TransactionStatus.Success,
])
const entities = await Promise.all(
addressEntities.map(async entity => {
const addressEntity = entity
Expand Down
2 changes: 1 addition & 1 deletion packages/neuron-wallet/src/database/chain/ormconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export const initConnection = async (genesisBlockHash: string) => {
try {
await getConnection().close()
} catch (err) {
logger.log({ level: 'error', message: err.message })
// do nothing
}
const connectionOptions = await connectOptions(genesisBlockHash)

Expand Down
42 changes: 17 additions & 25 deletions packages/neuron-wallet/src/services/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,36 +585,28 @@ export default class TransactionsService {
return [...new Set(inputBlake160s.concat(outputBlake160s))]
}

// tx count with one lockHash
public static getCountByLockHash = async (lockHash: string): Promise<number> => {
const outputs: OutputEntity[] = await getConnection()
.getRepository(OutputEntity)
.createQueryBuilder('output')
.where(`output.lockHash = :lockHash`, { lockHash })
.select('DISTINCT output.outPointTxHash', 'outPointTxHash')
.getRawMany()

const outputTxHashes: string[] = outputs.map(output => output.outPointTxHash)

const inputs: InputEntity[] = await getConnection()
.getRepository(InputEntity)
.createQueryBuilder('input')
.where(`input.lockHash = :lockHash`, { lockHash })
.select(`DISTINCT input.transactionHash`, 'transactionHash')
.getRawMany()

const inputTxHashes: string[] = inputs.map((input: any) => input.transactionHash)

const hashes: string[] = [...new Set(outputTxHashes.concat(inputTxHashes))]

const count: number = hashes.length
// tx count with one lockHash and status
public static getCountByLockHashAndStatus = async (
lockHash: string,
status: TransactionStatus[]
): Promise<number> => {
const count: number = await getConnection()
.getRepository(TransactionEntity)
.createQueryBuilder('tx')
.leftJoinAndSelect('tx.inputs', 'input')
.leftJoinAndSelect('tx.outputs', 'output')
.where(`(input.lockHash = :lockHash OR output.lockHash = :lockHash) AND tx.status IN (:...status)`, {
lockHash,
status,
})
.getCount()

return count
}

public static getCountByAddress = async (address: string): Promise<number> => {
public static getCountByAddressAndStatus = async (address: string, status: TransactionStatus[]): Promise<number> => {
const lockHash: string = await LockUtils.addressToLockHash(address)
return TransactionsService.getCountByLockHash(lockHash)
return TransactionsService.getCountByLockHashAndStatus(lockHash, status)
}

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

0 comments on commit a46d566

Please sign in to comment.