Skip to content

Commit

Permalink
fix: connection not found when delete wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Jul 31, 2019
1 parent 8beef45 commit 2afc7e7
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
2 changes: 2 additions & 0 deletions packages/neuron-wallet/src/database/chain/ormconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import env from '../../env'
import { InitMigration1561695143591 } from './migrations/1561695143591-InitMigration'
import { AddStatusToTx1562038960990 } from './migrations/1562038960990-AddStatusToTx'

export const CONNECTION_NOT_FOUND_NAME = 'ConnectionNotFoundError'

const dbPath = (networkName: string): string => {
const name = `cell-${networkName}.sqlite`
return path.join(env.fileBasePath, 'cells', name)
Expand Down
11 changes: 10 additions & 1 deletion packages/neuron-wallet/src/listeners/tx-status.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { remote } from 'electron'
import { interval } from 'rxjs'
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 { CONNECTION_NOT_FOUND_NAME } from '../database/chain/ormconfig'

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

Expand Down Expand Up @@ -47,7 +49,14 @@ const trackingStatus = async () => {
export const register = () => {
// every 5 seconds
interval(5000).subscribe(async () => {
await trackingStatus()
try {
getConnection()
await trackingStatus()
} catch (err) {
if (err.name !== CONNECTION_NOT_FOUND_NAME) {
throw err
}
}
})
}

Expand Down
26 changes: 26 additions & 0 deletions packages/neuron-wallet/src/services/tx/transaction-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { pubkeyToAddress } from '@nervosnetwork/ckb-sdk-utils'
import { Transaction, TransactionWithoutHash, TransactionStatus } from '../../types/cell-types'
import TransactionEntity from '../../database/chain/entities/transaction'
import LockUtils from '../../models/lock-utils'
import { CONNECTION_NOT_FOUND_NAME } from '../../database/chain/ormconfig'

export interface TransactionsByAddressesParam {
pageNo: number
Expand Down Expand Up @@ -122,6 +123,20 @@ export class TransactionsService {
params: TransactionsByLockHashesParam,
searchValue: string = ''
): Promise<PaginationResult<Transaction>> => {
try {
// if connection not found, may means no database to connection
// it will happend when first connection to database and no network.
getConnection()
} catch (err) {
if (err.name === CONNECTION_NOT_FOUND_NAME) {
return {
totalCount: 0,
items: [],
}
}
throw err
}

const skip = (params.pageNo - 1) * params.pageSize

const type = TransactionsService.filterSearchType(searchValue)
Expand Down Expand Up @@ -224,6 +239,17 @@ export class TransactionsService {
}

public static get = async (hash: string): Promise<Transaction | undefined> => {
try {
// if connection not found, may means no database to connection
// it will happend when first connection to database and no network.
getConnection()
} catch (err) {
if (err.name === CONNECTION_NOT_FOUND_NAME) {
return undefined
}
throw err
}

const tx = await getConnection()
.getRepository(TransactionEntity)
.findOne(hash, { relations: ['inputs', 'outputs'] })
Expand Down

0 comments on commit 2afc7e7

Please sign in to comment.