Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support symbol of value 0 #98

Merged
merged 2 commits into from
Jan 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading