Skip to content

Commit 3629787

Browse files
committed
fix: throw when capacity not enough for change
1 parent d5a60d7 commit 3629787

5 files changed

Lines changed: 29 additions & 3 deletions

File tree

packages/neuron-wallet/src/exceptions/wallet.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ export class CapacityNotEnough extends Error {
3535
}
3636
}
3737

38+
export class CapacityNotEnoughForChange extends Error {
39+
constructor() {
40+
super(i18n.t('messages.capacity-not-enough-for-change'))
41+
}
42+
}
43+
3844
export class InvalidKeystore extends Error {
3945
constructor() {
4046
super(i18n.t('messages.invalid-keystore'))
@@ -48,5 +54,6 @@ export default {
4854
EmptyPassword,
4955
CodeHashNotLoaded,
5056
CapacityNotEnough,
57+
CapacityNotEnoughForChange,
5158
InvalidKeystore,
5259
}

packages/neuron-wallet/src/locales/en.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export default {
9191
'invalid-mnemonic': 'Wallet seed is invalid',
9292
'unsupported-cipher': 'Unsupported cipher',
9393
'capacity-not-enough': 'Capacity is not enough',
94+
'capacity-not-enough-for-change': 'Capacity not enough for change',
9495
'capacity-too-small': 'Capacity less than min',
9596
'should-be-type-of': '{{field}} should be type of {{type}}',
9697
'invalid-keystore': 'Keystore is invalid',

packages/neuron-wallet/src/locales/zh.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ export default {
9090
'invalid-mnemonic': '助记词不合法',
9191
'unsupported-cipher': '不支持的 Cipher',
9292
'capacity-not-enough': '余额不足',
93+
'capacity-not-enough-for-change': '余额不足以支付找零',
9394
'capacity-too-small': '金额小于最低金额',
9495
'should-be-type-of': '{{field}} 应该为 {{type}} 类型',
9596
'invalid-keystore': 'Keystore 格式不正确',

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { getConnection, In } from 'typeorm'
22
import OutputEntity from 'database/chain/entities/output'
33
import { Cell, OutPoint, Input } from 'types/cell-types'
4-
import { CapacityNotEnough } from 'exceptions'
4+
import { CapacityNotEnough, CapacityNotEnoughForChange } from 'exceptions'
55
import { OutputStatus } from './tx/params'
66
import SkipDataAndType from './settings/skip-data-and-type'
77

@@ -129,10 +129,15 @@ export default class CellsService {
129129
return true
130130
})
131131

132-
if (inputCapacities < capacityInt) {
132+
if (inputCapacities < totalCapacities) {
133133
throw new CapacityNotEnough()
134134
}
135135

136+
const diffCapacities = inputCapacities - totalCapacities
137+
if (!(diffCapacities >= minChangeCapacity || diffCapacities === BigInt(0))) {
138+
throw new CapacityNotEnoughForChange()
139+
}
140+
136141
return {
137142
inputs,
138143
capacities: inputCapacities.toString(),

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

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import OutputEntity from '../../src/database/chain/entities/output'
44
import { OutputStatus } from '../../src/services/tx/params'
55
import { ScriptHashType, Script } from '../../src/types/cell-types'
66
import CellsService from '../../src/services/cells'
7-
import { CapacityNotEnough } from '../../src/exceptions/wallet'
7+
import { CapacityNotEnough, CapacityNotEnoughForChange } from '../../src/exceptions/wallet'
88
import SkipDataAndType from '../../src/services/settings/skip-data-and-type'
99

1010
const randomHex = (length: number = 64): string => {
@@ -259,5 +259,17 @@ describe('CellsService', () => {
259259

260260
expect(error).toBeInstanceOf(CapacityNotEnough)
261261
})
262+
263+
it('capacity not enough for change', async () => {
264+
await createCell(toShannon('100'), OutputStatus.Live, false, null)
265+
let error
266+
try {
267+
await CellsService.gatherInputs(toShannon('77'), [bob.lockHash])
268+
} catch (e) {
269+
error = e
270+
}
271+
272+
expect(error).toBeInstanceOf(CapacityNotEnoughForChange)
273+
})
262274
})
263275
})

0 commit comments

Comments
 (0)