Options
All
  • Public
  • Public/Protected
  • All
Menu

External module harmony-utils

@harmony-js/util provides utility functions for Harmony dapps and other harmony-js packages

Develop can use this package to:

  • Transform the unit of token (fromWei, toWei...)
  • Convert variable to different type (hexToBN, numberToHex...)
  • Check validators information (isAddress, isPublicKey, isBlockNumber...)

How to use this package

Step 1: create a Harmony Instance

const { Harmony } = require('@harmony-js/core');
const { ChainID, ChainType } = require('@harmony-js/utils');
const { BN } = require('@harmony-js/crypto');

const hmy = new Harmony(
  'http://localhost:9500',
  {
    chainType: ChainType.Harmony,
    chainId: ChainID.HmyLocal,
  },
);

Step 2: Select and call functions

Here are some examples:

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

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

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

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

Step 3: Using unit class to convet the token unit

// convert one to Gwei
const one = new hmy.utils.Unit('1').asOne();
const oneToGwei = one.toGwei();
console.log(oneToGwei);

Some Important consts and Enums

Chain Type

Harmony = 'hmy',
Ethereum = 'eth',

Chain ID

Default = 0,
EthMainnet = 1,
Morden = 2,
Ropsten = 3,
Rinkeby = 4,
RootstockMainnet = 30,
RootstockTestnet = 31,
Kovan = 42,
EtcMainnet = 61,
EtcTestnet = 62,
Geth = 1337,
Ganache = 0,
HmyMainnet = 1,
HmyTestnet = 2,
HmyLocal = 2,
HmyPangaea = 3,

Default Config

export const defaultConfig = {
  Default: {
    Chain_ID: ChainID.HmyLocal,
    Chain_Type: ChainType.Harmony,
    Chain_URL: 'http://localhost:9500',
    Network_ID: 'Local',
},
  DefaultWS: {
    Chain_ID: ChainID.HmyLocal,
    Chain_Type: ChainType.Harmony,
    Chain_URL: 'ws://localhost:9800',
    Network_ID: 'LocalWS',
  },
};

Unit Map

[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

Index

Functions

Const add0xToString

  • add0xToString(obj: string): string

Const assertObject

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

Const fromWei

  • fromWei(wei: BN | string, unit: Units, options?: any): string

generateValidateObjects

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

Const hexToBN

  • hexToBN(hex: string): BN

Const hexToNumber

  • hexToNumber(hex: string): string

Const isAddress

  • isAddress(address: string): boolean

Const isArray

  • isArray(obj: any): boolean

Const isBech32Address

  • isBech32Address(raw: string): boolean

Const isBech32TestNetAddress

  • isBech32TestNetAddress(raw: string): boolean

Const isBlockNumber

  • isBlockNumber(obj: any): boolean

Const isBoolean

  • isBoolean(obj: any): boolean

Const isFunction

  • isFunction(obj: any): boolean

Const isHash

  • isHash(hash: string): boolean

Const isHex

  • isHex(obj: any): boolean

Const isHttp

  • isHttp(obj: any): boolean

Const isInt

  • isInt(obj: any): boolean

Const isJsonString

  • isJsonString(obj: any): boolean

Const isKeyString

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

Const isNumber

  • isNumber(obj: any): boolean

Const isObject

  • isObject(obj: any): boolean

Const isPrivateKey

  • isPrivateKey(privateKey: string): boolean

Const isPublicKey

  • isPublicKey(publicKey: string): boolean

Const isString

  • isString(obj: any): boolean

Const isValidAddress

  • isValidAddress(address: string): boolean

Const isWs

  • isWs(obj: any): boolean

Const numToStr

  • numToStr(input: any): any

Const numberToHex

  • numberToHex(obj: any): string

Const numberToString

  • numberToString(obj: BN | number | string, radix?: number): string

Const strip0x

  • strip0x(obj: string): string

Const toWei

  • toWei(input: BN | string, unit: Units): BN

validateArgs

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

Generated using TypeDoc