5.7.1.6 Utilities
These functions operate on JavaScript representations of Reach values.
—
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.
—
Throws an exception if not given true
.
—
Returns a new array identical to arr
, except that index idx
is val
.
—
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.
—
Hashes the value.
—
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.
—
Parses a FixedPoint
number into a Javascript 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
.
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.