Skip to content

Commit

Permalink
Adding quantity field to Asset (#106)
Browse files Browse the repository at this point in the history
* Version 1.0.8-rc1

* Added `quantity` getter to access only string value without symbol

* Version 1.0.8-rc2

* Added `fromUnits` static call on Asset
  • Loading branch information
aaroncox authored Jun 25, 2024
1 parent cff9e10 commit 0608892
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@wharfkit/antelope",
"description": "Library for working with Antelope powered blockchains.",
"version": "1.0.7",
"version": "1.0.8-rc2",
"homepage": "https://github.com/wharfkit/antelope",
"license": "BSD-3-Clause-No-Military-License",
"main": "lib/antelope.js",
Expand Down
43 changes: 25 additions & 18 deletions src/chain/asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ export class Asset implements ABISerializableObject {
return new this(Int64.from(0), Asset.Symbol.abiDefault())
}

static formatUnits(units: Int64Type, precision: number) {
const digits = Int64.from(units).toString().split('')
let negative = false
if (digits[0] === '-') {
negative = true
digits.shift()
}
while (digits.length <= precision) {
digits.unshift('0')
}
if (precision > 0) {
digits.splice(digits.length - precision, 0, '.')
}
let rv = digits.join('')
if (negative) {
rv = '-' + rv
}
return rv
}

constructor(units: Int64, symbol: Asset.Symbol) {
this.units = units
this.symbol = symbol
Expand All @@ -80,30 +100,17 @@ export class Asset implements ABISerializableObject {
this.units = this.symbol.convertFloat(newValue)
}

get quantity(): string {
return Asset.formatUnits(this.units, this.symbol.precision)
}

toABI(encoder: ABIEncoder) {
this.units.toABI(encoder)
this.symbol.toABI(encoder)
}

toString() {
const digits = this.units.toString().split('')
let negative = false
if (digits[0] === '-') {
negative = true
digits.shift()
}
const p = this.symbol.precision
while (digits.length <= p) {
digits.unshift('0')
}
if (p > 0) {
digits.splice(digits.length - p, 0, '.')
}
let rv = digits.join('')
if (negative) {
rv = '-' + rv
}
return rv + ' ' + this.symbol.name
return this.quantity + ' ' + this.symbol.name
}

toJSON() {
Expand Down

0 comments on commit 0608892

Please sign in to comment.