cmditch/elm-ethereum/2.0.0

NOTE: the latest version is 5.0.0

Eth.Utils

String/Type Conversion and Application Helpers

Address

toAddress : String -> Result String 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 : Address -> String

Convert an Address to a String

addressToChecksumString : 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 -> Bool

Check if given string is a valid address.

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

isChecksumAddress : String -> Bool

Check if given string is a valid checksum address.

Hex

toHex : String -> Result String Hex

Safely convert a string into Hex.

hexToString : Hex -> String

Convert a Hex into a string.

isHex : String -> Bool

Check if given string is valid Hex

hexToAscii : Hex -> Result String String

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

hexToUtf8 : Hex -> Result String String

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

hexAppend : Hex -> Hex -> Hex

Append two Hex's together.

hexAppend (Hex 0x12) (Hex 0x34) == Hex 0x1234
hexConcat : List Hex -> Hex

Concatenate a list of Hex's

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

Transaction Hash

toTxHash : String -> Result String TxHash

Safely convert a string to a TxHash.

txHashToString : TxHash -> String

Convert a given TxHash to a string.

isTxHash : String -> Bool

Check if given string is a valid TxHash.

i.e. Hex and 64 characters long.

Block Hash

toBlockHash : String -> Result String BlockHash

Safely convert a given string to a BlockHash.

blockHashToString : BlockHash -> String

Convert a given BlockHash to a string.

isBlockHash : String -> Bool

Check if given string is a valid BlockHash.

i.e. Hex and 64 characters long.

SHA3

functionSig : String -> Hex

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

Utils.functionSig "transfer(address,uint256)" == Hex "a9059cbb"
keccak256 : String -> Hex

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

isSha256 : String -> Bool

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

lowLevelKeccak256 : List Int -> List Int

Same as ethereum_keccak_256 from this library.

IPFS

ipfsHashToString : IPFSHash -> String

Convert an IPFSHash to a string.

ipfsToBytes32 : IPFSHash -> Hex

Prepares IPFS Hash to store as soldity bytes32

Note: This assumes a SHA256 IPFS hash is used and chops off the "Qm". An "opinionated" feature of this library that helps save gas costs by avoiding the more expensive dynamic types like string or bytes. The "Qm" is there to help future proof IPFS, and allow for other hashing algorithms to be used.

U.toIPFSHash "QmNXnCWPS2szLaQGVA6TFtiUAJB2YnFTJJFTXPGuc4wocQ"
    |> Result.map U.ipfsToBytes32
    > Ok (Hex "02d9db84e21354dd4cc160eca9d13fa6f1b1bb44324013204098ae24090e717d")

Can be decoded safely decoded back to it's "Qm"-edness with Evm.Decode.ipfsHash.

toIPFSHash : String -> Result String IPFSHash

Safely convert a stringy IPFS Hash to it's properly typed form.

isIPFSHash : String -> Bool

Check if given string is a valid IPFS Hash.

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 -> Hex
unsafeToAddress : String -> Address
unsafeToTxHash : String -> TxHash
unsafeToBlockHash : String -> BlockHash
unsafeToIPFSHash : String -> IPFSHash

Application Helpers

type alias Retry =
{ attempts : Int
, sleep : 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) -> (Error -> msg) -> Decoder a -> 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