Skip to content

Commit

Permalink
Support symbol of value 0 (#98)
Browse files Browse the repository at this point in the history
* bug fix: support symbol(0)

* added more exceptions
  • Loading branch information
mschoenebeck authored Jan 20, 2024
1 parent 6945643 commit f09366a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/chain/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,12 @@ export namespace Asset {
return new Symbol(value)
}
const parts = value.split(',')
if (parts.length !== 2) {
if (parts.length !== 2 && value !== '0,') {
throw new Error('Invalid symbol string')
}
if (value === '0,') {
parts.push("")
}
const precision = Number.parseInt(parts[0])
return Symbol.fromParts(parts[1], precision)
}
Expand All @@ -152,7 +155,7 @@ export namespace Asset {
if (toSymbolPrecision(value) > Symbol.maxPrecision) {
throw new Error('Invalid asset symbol, precision too large')
}
if (!SymbolCode.pattern.test(toSymbolName(value))) {
if (value !== UInt64.from(0) && !SymbolCode.pattern.test(toSymbolName(value))) {
throw new Error('Invalid asset symbol, name must be uppercase A-Z')
}
this.value = value
Expand Down Expand Up @@ -229,7 +232,7 @@ export namespace Asset {
value: UInt64

constructor(value: UInt64) {
if (!SymbolCode.pattern.test(toSymbolName(value))) {
if (value !== UInt64.from(0) && !SymbolCode.pattern.test(toSymbolName(value))) {
throw new Error('Invalid asset symbol, name must be uppercase A-Z')
}
this.value = value
Expand Down
6 changes: 6 additions & 0 deletions test/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ suite('chain', function () {
assert.equal(symbol.precision, '10')
assert.equal(Asset.Symbol.from(symbol.value).toString(), symbol.toString())

const nft_symbol = Asset.Symbol.from(Asset.Symbol.from('0,'))
assert.equal(nft_symbol.name, '')
assert.equal(nft_symbol.precision, '0')
assert.equal(Asset.Symbol.from(nft_symbol.value).toString(), nft_symbol.toString())
assert.equal(nft_symbol.value, 0)

// test null asset
asset = Asset.from('0 ')
assert.equal(Number(asset.value), 0)
Expand Down

0 comments on commit f09366a

Please sign in to comment.