Options
All
  • Public
  • Public/Protected
  • All
Menu

Module harmony-utils

This package provides a collection of utility apis for unit conversions like fromWei, toWei, hexToNumber, numberToHex, isAddress, etc.

Installation

npm install @harmony-js/utils

Usage

Available units

const { Units } = require('@harmony-js/utils');

[Units.wei, '1'], // 1 wei
[Units.Kwei, '1000'], // 1e3 wei
[Units.Mwei, '1000000'], // 1e6 wei
[Units.Gwei, '1000000000'], // 1e9 wei
[Units.szabo, '1000000000000'], // 1e12 wei
[Units.finney, '1000000000000000'], // 1e15 wei
[Units.ether, '1000000000000000000'], // 1e18 wei
[Units.one, '1000000000000000000'], // 1e18 wei
[Units.Kether, '1000000000000000000000'], // 1e21 wei
[Units.Mether, '1000000000000000000000000'], // 1e24 wei
[Units.Gether, '1000000000000000000000000000'], // 1e27 wei
[Units.Tether, '1000000000000000000000000000000'], // 1e30 wei

Converting between different units

const { Units, Unit, numberToString, add0xToString, fromWei, toWei, numToStr} = require('@harmony-js/utils');
const { BN } = require('@harmony-js/crypto');

const one = new Unit('1').asOne();
const oneToGwei = one.toGwei();
console.log(oneToGwei);

// numberToString
const num = 123;
const str = numberToString(num)
console.log(str);

// add0xToString
const str = '12345';
const expected = add0xToString(str)
console.log(expected);

// fromWei
const Wei = new BN('1000000000000000000');
const expected = fromWei(Wei, Units.one);
console.log(expected);

// toWei
const one = new BN('1');
const expected = toWei(one, hmy.utils.Units.one);
const num = numToStr(expected);
console.log(num);

Index

Functions

Const add0xToString

  • add0xToString(obj: string): string
  • Parameters

    • obj: string

    Returns string

Const assertObject

  • assertObject(input: any): (Anonymous function)
  • Parameters

    • input: any

    Returns (Anonymous function)

Const fromWei

  • fromWei(wei: BN | string, unit: Units, options?: any): string
  • Converts any wei value into a ONE value.

    Parameters

    • wei: BN | string
    • unit: Units
    • Default value options: any = DEFAULT_OPTIONS

    Returns string

generateValidateObjects

  • generateValidateObjects(validatorObject: {}): { optionalArgs: any; requiredArgs: any }
  • Parameters

    • validatorObject: {}
      • [x: string]: any[]

    Returns { optionalArgs: any; requiredArgs: any }

    • optionalArgs: any
    • requiredArgs: any

Const hexToBN

  • hexToBN(hex: string): BN
  • Convert hex to Big Number

    Parameters

    • hex: string

    Returns BN

Const hexToNumber

  • hexToNumber(hex: string): string
  • Convert hex to Decimal number

    Parameters

    • hex: string

    Returns string

Const isAddress

  • isAddress(address: string): boolean
  • Parameters

    • address: string

    Returns boolean

Const isArray

  • isArray(obj: any): boolean
  • [isArray verify param input is an Array]

    Parameters

    • obj: any

    Returns boolean

    [boolean]

Const isBech32Address

  • isBech32Address(raw: string): boolean
  • Parameters

    • raw: string

    Returns boolean

Const isBech32TestNetAddress

  • isBech32TestNetAddress(raw: string): boolean
  • Parameters

    • raw: string

    Returns boolean

Const isBlockNumber

  • isBlockNumber(obj: any): boolean
  • Parameters

    • obj: any

    Returns boolean

Const isBoolean

  • isBoolean(obj: any): boolean
  • [isBoolean verify param is a Boolean]

    Parameters

    • obj: any

    Returns boolean

    [boolean]

Const isFunction

  • isFunction(obj: any): boolean
  • [isFunction verify param is a Function]

    Parameters

    • obj: any

    Returns boolean

    [description]

Const isHash

  • isHash(hash: string): boolean
  • Parameters

    • hash: string

    Returns boolean

Const isHex

  • isHex(obj: any): boolean
  • Parameters

    • obj: any

    Returns boolean

Const isHttp

  • isHttp(obj: any): boolean
  • Parameters

    • obj: any

    Returns boolean

Const isInt

  • isInt(obj: any): boolean
  • [isNumber verify param is a Number]

    Parameters

    • obj: any

    Returns boolean

    [boolean]

Const isJsonString

  • isJsonString(obj: any): boolean
  • [isJson verify param input is a Json]

    Parameters

    • obj: any

    Returns boolean

    [boolean]

Const isKeyString

  • isKeyString(keyString: string, lengh: number): boolean
  • Parameters

    • keyString: string
    • lengh: number

    Returns boolean

Const isNumber

  • isNumber(obj: any): boolean
  • [isNumber verify param is a Number]

    Parameters

    • obj: any

    Returns boolean

    [boolean]

Const isObject

  • isObject(obj: any): boolean
  • [isObject verify param is an Object]

    Parameters

    • obj: any

    Returns boolean

    [boolean]

Const isPrivateKey

  • isPrivateKey(privateKey: string): boolean
  • Parameters

    • privateKey: string

    Returns boolean

Const isPublicKey

  • isPublicKey(publicKey: string): boolean
  • Parameters

    • publicKey: string

    Returns boolean

Const isString

  • isString(obj: any): boolean
  • [isString verify param is a String]

    Parameters

    • obj: any

    Returns boolean

    [boolean]

Const isValidAddress

  • isValidAddress(address: string): boolean
  • Parameters

    • address: string

    Returns boolean

Const isWs

  • isWs(obj: any): boolean
  • Parameters

    • obj: any

    Returns boolean

Const numToStr

  • numToStr(input: any): any
  • Convert Number to String

    Parameters

    • input: any

    Returns any

Const numberToHex

  • numberToHex(obj: any): string
  • Convert number to hex

    Parameters

    • obj: any

    Returns string

Const numberToString

  • numberToString(obj: BN | number | string, radix?: number): string
  • Convert Number to String

    Parameters

    • obj: BN | number | string
    • Default value radix: number = 10

    Returns string

Const strip0x

  • strip0x(obj: string): string
  • Parameters

    • obj: string

    Returns string

Const toWei

  • toWei(input: BN | string, unit: Units): BN
  • Converts any ONE value into wei

    Parameters

    • input: BN | string
    • unit: Units

    Returns BN

validateArgs

  • validateArgs(args: any, requiredArgs: any, optionalArgs: any): boolean
  • Parameters

    • args: any
    • requiredArgs: any
    • optionalArgs: any

    Returns boolean

Generated using TypeDoc