burnable-tech / elm-ethereum / Eth.Utils

String/Type Conversion and Application Helpers

Address

toAddress : String -> Result String Eth.Types.Address

Safely convert a string into an address.

All lowercase or uppercase strings, shaped like addresses, will result in Ok. Mixed case strings will return Err if the EIP-55 checksum is invalid.

addressToString : Eth.Types.Address -> String

Convert an Address to a String

addressToChecksumString : Eth.Types.Address -> String

Convert an Address to a string conforming to the EIP-55 checksum.

Note: This lowercases all the characters inside the Address and runs it through the checksum algorithm.

isAddress : String -> Basics.Bool

Check if given string is a valid address.

Note: Works on mixed case strings, with or without the 0x.

isChecksumAddress : String -> Basics.Bool

Check if given string is a valid checksum address.

Hex

toHex : String -> Result String Eth.Types.Hex

Safely convert a string into Hex.

hexToString : Eth.Types.Hex -> String

Convert a Hex into a string.

isHex : String -> Basics.Bool

Check if given string is valid Hex

hexToAscii : Eth.Types.Hex -> Result String String

Convert Given Hex into ASCII. Will fail if Hex is an uneven length.

hexToUtf8 : Eth.Types.Hex -> Result String String

Convert Given Hex into UTF8. Will fail if Hex is an uneven length.

hexAppend : Eth.Types.Hex -> Eth.Types.Hex -> Eth.Types.Hex

Append two Hex's together.

hexAppend (Hex 0x12) (Hex 0x34) == Hex 0x1234

hexConcat : List Eth.Types.Hex -> Eth.Types.Hex

Concatenate a list of Hex's

hexConcat [ Hex 0x12, Hex 0x34, Hex 0x56 ] == Hex 0x 00123456

Transaction Hash

toTxHash : String -> Result String Eth.Types.TxHash

Safely convert a string to a TxHash.

txHashToString : Eth.Types.TxHash -> String

Convert a given TxHash to a string.

isTxHash : String -> Basics.Bool

Check if given string is a valid TxHash.

i.e. Hex and 64 characters long.

Block Hash

toBlockHash : String -> Result String Eth.Types.BlockHash

Safely convert a given string to a BlockHash.

blockHashToString : Eth.Types.BlockHash -> String

Convert a given BlockHash to a string.

isBlockHash : String -> Basics.Bool

Check if given string is a valid BlockHash.

i.e. Hex and 64 characters long.

SHA3

functionSig : String -> Eth.Types.Hex

Convert a contract function name to it's 4-byte function signature.

Utils.functionSig "transfer(address,uint256)" == Hex "a9059cbb"

keccak256 : String -> Eth.Types.Hex

Hash a given string into it's SHA3/Keccak256 form.

isSha256 : String -> Basics.Bool

Checks if a given string is valid hex and 64 chars long.

lowLevelKeccak256 : List Basics.Int -> List Basics.Int

Same as ethereum_keccak_256 from this library.

Unsafe

User beware!! These are sidestepping the power of Elm, and it's static types.

Undoubtedly convenient for baking values, like contract addresses, into your source code.

All values coming from the outside world, like user input or server responses, should use the safe functions.

unsafeToHex : String -> Eth.Types.Hex

unsafeToAddress : String -> Eth.Types.Address

unsafeToTxHash : String -> Eth.Types.TxHash

unsafeToBlockHash : String -> Eth.Types.BlockHash

Application Helpers


type alias Retry =
{ attempts : Basics.Int
, sleep : Basics.Float 
}

Config for a retry task

retry : Retry -> Task x a -> Task x a

Retry a given Task till it succeeds, or runs out of time.

The below will wait for 5 minutes until giving up, and polls every 5 seconds.

pollForMinedTx : HttpProvider -> TxHash -> Task Http.Error TxReceipt
pollForMinedTx ethNode txHash =
    Eth.getTxReceipt ethNode txHash
        |> retry { attempts = 60, sleep = 5 }

valueToMsg : (a -> msg) -> (Json.Decode.Error -> msg) -> Json.Decode.Decoder a -> Json.Encode.Value -> msg

Useful for decoding past a result straight into a Msg. Comes in handy with Eth.Sentry.Event values.

transferDecoder : Value -> Msg
transferDecoder =
    valueToMsg Transfer Error transferEventDecoder

type Msg
    = Transfer (Event Transfer)
    | Error String