intrepidshape/elm-web3/1.2.2

NOTE: the latest version is 3.0.0

Web3.BigInt

Arbitrary-precision integers for EVM applications.

Supports the full uint256 range (0 to 2^256-1) and int256 range. Implemented as a base-10^7 digit list with no external dependencies.

All values crossing the port boundary are decimal strings — this module handles the conversion safely.

type BigInt

An arbitrary-precision integer.

fromInt : Int -> BigInt

Create a BigInt from an Elm Int.

fromString : String -> Maybe BigInt

Parse a decimal integer string. Returns Nothing for invalid input.

Accepts optional leading '-' or '+' sign. Rejects empty strings, non-digit characters, and floating-point notation.

fromString "0"                    == Just zero
fromString "-42"                  == Just (fromInt -42)
fromString "115792...9935"        -- uint256 max: Just <big>
fromString "abc"                  == Nothing
fromString ""                     == Nothing
fromIntString : String -> Maybe BigInt

Alias for fromString (decimal). Kept for API symmetry with fromHexString.

fromHexString : String -> Maybe BigInt

Parse a 0x-prefixed hexadecimal string as a non-negative BigInt. Returns Nothing for invalid input or empty hex body.

fromHexString "0x64"  == Just (fromInt 100)
fromHexString "0xFF"  == Just (fromInt 255)
fromHexString "0x"    == Nothing
fromHexString "0xgg"  == Nothing
toString : BigInt -> String

Convert to decimal string.

toHexString : BigInt -> String

Convert a non-negative BigInt to a lower-case hexadecimal string without a "0x" prefix or leading zeros.

Returns "0" for zero. For negative integers, the sign is dropped and the absolute value is rendered — callers needing two's-complement should do the twiddling themselves (or use Web3.Abi.Calldata.int256).

toHexString (fromInt 0)     == "0"
toHexString (fromInt 255)   == "ff"
toHexString (fromInt 65535) == "ffff"

Add two BigInts.

Subtract.

Multiply.

div : BigInt -> BigInt -> Maybe BigInt

Divide. Returns Nothing if divisor is zero.

mod : BigInt -> BigInt -> Maybe BigInt

Remainder: a mod b. Returns Nothing if b is zero.

mod (fromInt 10) (fromInt 3) == Just (fromInt 1)
mod (fromInt 10) zero        == Nothing
compare : BigInt -> BigInt -> Order

Compare two BigInts.

eq : BigInt -> BigInt -> Bool

Equality.

lt : BigInt -> BigInt -> Bool

Less than.

lte : BigInt -> BigInt -> Bool

Less than or equal.

gt : BigInt -> BigInt -> Bool

Greater than.

gte : BigInt -> BigInt -> Bool

Greater than or equal.

Zero.

isZero : BigInt -> Bool

Is this BigInt zero?

decoder : Decoder BigInt

Decode a BigInt from a JSON decimal string.

D.decodeString BigInt.decoder "\"12345\"" == Ok (BigInt.fromInt 12345)