Asset Conversions
Asset conversions are required for various Hive financial transactions. The @hiveio/wax library provides methods like VESTS to HP and HBD to Hive to facilitate these conversions.
VESTS to HP conversion
This method converts VESTS into Hive Power (HP). This conversion requires three NaiAsset instances or values convertible to NaiAsset (number, string or bigint for JavaScript): one for the VESTS and two others for the total vesting fund Hive and total vesting shares.
This method takes 3 parameters:
- vests: The amount of VESTS to convert.
- total vesting fund: The total amount of Hive in the vesting fund (Retrieved from the
database_api.get_dynamic_global_propertiesAPI call -total_vesting_fund_hiveproperty). - total vesting shares: The total amount of vesting shares (Retrieved from the
database_api.get_dynamic_global_propertiesAPI call -total_vesting_sharesproperty).
import { createWaxFoundation } from '@hiveio/wax';
// Create a Wax Foundation instance
const waxApi = await createWaxFoundation();
// Assume these amounts represent the vests, totalVestingFundHive, and totalVestingShares
const vestsAmount = 1_000000;
const totalVestingFundHiveAmount = 20_000;
const totalVestingSharesAmount = 5_000000;
// Convert amounts to `NaiAsset`
const vestsAsset = waxApi.vestsSatoshis(vestsAmount);
const totalVestingFundHiveAsset = waxApi.hiveSatoshis(totalVestingFundHiveAmount);
const totalVestingSharesAsset = waxApi.vestsSatoshis(totalVestingSharesAmount);
// Use `vestsToHp` to perform the conversion
const hpAsset = waxApi.vestsToHp(
vestsAsset,
totalVestingFundHiveAsset,
totalVestingSharesAsset
);
console.log(`HP Asset: ${JSON.stringify(hpAsset)}`);
You can also perform the reverse conversion from HP to VESTS using the hpToVests method.
from wax import create_wax_foundation
# Create a Wax Foundation instance
wax = create_wax_foundation()
# Assume these amounts represent the vests, total_vesting_fund_hive_amount, and total_vesting_shares_amount
vests_asset = 1_000000
total_vesting_fund_hive_amount = 20_000
total_vesting_shares_amount = 5_000000
# Convert amounts to `NaiAsset`
vests_asset = wax.vests.satoshis(vests_asset)
total_vesting_fund_hive_amount = wax.hive.satoshis(total_vesting_fund_hive_amount)
total_vesting_shares_amount = wax.vests.satoshis(total_vesting_shares_amount)
# Use `vests_to_hp` to perform the conversion
hp_asset = wax.vests_to_hp(vests_asset, total_vesting_fund_hive_amount, total_vesting_shares_amount)
print(f"HP Asset: \n\n{hp_asset}")
use wax::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a Wax Foundation instance
let wax_api = create_wax_foundation(None);
// Assume these amounts represent the vests, totalVestingFundHive, and totalVestingShares
let vests_amount = 1_000000;
let total_vesting_fund_hive_amount = 20_000;
let total_vesting_shares_amount = 5_000000;
// Convert amounts to `NaiAsset`
let vests_asset = wax_api.vests_satoshis(vests_amount)?;
let total_vesting_fund_hive_asset =
wax_api.hive_satoshis(total_vesting_fund_hive_amount)?;
let total_vesting_shares_asset =
wax_api.vests_satoshis(total_vesting_shares_amount)?;
// Use `vests_to_hp` to perform the conversion
let hp_asset = wax_api.vests_to_hp(
&vests_asset,
&total_vesting_fund_hive_asset,
&total_vesting_shares_asset,
)?;
println!("HP Asset: {hp_asset:?}");
Ok(())
}
HBD to HIVE conversion
This method converts HBD into Hive. This conversion requires three NaiAsset instances or values convertible to NaiAsset (number, string or bigint for JavaScript): one for the HBD, one for the base (another HBD asset), and one for the quote (a HIVE asset).
This method takes 3 parameters:
- hbd: The amount of HBD to convert.
- base: The base asset (Retrieved from the
database_api.get_current_price_feedAPI call -baseproperty). - quote: The quote asset (Retrieved from the
database_api.get_current_price_feedAPI call -quoteproperty).
import { createWaxFoundation } from '@hiveio/wax';
// Create a Wax Foundation instance
const waxApi = await createWaxFoundation();
// Assume these amounts represent the HBD, base, and quote
const hbdAmount = 1_000;
const baseAmount = 1_500;
const quoteAmount = 2_000;
// Convert amounts to `NaiAsset`
const hbdAsset = waxApi.hbdSatoshis(hbdAmount);
const baseAsset = waxApi.hbdSatoshis(baseAmount);
const quoteAsset = waxApi.hiveSatoshis(quoteAmount);
// Use `hbdToHive` to perform the conversion
const hiveAsset = waxApi.hbdToHive(hbdAsset, baseAsset, quoteAsset);
console.log(`Converted Hive Asset: ${JSON.stringify(hiveAsset)}`);
You can also perform the reverse conversion from HIVE to HBD using the hiveToHbd method.
from wax import create_wax_foundation
# Create a Wax Foundation instance
wax = create_wax_foundation()
# Assume these amounts represent the HBD, base, and quote
hbd_amount = 1_000
base_amount = 1_500
quote_amount = 2_000
# Convert amounts to `NaiAsset`
hbd_asset = wax.hbd.satoshis(hbd_amount)
base_asset = wax.hbd.satoshis(base_amount)
quote_asset = wax.hive.satoshis(quote_amount)
# Use `hbd_to_hive` to perform the conversion
hive_asset = wax.hbd_to_hive(hbd_asset, base_asset, quote_asset)
print(f"Converted Hive Asset: {hive_asset}")
use wax::prelude::*;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a Wax Foundation instance
let wax_api = create_wax_foundation(None);
// Assume these amounts represent the HBD, base, and quote
let hbd_amount = 1_000;
let base_amount = 1_500;
let quote_amount = 2_000;
// Convert amounts to `NaiAsset`
let hbd_asset = wax_api.hbd_satoshis(hbd_amount)?;
let base_asset = wax_api.hbd_satoshis(base_amount)?;
let quote_asset = wax_api.hive_satoshis(quote_amount)?;
// Use `hbd_to_hive` to perform the conversion
let hive_asset = wax_api.hbd_to_hive(&hbd_asset, &base_asset, "e_asset)?;
println!("Converted Hive Asset: {hive_asset:?}");
Ok(())
}