cmditch/elm-ethereum/2.0.1

NOTE: the latest version is 5.0.0

Eth

Ethereum RPC Methods

See the official docs for reference.

Contracts

Make sure to use the elm-ethereum-generator to auto-generate the necessary Elm <-> Contract interface from a contract's ABI.

If you're making Eth transactions, you'll need to build a Call, convert it to a Send, and use Eth.Sentry.Tx to hand it off to your browser's wallet (e.g., MetaMask, Trust).

( newSentry, sentryCmd ) =
    myCallParams
        |> Eth.toSend
        |> TxSentry.send TxSendResponse model.txSentry
call : HttpProvider -> Call a -> Task Error a

Call a function on an Ethereum contract. Useful for reading data from contracts, or simulating a transaction before doing a real Send.

Use the elm-ethereum-generator code generator to produce an interface for a smart contract from it's ABI.

Note The decoder for a call is baked into the Call record to allow for a smoother developer experience.

getStorageAt : HttpProvider -> Address -> Int -> Task Error String

Returns the value from a storage position at a given address. See Ethereum JSON-RPC methods for specification on retrieving data from complex data structures like maps.

getCode : HttpProvider -> Address -> Task Error String

Returns the bytecode from a contract at a given contract's address.

callAtBlock : HttpProvider -> Call a -> BlockId -> Task Error a

Call a function on an Ethereum contract from a particular point in history.

getStorageAtBlock : HttpProvider -> Address -> Int -> BlockId -> Task Error String

Returns the value from a storage position at a given address, at a certain block height.

getCodeAtBlock : HttpProvider -> Address -> BlockId -> Task Error String

Returns the bytecode from a contract at a given address, at a certain block height.

Transactions

getTx : HttpProvider -> TxHash -> Task Error Tx

Get transaction information from it's hash. Includes pre-execution info: value, nonce, data/input, gas, gasPrice, to, and from.

getTxReceipt : HttpProvider -> TxHash -> Task Error TxReceipt

Get the receipt of a transaction from it's hash. Only exists after the transaction has been mined!

Includes post-execution info: gasUsed, cumulativeGasUsed, contractAddress, logs, logsBloom. Also includes the tx execution status (only if block is post-byzantium).

toSend : Call a -> Send

Prepare a Call to be executed on chain. Used in Eth.Sentry.Tx, a means to interact with MetaMask.

encodeSend : Send -> Value

Useful if your handling txParams in javascript land yourself. Will return Error if params are invalid. e.g., buffer overflow

sendTx : HttpProvider -> Send -> Task Error TxHash

Execute a transaction on chain. Only useful if your keys live on the node your talking too, which is generally considered very poor practice.

NOTE: You probably don't need this. If you're writing a proper dApp, look at using the Eth.Sentry.Tx to interface with wallets like MetaMask.

sendRawTx : HttpProvider -> String -> Task Error TxHash

Broadcast a signed and RLP encoded transaction.

getTxByBlockHashAndIndex : HttpProvider -> BlockHash -> Int -> Task Error Tx

Get a transaction by it's index in a certain block given the block hash.

getTxByBlockNumberAndIndex : HttpProvider -> Int -> Int -> Task Error Tx

Get a transaction by it's index in a certain block given the block number.

Address/Accounts

getBalance : HttpProvider -> Address -> Task Error BigInt

Get the balance of a given address/account. Returns Wei amount as BigInt

getTxCount : HttpProvider -> Address -> Task Error Int

Get the number of transactions sent from a given address/account.

getBalanceAtBlock : HttpProvider -> Address -> BlockId -> Task Error BigInt

Get the balance of a given address/account, at a certain block height

getTxCountAtBlock : HttpProvider -> Address -> BlockId -> Task Error Int

Get the number of transactions sent from a given address/account at a given block height.

Blocks

getBlockNumber : HttpProvider -> Task Error Int

Get the block number of the most recently mined block.

getBlock : HttpProvider -> Int -> Task Error (Block TxHash)

Get information about a block given a valid block number.

The transactions field will be an array of TxHash's mined during this block.

getBlockByHash : HttpProvider -> BlockHash -> Task Error (Block TxHash)

Get information about a block given a valid block hash.

getBlockWithTxObjs : HttpProvider -> Int -> Task Error (Block Tx)

Get information about a block given a valid block number.

The transactions field will be an array of Tx objects instead of TxHash's.

getBlockByHashWithTxObjs : HttpProvider -> BlockHash -> Task Error (Block Tx)

See getBlockWithTxObjs above.

Uses block hash instead of number for the identifier.

getBlockTxCount : HttpProvider -> Int -> Task Error Int

Get the number of transactions in a block from a given block number.

getBlockTxCountByHash : HttpProvider -> BlockHash -> Task Error Int

Get the number of transactions in a block from a given block hash.

getUncleCount : HttpProvider -> Int -> Task Error Int

Get the number of uncles in a given block given a block number.

getUncleCountByHash : HttpProvider -> BlockHash -> Task Error Int

Get the number of uncles in a given block given a block hash.

getUncleAtIndex : HttpProvider -> Int -> Int -> Task Error Uncle

Get information about an uncle given it's index in a block by block number

getUncleByBlockHashAtIndex : HttpProvider -> BlockHash -> Int -> Task Error Uncle

Get information about an uncle given it's index in a block by block hash

Filter/Logs/Events

If you have access to a websocket RPC endpoint, it's much easier to just use Eth.Sentry.Event. Geth, Parity, and Infura support websockets.

getLogs : HttpProvider -> LogFilter -> Task Error (List Log)

Get an array of all logs matching a given filter object. Most likely you won't need this, as they are generated for you in elm-ethereum-generator

newFilter : HttpProvider -> LogFilter -> Task Error FilterId

Establishes a filter object on a given node. Useful for contract events.

To check if the state has changed, call getFilterChanges.

newBlockFilter : HttpProvider -> Task Error FilterId

Creates a filter in the node to notify when a new block arrives. To check if the state has changed, call getFilterChanges.

newPendingTxFilter : HttpProvider -> Task Error FilterId

Creates a filter in the node to notify when new pending transactions arrive. To check if the state has changed, call getFilterChanges.

getFilterChanges : HttpProvider -> Decoder a -> FilterId -> Task Error (List a)

Polling method for a filter, which returns an array of logs which occurred since last poll.

Use the correct decoder for the given filter type:

For a newFilter:

import Eth.Decode as Decode
import Eth.Evm.Decode as Evm
import Json.Decode exposing (Decoder)
import Json.Decode.Pipeline exposing (custom)


transferEventDecoder : Decoder (Event Erc20Transfer)
transferEventDecoder =
    Decode.event erc20TransferDecoder


type alias Erc20Transfer =
    { from : Address
    , to : Address
    , value : BigInt
    }


erc20TransferDecoder : Decoder Erc20Transfer
erc20TransferDecoder =
    decode Transfer
        |> custom (Evm.topic 1 Evm.address)
        |> custom (Evm.topic 2 Evm.address)
        |> custom (Evm.data 0 Evm.uint)

For a newBlockFilter:

newBlockDecoder : Decoder BlockHead
newBlockDecoder =
    Eth.Decode.blockHead

For a newPendingTxFilter:

newPendingTxDecoder : Decoder TxHash
newPendingTxDecoder =
    Eth.Decode.tx
getFilterLogs : HttpProvider -> Decoder a -> FilterId -> Task Error (List a)

Returns an array of all logs matching filter with given id. See above note on decoders.

uninstallFilter : HttpProvider -> FilterId -> Task Error FilterId

Uninstalls a filter with given id. Should always be called when watch is no longer needed. Additonally Filters timeout when they aren't requested with eth_getFilterChanges for a period of time.

Misc

sign : HttpProvider -> Address -> String -> Task Error String

Sign an arbitrary chunk of N bytes.

The sign method calculates an Ethereum specific signature with: sign(keccak256("\x19Ethereum Signed Message:\n" + len(message) + message))).

By adding a prefix to the message makes the calculated signature recognisable as an Ethereum specific signature. This prevents misuse where a malicious DApp can sign arbitrary data (e.g. transaction) and use the signature to impersonate the victim.

Note the address to sign with must be unlocked.

protocolVersion : HttpProvider -> Task Error Int

Get the current ethereum protocol version.

syncing : HttpProvider -> Task Error (Maybe SyncStatus)

Get the sync status of a particular node.

Nothing == Not Syncing
Just SyncStatus == starting, current, and highestBlock
coinbase : HttpProvider -> Task Error Address

Get the client's coinbase address.

mining : HttpProvider -> Task Error Bool

See whether or not a given node is mining.

hashrate : HttpProvider -> Task Error Int

Returns the number of hashes per second that the node is mining with.

gasPrice : HttpProvider -> Task Error BigInt

Get the current price per gas in wei

Note: not always accurate. See EthGasStation website

accounts : HttpProvider -> Task Error (List Address)

Returns a list of addresses owned by client.