Blockchain Data Formatters

Formatting a large number

Using Wax Formatters, you can format numbers represented as string, number, BigInt using formatNumber method

import { createHiveChain } from '@hiveio/wax';

const chain = await createHiveChain();

const output = chain.formatter.formatNumber(76543212345678, 3, "en-US");

console.log(output);
76,543,212,345,678.000

Not implemented yet — planned for a future release.

format_number accepts any amount convertible to a string (integers, floats, decimal strings). There is no locale argument — group separators default to the en-US style and can be changed with WaxFormatterOptions::default().with_separators(...).

use wax::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let wax_api = create_wax_foundation(None);

    // Group separators default to the en-US style (`,` and `.`); use
    // `WaxFormatterOptions::default().with_separators(...)` to change them
    let output = wax_api.formatter().format_number(76543212345678i64, 3);

    println!("{output}");

    Ok(())
}
76,543,212,345,678.000

Formatting NAI Asset

If you want to format a string, use waxify literal

import { createHiveChain } from '@hiveio/wax';

const chain = await createHiveChain();

// Data from blockchain
const naiAsset = {
  amount: "300000",
  precision: 3,
  nai: "@@000000021"
};

const output = chain.waxify`Amount: ${naiAsset}`;

console.log(output);
Amount: 300.000 HIVE

Not implemented yet — planned for a future release.

Rust has no tagged template literals — display is the counterpart of waxify: it formats the value and renders it for display, ready to be composed with the format! family.

Test it yourself on github codespace: src/rust/formatters/blockchain_formatters/format_nai.rs
use serde_json::json;
use wax::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let wax_api = create_wax_foundation(None);

    // Data from blockchain
    let nai_asset = json!({
        "amount": "300000",
        "precision": 3,
        "nai": "@@000000021"
    });

    // `display` is the Rust counterpart of the TS `waxify` literal — compose
    // it with the `format!` family
    let output = format!("Amount: {}", wax_api.formatter().display(&nai_asset)?);

    println!("{output}");

    Ok(())
}
Amount: 300.000 HIVE

Formatting Transaction

import { createHiveChain } from '@hiveio/wax';

const chain = await createHiveChain();

// Data from blockchain
const tx = {
  ref_block_num: 1959,
  ref_block_prefix: 3625727107,
  expiration: "2023-11-09T22:01:24",
  operations: [
    {
      type: "transfer_operation",
      value: {
        from: "oneplus7",
        to: "kryptogames",
        amount: {
          amount: "300000",
          precision: 3,
          nai: "@@000000021"
        },
        memo: "Roll under 50 4d434bd943616"
      }
    }
  ],
  extensions: [],
  signatures: []
};

const output = chain.waxify`Tx: #${tx}`;

console.log(output);
Tx: #3725c81634f152011e2043eb7119911b953d4267

Not implemented yet — planned for a future release.

Test it yourself on github codespace: src/rust/formatters/blockchain_formatters/format_tx.rs
use serde_json::json;
use wax::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let wax_api = create_wax_foundation(None);

    // Data from blockchain
    let tx = json!({
        "ref_block_num": 1959,
        "ref_block_prefix": 3625727107u32,
        "expiration": "2023-11-09T22:01:24",
        "operations": [
            {
                "type": "transfer_operation",
                "value": {
                    "from": "oneplus7",
                    "to": "kryptogames",
                    "amount": {
                        "amount": "300000",
                        "precision": 3,
                        "nai": "@@000000021"
                    },
                    "memo": "Roll under 50 4d434bd943616"
                }
            }
        ],
        "extensions": [],
        "signatures": []
    });

    let output = format!("Tx: #{}", wax_api.formatter().display(&tx)?);

    println!("{output}");

    Ok(())
}
Tx: #3725c81634f152011e2043eb7119911b953d4267

Formatting entire transaction without replacing with transaction id

Sometimes there is a need to convert an entire transaction object, but you do not want it to be replaced with the string. You can achieve that by using format method and extending the default Wax Formatter with your custom options, e.g.:

import { createHiveChain } from '@hiveio/wax';

const chain = await createHiveChain();

// Data from blockchain
const tx = {
  ref_block_num: 1959,
  ref_block_prefix: 3625727107,
  expiration: "2023-11-09T22:01:24",
  operations: [
    {
      type: "transfer_operation",
      value: {
        from: "oneplus7",
        to: "kryptogames",
        amount: {
          amount: "300000",
          precision: 3,
          nai: "@@000000021"
        },
        memo: "Roll under 50 4d434bd943616"
      }
    }
  ],
  extensions: []
};

const formatter = chain.formatter.extend({ transaction: { displayAsId: false } });

const output = formatter.format(tx);

console.log(output);
{
  ref_block_num: 1959,
  ref_block_prefix: 3625727107,
  expiration: "2023-11-09T22:01:24",
  operations: [
    {
      type: "transfer_operation",
      value: {
        from: "oneplus7",
        to: "kryptogames",
        amount: {
          amount: "300000",
          precision: 3,
          nai: "@@000000021"
        },
        memo: "Roll under 50 4d434bd943616"
      }
    }
  ],
  extensions: []
}

Not implemented yet — planned for a future release.

In Rust the options-only extension is a separate method: extend_options derives a new formatter with the same rules but the given options (extend::<F>() is the rule-extension form). format returns the rewritten tree as plain serde_json::Value data.

use serde_json::json;
use wax::prelude::*;

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let wax_api = create_wax_foundation(None);

    // Data from blockchain
    let tx = json!({
        "ref_block_num": 1959,
        "ref_block_prefix": 3625727107u32,
        "expiration": "2023-11-09T22:01:24",
        "operations": [
            {
                "type": "transfer_operation",
                "value": {
                    "from": "oneplus7",
                    "to": "kryptogames",
                    "amount": {
                        "amount": "300000",
                        "precision": 3,
                        "nai": "@@000000021"
                    },
                    "memo": "Roll under 50 4d434bd943616"
                }
            }
        ],
        "extensions": []
    });

    // Derive a formatter that keeps the transaction object instead of
    // replacing it with the transaction id
    let formatter = wax_api.formatter().extend_options(
        WaxFormatterOptions::default().with_transaction_as_id(false),
    );

    let output = formatter.format(&tx)?;

    println!("{}", serde_json::to_string_pretty(&output)?);

    Ok(())
}
{
  "expiration": "2023-11-09T22:01:24",
  "extensions": [],
  "operations": [
    {
      "type": "transfer_operation",
      "value": {
        "amount": "300.000 HIVE",
        "from": "oneplus7",
        "memo": "Roll under 50 4d434bd943616",
        "to": "kryptogames"
      }
    }
  ],
  "ref_block_num": 1959,
  "ref_block_prefix": 3625727107
}