Skip to content

Commit

Permalink
feat: check tx success regular intervals
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Aug 17, 2019
1 parent 9bfa9b3 commit a1203da
Showing 1 changed file with 31 additions and 13 deletions.
44 changes: 31 additions & 13 deletions packages/neuron-wallet/src/listeners/tx-status.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,35 @@ import { getConnection } from 'typeorm'
import { TransactionStatus } from 'types/cell-types'
import LockUtils from 'models/lock-utils'
import AddressesUsedSubject from 'models/subjects/addresses-used-subject'
import { FailedTransaction } from 'services/tx'
import { FailedTransaction, TransactionPersistor } from 'services/tx'
import { CONNECTION_NOT_FOUND_NAME } from 'database/chain/ormconfig'
import TypeConvert from 'types/type-convert'

const { nodeService } = remote.require('./startup/sync-block-task/params')

const getTransactionStatus = async (hash: string) => {
const { core } = nodeService
// getTransaction function return type seems error
const tx = (await core.rpc.getTransaction(hash)) as any
const tx = (await core.rpc.getTransaction(hash)) as CKBComponents.TransactionWithStatus
if (!tx) {
return TransactionStatus.Failed
return {
tx,
status: TransactionStatus.Failed,
}
}
if (tx.txStatus.status === 'committed') {
return TransactionStatus.Success
return {
tx: tx.transaction,
status: TransactionStatus.Success,
}
}
return {
tx: tx.transaction,
status: TransactionStatus.Pending,
}
return TransactionStatus.Pending
}

/* eslint no-await-in-loop: "off" */
/* eslint no-restricted-syntax: "off" */
const trackingStatus = async () => {
const pendingTransactions = await FailedTransaction.pendings()
if (!pendingTransactions.length) {
Expand All @@ -30,20 +41,27 @@ const trackingStatus = async () => {
const pendingHashes = pendingTransactions.map(tx => tx.hash)
const txs = await Promise.all(
pendingHashes.map(async hash => {
const status = await getTransactionStatus(hash)
const txWithStatus = await getTransactionStatus(hash)
return {
hash,
status,
tx: txWithStatus.tx,
status: txWithStatus.status,
}
})
)
const failedTxs = txs.filter(tx => tx.status === TransactionStatus.Failed)
if (!failedTxs.length) {
return
const successTxs = txs.filter(tx => tx.status === TransactionStatus.Success)

if (failedTxs.length) {
const blake160s = await FailedTransaction.updateFailedTxs(failedTxs.map(tx => tx.hash))
const usedAddresses = blake160s.map(blake160 => LockUtils.blake160ToAddress(blake160))
AddressesUsedSubject.getSubject().next(usedAddresses)
}

for (const successTx of successTxs) {
const transaction = TypeConvert.toTransaction(successTx.tx)
await TransactionPersistor.saveFetchTx(transaction)
}
const blake160s = await FailedTransaction.updateFailedTxs(failedTxs.map(tx => tx.hash))
const usedAddresses = blake160s.map(blake160 => LockUtils.blake160ToAddress(blake160))
AddressesUsedSubject.getSubject().next(usedAddresses)
}

export const register = () => {
Expand Down

0 comments on commit a1203da

Please sign in to comment.