Skip to content

Commit 1bc25eb

Browse files
committed
feat: remove SkipDataAndType module
1 parent 5a3f8a5 commit 1bc25eb

6 files changed

Lines changed: 21 additions & 180 deletions

File tree

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,10 @@ export default class AddressDao {
7575
const addressEntity = entity
7676
addressEntity.txCount = txCount
7777
const lockHashes: string[] = lockUtils.addressToAllLockHashes(addressEntity.address)
78-
addressEntity.liveBalance = await CellsService.getBalance(lockHashes, OutputStatus.Live, true)
79-
addressEntity.sentBalance = await CellsService.getBalance(lockHashes, OutputStatus.Sent, true)
80-
addressEntity.pendingBalance = await CellsService.getBalance(lockHashes, OutputStatus.Pending, true)
81-
const totalLiveBalance = await CellsService.getBalance(lockHashes, OutputStatus.Live, false)
82-
const totalSentBalance = await CellsService.getBalance(lockHashes, OutputStatus.Sent, false)
83-
addressEntity.totalBalance = (BigInt(totalLiveBalance) - BigInt(totalSentBalance)).toString()
78+
addressEntity.liveBalance = await CellsService.getBalance(lockHashes, OutputStatus.Live)
79+
addressEntity.sentBalance = await CellsService.getBalance(lockHashes, OutputStatus.Sent)
80+
addressEntity.pendingBalance = await CellsService.getBalance(lockHashes, OutputStatus.Pending)
81+
addressEntity.totalBalance = '0'
8482
return addressEntity
8583
})
8684
)

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

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,15 @@ import OutputEntity from 'database/chain/entities/output'
33
import { Cell, OutPoint, Input } from 'types/cell-types'
44
import { CapacityNotEnough, CapacityNotEnoughForChange } from 'exceptions'
55
import { OutputStatus } from './tx/params'
6-
import SkipDataAndType from './settings/skip-data-and-type'
76

87
export const MIN_CELL_CAPACITY = '6100000000'
98

109
export default class CellsService {
1110
// exclude hasData = true and typeScript != null
1211
public static getBalance = async (
1312
lockHashes: string[],
14-
status: OutputStatus,
15-
skipDataAndType: boolean
13+
status: OutputStatus
1614
): Promise<string> => {
17-
const queryParams = {
18-
lockHash: In(lockHashes),
19-
status,
20-
}
21-
22-
if (skipDataAndType) {
23-
Object.assign(queryParams, {
24-
hasData: false,
25-
typeScript: null,
26-
})
27-
}
28-
2915
const cells: OutputEntity[] = await getConnection()
3016
.getRepository(OutputEntity)
3117
.createQueryBuilder('output')
@@ -36,7 +22,12 @@ export default class CellsService {
3622
"output.typeScript",
3723
"output.capacity"
3824
])
39-
.where(queryParams)
25+
.where({
26+
lockHash: In(lockHashes),
27+
status,
28+
hasData: false,
29+
typeScript: null,
30+
})
4031
.getMany()
4132

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

98-
const queryParams = {
99-
lockHash: In(lockHashes),
100-
status: OutputStatus.Live,
101-
}
102-
const skipDataAndType = SkipDataAndType.getInstance().get()
103-
if (skipDataAndType) {
104-
Object.assign(queryParams, {
105-
hasData: false,
106-
typeScript: null,
107-
})
108-
}
109-
11089
// only live cells, skip which has data or type
11190
const cellEntities: OutputEntity[] = await getConnection()
11291
.getRepository(OutputEntity)
11392
.find({
114-
where: queryParams,
93+
where: {
94+
lockHash: In(lockHashes),
95+
status: OutputStatus.Live,
96+
hasData: false,
97+
typeScript: null,
98+
},
11599
})
116100
cellEntities.sort((a, b) => {
117101
const result = BigInt(a.capacity) - BigInt(b.capacity)

packages/neuron-wallet/src/services/settings/skip-data-and-type.ts

Lines changed: 0 additions & 39 deletions
This file was deleted.

packages/neuron-wallet/tests/services/cells.test.ts

Lines changed: 4 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import { OutputStatus } from '../../src/services/tx/params'
55
import { ScriptHashType, Script } from '../../src/types/cell-types'
66
import CellsService from '../../src/services/cells'
77
import { CapacityNotEnough, CapacityNotEnoughForChange } from '../../src/exceptions/wallet'
8-
import SkipDataAndType from '../../src/services/settings/skip-data-and-type'
98

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

139-
const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Live, true)
138+
const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Live)
140139
expect(balance).toEqual('100')
141140
})
142141

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

146-
const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Sent, true)
145+
const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Sent)
147146
expect(balance).toEqual('200')
148147
})
149148

150-
it('getBalance, Live, not skip', async () => {
151-
await createCells()
152-
153-
const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Live, false)
154-
expect(balance).toEqual('11100')
155-
})
156-
157-
it('getBalance, Pending, not skip', async () => {
158-
await createCells()
159-
160-
const balance: string = await CellsService.getBalance(lockHashes, OutputStatus.Pending, false)
161-
expect(balance).toEqual('33300')
162-
})
163-
164149
it('getBalance with alice', async () => {
165150
await createCells()
166151
await createCell('2222', OutputStatus.Live, false, null, alice)
167152

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

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

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

193178
it('1000, skip', async () => {
194-
SkipDataAndType.getInstance().update(true)
195179
await createCells()
196180

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

202186
it('1001, skip', async () => {
203-
SkipDataAndType.getInstance().update(true)
204187
await createCells()
205188

206189
let error
@@ -212,31 +195,7 @@ describe('CellsService', () => {
212195
expect(error).toBeInstanceOf(CapacityNotEnough)
213196
})
214197

215-
it('6000, not skip', async () => {
216-
SkipDataAndType.getInstance().update(false)
217-
await createCells()
218-
219-
const ckb = toShannon('6000')
220-
const result = await CellsService.gatherInputs(ckb, lockHashes)
221-
222-
expect(result.capacities).toEqual(ckb)
223-
})
224-
225-
it('6001, not skip', async () => {
226-
SkipDataAndType.getInstance().update(false)
227-
await createCells()
228-
229-
let error
230-
try {
231-
await CellsService.gatherInputs(toShannon('6001'), lockHashes)
232-
} catch (e) {
233-
error = e
234-
}
235-
expect(error).toBeInstanceOf(CapacityNotEnough)
236-
})
237-
238198
it(`bob's and alice's cells`, async () => {
239-
SkipDataAndType.getInstance().update(true)
240199
await createCells()
241200
await createCell(toShannon('5000'), OutputStatus.Live, false, null, alice)
242201

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

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

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

275233
describe('skip, by feeRate 1000', () => {
276234
beforeEach(async done => {
277-
SkipDataAndType.getInstance().update(true)
278235
const cells: OutputEntity[] = [
279236
generateCell(toShannon('1000'), OutputStatus.Live, false, null),
280237
generateCell(toShannon('2000'), OutputStatus.Live, false, null),

packages/neuron-wallet/tests/services/settings/skip-data-and-type.test.ts

Lines changed: 0 additions & 57 deletions
This file was deleted.

packages/neuron-wallet/tests/services/tx/transaction-generator.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { initConnection } from '../../../src/database/chain/ormconfig'
33
import { ScriptHashType, Script, TransactionWithoutHash } from '../../../src/types/cell-types'
44
import { OutputStatus } from '../../../src/services/tx/params'
55
import OutputEntity from '../../../src/database/chain/entities/output'
6-
import SkipDataAndType from '../../../src/services/settings/skip-data-and-type'
76
import TransactionGenerator from '../../../src/services/tx/transaction-generator'
87
import LockUtils from '../../../src/models/lock-utils'
98
import CellsService from '../../../src/services/cells'
@@ -110,7 +109,6 @@ describe('TransactionGenerator', () => {
110109

111110
describe('generateTx', () => {
112111
beforeEach(async done => {
113-
SkipDataAndType.getInstance().update(true)
114112
const cells: OutputEntity[] = [
115113
generateCell(toShannon('1000'), OutputStatus.Live, false, null),
116114
generateCell(toShannon('2000'), OutputStatus.Live, false, null),

0 commit comments

Comments
 (0)