Skip to content

Commit

Permalink
feat: remove SkipDataAndType module
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Nov 11, 2019
1 parent 5a3f8a5 commit 1bc25eb
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 180 deletions.
10 changes: 4 additions & 6 deletions packages/neuron-wallet/src/database/address/dao.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,10 @@ export default class AddressDao {
const addressEntity = entity
addressEntity.txCount = txCount
const lockHashes: string[] = lockUtils.addressToAllLockHashes(addressEntity.address)
addressEntity.liveBalance = await CellsService.getBalance(lockHashes, OutputStatus.Live, true)
addressEntity.sentBalance = await CellsService.getBalance(lockHashes, OutputStatus.Sent, true)
addressEntity.pendingBalance = await CellsService.getBalance(lockHashes, OutputStatus.Pending, true)
const totalLiveBalance = await CellsService.getBalance(lockHashes, OutputStatus.Live, false)
const totalSentBalance = await CellsService.getBalance(lockHashes, OutputStatus.Sent, false)
addressEntity.totalBalance = (BigInt(totalLiveBalance) - BigInt(totalSentBalance)).toString()
addressEntity.liveBalance = await CellsService.getBalance(lockHashes, OutputStatus.Live)
addressEntity.sentBalance = await CellsService.getBalance(lockHashes, OutputStatus.Sent)
addressEntity.pendingBalance = await CellsService.getBalance(lockHashes, OutputStatus.Pending)
addressEntity.totalBalance = '0'
return addressEntity
})
)
Expand Down
42 changes: 13 additions & 29 deletions packages/neuron-wallet/src/services/cells.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,15 @@ import OutputEntity from 'database/chain/entities/output'
import { Cell, OutPoint, Input } from 'types/cell-types'
import { CapacityNotEnough, CapacityNotEnoughForChange } from 'exceptions'
import { OutputStatus } from './tx/params'
import SkipDataAndType from './settings/skip-data-and-type'

export const MIN_CELL_CAPACITY = '6100000000'

export default class CellsService {
// exclude hasData = true and typeScript != null
public static getBalance = async (
lockHashes: string[],
status: OutputStatus,
skipDataAndType: boolean
status: OutputStatus
): Promise<string> => {
const queryParams = {
lockHash: In(lockHashes),
status,
}

if (skipDataAndType) {
Object.assign(queryParams, {
hasData: false,
typeScript: null,
})
}

const cells: OutputEntity[] = await getConnection()
.getRepository(OutputEntity)
.createQueryBuilder('output')
Expand All @@ -36,7 +22,12 @@ export default class CellsService {
"output.typeScript",
"output.capacity"
])
.where(queryParams)
.where({
lockHash: In(lockHashes),
status,
hasData: false,
typeScript: null,
})
.getMany()

const capacity: bigint = cells.map(c => BigInt(c.capacity)).reduce((result, c) => result + c, BigInt(0))
Expand Down Expand Up @@ -95,23 +86,16 @@ export default class CellsService {
throw new Error(`capacity can't be less than ${MIN_CELL_CAPACITY}`)
}

const queryParams = {
lockHash: In(lockHashes),
status: OutputStatus.Live,
}
const skipDataAndType = SkipDataAndType.getInstance().get()
if (skipDataAndType) {
Object.assign(queryParams, {
hasData: false,
typeScript: null,
})
}

// only live cells, skip which has data or type
const cellEntities: OutputEntity[] = await getConnection()
.getRepository(OutputEntity)
.find({
where: queryParams,
where: {
lockHash: In(lockHashes),
status: OutputStatus.Live,
hasData: false,
typeScript: null,
},
})
cellEntities.sort((a, b) => {
const result = BigInt(a.capacity) - BigInt(b.capacity)
Expand Down
39 changes: 0 additions & 39 deletions packages/neuron-wallet/src/services/settings/skip-data-and-type.ts

This file was deleted.

51 changes: 4 additions & 47 deletions packages/neuron-wallet/tests/services/cells.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { OutputStatus } from '../../src/services/tx/params'
import { ScriptHashType, Script } from '../../src/types/cell-types'
import CellsService from '../../src/services/cells'
import { CapacityNotEnough, CapacityNotEnoughForChange } from '../../src/exceptions/wallet'
import SkipDataAndType from '../../src/services/settings/skip-data-and-type'

const randomHex = (length: number = 64): string => {
const str: string = Array.from({ length })
Expand Down Expand Up @@ -136,44 +135,30 @@ describe('CellsService', () => {
it('getBalance, Live, skip', async () => {
await createCells()

const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Live, true)
const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Live)
expect(balance).toEqual('100')
})

it('getBalance, Sent, skip', async () => {
await createCells()

const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Sent, true)
const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Sent)
expect(balance).toEqual('200')
})

it('getBalance, Live, not skip', async () => {
await createCells()

const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Live, false)
expect(balance).toEqual('11100')
})

it('getBalance, Pending, not skip', async () => {
await createCells()

const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Pending, false)
expect(balance).toEqual('33300')
})

it('getBalance with alice', async () => {
await createCells()
await createCell('2222', OutputStatus.Live, false, null, alice)

const balance: string = await CellsService.getBalance([alice.lockHash, bob.lockHash], OutputStatus.Live, true)
const balance: string = await CellsService.getBalance([alice.lockHash, bob.lockHash], OutputStatus.Live)
expect(balance).toEqual((100 + 2222).toString())
})

it(`get alice's balance`, async () => {
await createCells()
await createCell('2222', OutputStatus.Live, false, null, alice)

const balance: string = await CellsService.getBalance([alice.lockHash], OutputStatus.Live, true)
const balance: string = await CellsService.getBalance([alice.lockHash], OutputStatus.Live)
expect(balance).toEqual('2222')
})
})
Expand All @@ -191,7 +176,6 @@ describe('CellsService', () => {
}

it('1000, skip', async () => {
SkipDataAndType.getInstance().update(true)
await createCells()

const result = await CellsService.gatherInputs(toShannon('1000'), lockHashes)
Expand All @@ -200,7 +184,6 @@ describe('CellsService', () => {
})

it('1001, skip', async () => {
SkipDataAndType.getInstance().update(true)
await createCells()

let error
Expand All @@ -212,31 +195,7 @@ describe('CellsService', () => {
expect(error).toBeInstanceOf(CapacityNotEnough)
})

it('6000, not skip', async () => {
SkipDataAndType.getInstance().update(false)
await createCells()

const ckb = toShannon('6000')
const result = await CellsService.gatherInputs(ckb, lockHashes)

expect(result.capacities).toEqual(ckb)
})

it('6001, not skip', async () => {
SkipDataAndType.getInstance().update(false)
await createCells()

let error
try {
await CellsService.gatherInputs(toShannon('6001'), lockHashes)
} catch (e) {
error = e
}
expect(error).toBeInstanceOf(CapacityNotEnough)
})

it(`bob's and alice's cells`, async () => {
SkipDataAndType.getInstance().update(true)
await createCells()
await createCell(toShannon('5000'), OutputStatus.Live, false, null, alice)

Expand All @@ -246,7 +205,6 @@ describe('CellsService', () => {
})

it(`only bob's cells`, async () => {
SkipDataAndType.getInstance().update(true)
await createCells()
await createCell(toShannon('5000'), OutputStatus.Live, false, null, alice)

Expand Down Expand Up @@ -274,7 +232,6 @@ describe('CellsService', () => {

describe('skip, by feeRate 1000', () => {
beforeEach(async done => {
SkipDataAndType.getInstance().update(true)
const cells: OutputEntity[] = [
generateCell(toShannon('1000'), OutputStatus.Live, false, null),
generateCell(toShannon('2000'), OutputStatus.Live, false, null),
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { initConnection } from '../../../src/database/chain/ormconfig'
import { ScriptHashType, Script, TransactionWithoutHash } from '../../../src/types/cell-types'
import { OutputStatus } from '../../../src/services/tx/params'
import OutputEntity from '../../../src/database/chain/entities/output'
import SkipDataAndType from '../../../src/services/settings/skip-data-and-type'
import TransactionGenerator from '../../../src/services/tx/transaction-generator'
import LockUtils from '../../../src/models/lock-utils'
import CellsService from '../../../src/services/cells'
Expand Down Expand Up @@ -110,7 +109,6 @@ describe('TransactionGenerator', () => {

describe('generateTx', () => {
beforeEach(async done => {
SkipDataAndType.getInstance().update(true)
const cells: OutputEntity[] = [
generateCell(toShannon('1000'), OutputStatus.Live, false, null),
generateCell(toShannon('2000'), OutputStatus.Live, false, null),
Expand Down

0 comments on commit 1bc25eb

Please sign in to comment.