5.7.1 Java  Script
5.7.1.1 Types
5.7.1.2 loader.mjs
5.7.1.3 Accounts
5.7.1.4 Contracts
5.7.1.5 Network Utilities
5.7.1.6 Utilities
5.7.1.7 ask.mjs
On this page:
protect
T_  Null
T_  Bool
T_  UInt
T_  Bytes
T_  Address
T_  Array
T_  Tuple
T_  Object
assert
Array_  set
big  Numberify
is  Big  Number
is  Hex
hex  To  Big  Number
string  To  Hex
big  Number  To  Hex
uint  To  Bytes
bytes  Eq
digest  Eq
address  Eq
digest
random  UInt
parse  Fixed  Point
parse  Int
add
sub
mod
mul
div
eq
ge
gt
le
lt
standard  Unit
atomic  Unit
minimum  Balance
parse  Currency
format  Currency
5.7.1.6 Utilities

These functions operate on JavaScript representations of Reach values.

protect(t, x) => x 

Asserts that value x has Reach type t. An exception is thrown if this is not the case.

T_Null => ReachType
T_Bool => ReachType
T_UInt => ReachType
T_Bytes(number) => ReachType
T_Digest => ReachType
T_Address => ReachType
T_Array(ReachType, number) => ReachType
T_Tuple([ReachType ...]) => ReachType
T_Object({Key: ReachType ...}) => ReachType
T_Data({Variant: ReachType ...}) => ReachType

Each of these represent the corresponding Reach type.

assert(p) 

Throws an exception if not given true.

Array_set(arr, idx, val) 

Returns a new array identical to arr, except that index idx is val.

bigNumberify(x) => UInt
isBigNumber(x) => bool
bigNumberToNumber(x) => number

bigNumberify converts a JavaScript number to a BigNumber, the JavaScript representation of Reach’s UInt.

isBigNumber checks if its input is a BigNumber.

bigNumberToNumber transparently applies bigNumberify to its argument and returns a JavaScript number.

isHex(x) => bool
hexToBigNumber(bytes) => UInt
stringToHex(string) => bytes
bigNumberToHex(UInt) => bytes
uintToBytes(UInt) => bytes
bytesEq(bytes, bytes) => bool
digestEq(Digest, Digest) => bool
addressEq(Address, Address) => bool

These are additional conversion and comparison utilities.

digest(x) => Digest

Hashes the value.

randomUInt() => UInt

Generates random bits as a UInt. The number of bits generated depends on the particular consensus network.

hasRandom

A value suitable for use as a participant interact interface requiring a random function.

parseFixedPoint(FixedPoint) => number

Parses a FixedPoint number into a Javascript number.

parseInt(Int) => number

Parses a signed Int into a Javascript number.

add(UInt, UInt) => UInt
sub(UInt, UInt) => UInt
mod(UInt, UInt) => UInt
mul(UInt, UInt) => UInt
div(UInt, UInt) => UInt 

Integer arithmetic on UInt.

eq(UInt, UInt) => bool
ge(UInt, UInt) => bool
gt(UInt, UInt) => bool
le(UInt, UInt) => bool
lt(UInt, UInt) => bool

Integer comparisons on UInt.

The following exports are for dealing with network tokens.

standardUnit // string
atomicUnit // string
minimumBalance // atomicUnitAmount
parseCurrency(standardUnitAmount) => atomicUnitAmount
formatCurrency(atomicUnitAmount, int) => string  // display amount in standard unit

These functions handle amounts in a network’s standard unit and its atomic unit. A standard unit is the network token unit most commonly associated with a network. For example, the standard unit of Ethereum is ETH. An atomic unit is the smallest unit of measure for the standard unit. For example, the atomic unit of Ethereum is WEI. An atomic unit is atomic, which means it cannot be divided into smaller units.

Some consensus networks, typically those with proof-of-stake, have minimum balances on their accounts, so this is exposed as minimumBalance.

Because there are 1,000,000,000,000,000,000 WEI in 1 ETH, BigNumber is used to represet values in WEI.

Quantities of a network token should always be passed into Reach in the token’s atomic unit.

bigNumberify is transparently applied to formatCurrency’s first argument.