Skip to content

Commit f455545

Browse files
committed
feat: calculate totalBalance and check skip data and type in gather inputs
1 parent 5746438 commit f455545

2 files changed

Lines changed: 40 additions & 16 deletions

File tree

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ export default class AddressDao {
5151
// sentBalance means balance of OutputStatus.Sent cells (sent to me but not committed)
5252
// pendingBalance means balance of OutputStatus.Pending cells (sent from me, but not committed)
5353
// so the final balance is (liveBalance + sentBalance - pendingBalance)
54+
// balance is the balance which skipped data and type
55+
// totalBalance means balance with data and type
5456
public static updateTxCountAndBalance = async (address: string): Promise<AddressEntity[]> => {
5557
const addressEntities = await getConnection()
5658
.getRepository(AddressEntity)
@@ -67,9 +69,12 @@ export default class AddressDao {
6769
const addressEntity = entity
6870
addressEntity.txCount = txCount
6971
const lockHashes: string[] = await LockUtils.addressToAllLockHashes(addressEntity.address)
70-
addressEntity.liveBalance = await CellsService.getBalance(lockHashes, OutputStatus.Live)
71-
addressEntity.sentBalance = await CellsService.getBalance(lockHashes, OutputStatus.Sent)
72-
addressEntity.pendingBalance = await CellsService.getBalance(lockHashes, OutputStatus.Pending)
72+
addressEntity.liveBalance = await CellsService.getBalance(lockHashes, OutputStatus.Live, true)
73+
addressEntity.sentBalance = await CellsService.getBalance(lockHashes, OutputStatus.Sent, true)
74+
addressEntity.pendingBalance = await CellsService.getBalance(lockHashes, OutputStatus.Pending, true)
75+
const totalLiveBalance = await CellsService.getBalance(lockHashes, OutputStatus.Live, false)
76+
const totalSentBalance = await CellsService.getBalance(lockHashes, OutputStatus.Sent, false)
77+
addressEntity.totalBalance = (BigInt(totalLiveBalance) - BigInt(totalSentBalance)).toString()
7378
return addressEntity
7479
})
7580
)

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

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

78
export const MIN_CELL_CAPACITY = '6100000000'
89

@@ -11,16 +12,27 @@ export const MIN_CELL_CAPACITY = '6100000000'
1112
/* eslint no-restricted-syntax: "warn" */
1213
export default class CellsService {
1314
// exclude hasData = true and typeScript != null
14-
public static getBalance = async (lockHashes: string[], status: OutputStatus): Promise<string> => {
15+
public static getBalance = async (
16+
lockHashes: string[],
17+
status: OutputStatus,
18+
skipDataAndType: boolean
19+
): Promise<string> => {
20+
const queryParams = {
21+
lockHash: In(lockHashes),
22+
status,
23+
}
24+
25+
if (skipDataAndType) {
26+
Object.assign(queryParams, {
27+
hasData: false,
28+
typeScript: null,
29+
})
30+
}
31+
1532
const cells: OutputEntity[] = await getConnection()
1633
.getRepository(OutputEntity)
1734
.find({
18-
where: {
19-
lockHash: In(lockHashes),
20-
hasData: false,
21-
typeScript: null,
22-
status,
23-
},
35+
where: queryParams,
2436
})
2537

2638
const capacity: bigint = cells.map(c => BigInt(c.capacity)).reduce((result, c) => result + c, BigInt(0))
@@ -70,16 +82,23 @@ export default class CellsService {
7082
throw new Error(`capacity can't be less than ${MIN_CELL_CAPACITY}`)
7183
}
7284

85+
const queryParams = {
86+
lockHashes: In(lockHashes),
87+
status: OutputStatus.Live,
88+
}
89+
const skipDataAndType = SkipDataAndType.getInstance().get()
90+
if (skipDataAndType) {
91+
Object.assign(queryParams, {
92+
hasData: false,
93+
typeScript: null,
94+
})
95+
}
96+
7397
// only live cells, skip which has data or type
7498
const cellEntities: OutputEntity[] = await getConnection()
7599
.getRepository(OutputEntity)
76100
.find({
77-
where: {
78-
lockHash: In(lockHashes),
79-
status: 'live',
80-
hasData: false,
81-
typeScript: null,
82-
},
101+
where: queryParams,
83102
})
84103
cellEntities.sort((a, b) => {
85104
const result = BigInt(a.capacity) - BigInt(b.capacity)

0 commit comments

Comments
 (0)