Overview
The Asset class represents Hive blockchain assets with proper precision and symbol handling. It supports the three main asset types: HIVE, HBD (Hive Backed Dollars), and VESTS (vesting shares).
Assets are formatted as amount symbol (e.g., "1.000 HIVE", "12.500 HBD", "1234.567890 VESTS") with type-appropriate precision.
Constructor
const asset = new Asset(amount, symbol)
Numeric amount of the asset
Asset symbol: 'HIVE', 'HBD', 'VESTS', or legacy 'STEEM', 'SBD', 'TESTS', 'TBD'
The constructor automatically converts 'HIVE' to 'STEEM' and 'HBD' to 'SBD' internally for serialization compatibility.
Example
import { Asset } from 'hive-tx'
// Create HIVE asset
const hive = new Asset(10.5, 'HIVE')
console.log(hive.toString()) // '10.500 STEEM'
// Create HBD asset
const hbd = new Asset(25.123, 'HBD')
console.log(hbd.toString()) // '25.123 SBD'
// Create VESTS asset
const vests = new Asset(1000000.123456, 'VESTS')
console.log(vests.toString()) // '1000000.123456 VESTS'
Static Methods
fromString
Creates a new Asset instance from a string representation.
const asset = Asset.fromString(assetString, expectedSymbol?)
Asset string in format "amount symbol" (e.g., "42.000 HIVE")
expectedSymbol
string | null
default:"null"
Optional expected symbol for validation. Throws error if actual symbol doesn’t match.
New Asset instance parsed from the string
Example
import { Asset } from 'hive-tx'
// Parse HIVE asset
const hive = Asset.fromString('10.000 HIVE')
console.log('Amount:', hive.amount) // 10
console.log('Symbol:', hive.symbol) // 'STEEM' (internal representation)
// Parse HBD asset
const hbd = Asset.fromString('25.500 HBD')
console.log(hbd.toString()) // '25.500 SBD'
// Parse with validation
const validated = Asset.fromString('100.000 HIVE', 'HIVE')
// This throws an error
try {
Asset.fromString('100.000 HBD', 'HIVE')
} catch (error) {
console.error(error.message)
// Output: Invalid asset, expected symbol: HIVE got: SBD
}
from
Convenience method to create a new Asset from various input types.
const asset = Asset.from(value, symbol?)
value
number | string | Asset
required
Value to convert to Asset:
number: Creates asset with specified symbol
string: Parses asset string
Asset: Returns as-is or validates symbol
Symbol to use for number values or validate against
New or existing Asset instance
Example
import { Asset } from 'hive-tx'
// From number (requires symbol)
const asset1 = Asset.from(42, 'HIVE')
console.log(asset1.toString()) // '42.000 STEEM'
// From string
const asset2 = Asset.from('25.500 HBD')
console.log(asset2.amount) // 25.5
// From existing Asset (validates symbol)
const asset3 = Asset.from(asset1, 'HIVE')
console.log(asset3 === asset1) // true (if symbol matches internally)
// From Asset with validation
try {
Asset.from(asset1, 'HBD') // asset1 is HIVE
} catch (error) {
console.error(error.message)
// Output: Invalid asset, expected symbol: HBD got: STEEM
}
// Flexible function parameter
function processAsset(value: number | string | Asset) {
const asset = Asset.from(value, 'HIVE')
return asset.amount * 2
}
console.log(processAsset(10)) // 20
console.log(processAsset('5.000 HIVE')) // 10
console.log(processAsset(asset1)) // 84
Instance Methods
getPrecision
Returns the decimal precision for the asset type.
const precision = asset.getPrecision()
Decimal precision:
3 for HIVE, HBD, STEEM, SBD, TESTS, TBD
6 for VESTS
Example
import { Asset } from 'hive-tx'
const hive = new Asset(10, 'HIVE')
console.log(hive.getPrecision()) // 3
const vests = new Asset(1000000, 'VESTS')
console.log(vests.getPrecision()) // 6
// Precision determines formatting
const amount = 12.1234567
const hiveAsset = new Asset(amount, 'HIVE')
const vestsAsset = new Asset(amount, 'VESTS')
console.log(hiveAsset.toString()) // '12.123 STEEM' (3 decimals)
console.log(vestsAsset.toString()) // '12.123457 VESTS' (6 decimals)
toString
Returns a string representation of the asset with proper precision.
const assetString = asset.toString()
Formatted asset string: "amount symbol" with type-appropriate decimal places
Example
import { Asset } from 'hive-tx'
// HIVE/HBD with 3 decimal places
const hive = new Asset(10.5, 'HIVE')
console.log(hive.toString()) // '10.500 STEEM'
const hbd = new Asset(25.1, 'HBD')
console.log(hbd.toString()) // '25.100 SBD'
// VESTS with 6 decimal places
const vests = new Asset(1234.56, 'VESTS')
console.log(vests.toString()) // '1234.560000 VESTS'
// Rounding applied automatically
const rounded = new Asset(10.123456789, 'HIVE')
console.log(rounded.toString()) // '10.123 STEEM'
toJSON
Returns JSON representation of the asset (same as toString()).
const json = asset.toJSON()
Asset string representation
Example
import { Asset } from 'hive-tx'
const hive = new Asset(10, 'HIVE')
// Called automatically by JSON.stringify
const json = JSON.stringify({ balance: hive })
console.log(json)
// Output: {"balance":"10.000 STEEM"}
// Manual call
console.log(hive.toJSON()) // '10.000 STEEM'
Supported Asset Types
HIVE (Liquid HIVE)
import { Asset } from 'hive-tx'
const hive = Asset.from('10.000 HIVE')
console.log('Amount:', hive.amount) // 10
console.log('Symbol:', hive.symbol) // 'STEEM' (internal)
console.log('Precision:', hive.getPrecision()) // 3
console.log('String:', hive.toString()) // '10.000 STEEM'
HBD (Hive Backed Dollars)
import { Asset } from 'hive-tx'
const hbd = Asset.from('25.500 HBD')
console.log('Amount:', hbd.amount) // 25.5
console.log('Symbol:', hbd.symbol) // 'SBD' (internal)
console.log('Precision:', hbd.getPrecision()) // 3
console.log('String:', hbd.toString()) // '25.500 SBD'
VESTS (Vesting Shares / Hive Power)
import { Asset } from 'hive-tx'
const vests = Asset.from('1234567.123456 VESTS')
console.log('Amount:', vests.amount) // 1234567.123456
console.log('Symbol:', vests.symbol) // 'VESTS'
console.log('Precision:', vests.getPrecision()) // 6
console.log('String:', vests.toString()) // '1234567.123456 VESTS'
Usage in Operations
Assets are commonly used in Hive operations:
Transfer Operation
import { Transaction, PrivateKey, Asset } from 'hive-tx'
async function sendTransfer() {
const tx = new Transaction()
// Using asset string directly
await tx.addOperation('transfer', {
from: 'alice',
to: 'bob',
amount: '10.000 HIVE',
memo: 'Payment'
})
// Or using Asset class
const amount = new Asset(10, 'HIVE')
await tx.addOperation('transfer', {
from: 'alice',
to: 'carol',
amount: amount.toString(),
memo: 'Payment'
})
const key = PrivateKey.fromLogin('alice', 'password', 'active')
tx.sign(key)
const result = await tx.broadcast()
console.log('Transfers sent:', result.tx_id)
}
sendTransfer()
Transfer to Vesting (Power Up)
import { Transaction, PrivateKey, Asset } from 'hive-tx'
async function powerUp() {
const tx = new Transaction()
const amount = Asset.from(100, 'HIVE')
await tx.addOperation('transfer_to_vesting', {
from: 'alice',
to: 'alice',
amount: amount.toString()
})
const key = PrivateKey.fromLogin('alice', 'password', 'active')
tx.sign(key)
await tx.broadcast()
console.log('Powered up:', amount.toString())
}
powerUp()
Withdraw Vesting (Power Down)
import { Transaction, PrivateKey, Asset } from 'hive-tx'
async function powerDown() {
const tx = new Transaction()
// Specify VESTS amount to power down
const vests = Asset.from(1000000, 'VESTS')
await tx.addOperation('withdraw_vesting', {
account: 'alice',
vesting_shares: vests.toString()
})
const key = PrivateKey.fromLogin('alice', 'password', 'active')
tx.sign(key)
await tx.broadcast()
console.log('Power down initiated:', vests.toString())
}
powerDown()
Asset Arithmetic
import { Asset } from 'hive-tx'
// Create assets
const asset1 = Asset.from('10.000 HIVE')
const asset2 = Asset.from('5.500 HIVE')
// Manual arithmetic on amounts
const sum = new Asset(asset1.amount + asset2.amount, 'HIVE')
console.log('Sum:', sum.toString()) // '15.500 STEEM'
const difference = new Asset(asset1.amount - asset2.amount, 'HIVE')
console.log('Difference:', difference.toString()) // '4.500 STEEM'
const product = new Asset(asset1.amount * 2, 'HIVE')
console.log('Double:', product.toString()) // '20.000 STEEM'
// Division
const quotient = new Asset(asset1.amount / 2, 'HIVE')
console.log('Half:', quotient.toString()) // '5.000 STEEM'
Validation and Error Handling
import { Asset } from 'hive-tx'
try {
// Invalid symbol
Asset.fromString('10.000 INVALID')
} catch (error) {
console.error(error.message)
// Output: Invalid asset symbol: INVALID
}
try {
// Invalid amount
Asset.fromString('abc HIVE')
} catch (error) {
console.error(error.message)
// Output: Invalid asset amount: abc
}
try {
// Symbol mismatch
Asset.fromString('10.000 HBD', 'HIVE')
} catch (error) {
console.error(error.message)
// Output: Invalid asset, expected symbol: HIVE got: SBD
}
try {
// Invalid type
Asset.from({} as any)
} catch (error) {
console.error(error.message)
// Output: Invalid asset '[object Object]'
}
Parsing Account Balances
import { Asset } from 'hive-tx'
import { Client } from '@hiveio/dhive'
const client = new Client(['https://api.hive.blog'])
async function getAccountBalances(username: string) {
const [account] = await client.database.getAccounts([username])
if (!account) {
throw new Error(`Account ${username} not found`)
}
// Parse balances
const hiveBalance = Asset.fromString(account.balance, 'HIVE')
const hbdBalance = Asset.fromString(account.hbd_balance, 'HBD')
const vestingShares = Asset.fromString(account.vesting_shares, 'VESTS')
console.log('Balances for', username)
console.log('HIVE:', hiveBalance.amount)
console.log('HBD:', hbdBalance.amount)
console.log('VESTS:', vestingShares.amount)
// Calculate total in HIVE (requires conversion rate)
const props = await client.database.getDynamicGlobalProperties()
const totalVests = Asset.fromString(props.total_vesting_shares).amount
const totalHive = Asset.fromString(props.total_vesting_fund_hive).amount
const vestToHive = totalHive / totalVests
const hivePower = vestingShares.amount * vestToHive
console.log('Hive Power (HP):', hivePower.toFixed(3))
}
getAccountBalances('alice')
Properties
Numeric amount of the asset
Asset symbol (internally stored as STEEM, SBD, or VESTS for serialization)
Legacy Symbol Handling
Symbol Conversion:The Asset class internally converts:
'HIVE' → 'STEEM'
'HBD' → 'SBD'
This is for blockchain serialization compatibility. When you create assets with ‘HIVE’ or ‘HBD’, they’re stored as ‘STEEM’ or ‘SBD’ internally, but this is transparent to most use cases.
import { Asset } from 'hive-tx'
const hive = new Asset(10, 'HIVE')
console.log('Internal symbol:', hive.symbol) // 'STEEM'
console.log('String representation:', hive.toString()) // '10.000 STEEM'
// When parsing, use HIVE/HBD
const parsed = Asset.fromString('10.000 HIVE')
console.log('Parsed symbol:', parsed.symbol) // 'STEEM'
See Also