Skip to content

Commit

Permalink
fix: throw when capacity not enough for change
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Sep 21, 2019
1 parent d5a60d7 commit 3629787
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 3 deletions.
7 changes: 7 additions & 0 deletions packages/neuron-wallet/src/exceptions/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@ export class CapacityNotEnough extends Error {
}
}

export class CapacityNotEnoughForChange extends Error {
constructor() {
super(i18n.t('messages.capacity-not-enough-for-change'))
}
}

export class InvalidKeystore extends Error {
constructor() {
super(i18n.t('messages.invalid-keystore'))
Expand All @@ -48,5 +54,6 @@ export default {
EmptyPassword,
CodeHashNotLoaded,
CapacityNotEnough,
CapacityNotEnoughForChange,
InvalidKeystore,
}
1 change: 1 addition & 0 deletions packages/neuron-wallet/src/locales/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export default {
'invalid-mnemonic': 'Wallet seed is invalid',
'unsupported-cipher': 'Unsupported cipher',
'capacity-not-enough': 'Capacity is not enough',
'capacity-not-enough-for-change': 'Capacity not enough for change',
'capacity-too-small': 'Capacity less than min',
'should-be-type-of': '{{field}} should be type of {{type}}',
'invalid-keystore': 'Keystore is invalid',
Expand Down
1 change: 1 addition & 0 deletions packages/neuron-wallet/src/locales/zh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ export default {
'invalid-mnemonic': '助记词不合法',
'unsupported-cipher': '不支持的 Cipher',
'capacity-not-enough': '余额不足',
'capacity-not-enough-for-change': '余额不足以支付找零',
'capacity-too-small': '金额小于最低金额',
'should-be-type-of': '{{field}} 应该为 {{type}} 类型',
'invalid-keystore': 'Keystore 格式不正确',
Expand Down
9 changes: 7 additions & 2 deletions packages/neuron-wallet/src/services/cells.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getConnection, In } from 'typeorm'
import OutputEntity from 'database/chain/entities/output'
import { Cell, OutPoint, Input } from 'types/cell-types'
import { CapacityNotEnough } from 'exceptions'
import { CapacityNotEnough, CapacityNotEnoughForChange } from 'exceptions'
import { OutputStatus } from './tx/params'
import SkipDataAndType from './settings/skip-data-and-type'

Expand Down Expand Up @@ -129,10 +129,15 @@ export default class CellsService {
return true
})

if (inputCapacities < capacityInt) {
if (inputCapacities < totalCapacities) {
throw new CapacityNotEnough()
}

const diffCapacities = inputCapacities - totalCapacities
if (!(diffCapacities >= minChangeCapacity || diffCapacities === BigInt(0))) {
throw new CapacityNotEnoughForChange()
}

return {
inputs,
capacities: inputCapacities.toString(),
Expand Down
14 changes: 13 additions & 1 deletion packages/neuron-wallet/tests/services/cells.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import OutputEntity from '../../src/database/chain/entities/output'
import { OutputStatus } from '../../src/services/tx/params'
import { ScriptHashType, Script } from '../../src/types/cell-types'
import CellsService from '../../src/services/cells'
import { CapacityNotEnough } from '../../src/exceptions/wallet'
import { CapacityNotEnough, CapacityNotEnoughForChange } from '../../src/exceptions/wallet'
import SkipDataAndType from '../../src/services/settings/skip-data-and-type'

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

expect(error).toBeInstanceOf(CapacityNotEnough)
})

it('capacity not enough for change', async () => {
await createCell(toShannon('100'), OutputStatus.Live, false, null)
let error
try {
await CellsService.gatherInputs(toShannon('77'), [bob.lockHash])
} catch (e) {
error = e
}

expect(error).toBeInstanceOf(CapacityNotEnoughForChange)
})
})
})

0 comments on commit 3629787

Please sign in to comment.