intrepidshape/elm-web3/2.1.0

NOTE: the latest version is 3.0.0

Web3.Block

Block number and block data queries.

-- One-shot block number query:
port web3Cmd : Json.Encode.Value -> Cmd msg
port web3Sub : (Json.Decode.Value -> msg) -> Sub msg


type Msg = GotBlock Block.Msg | ...


subscriptions model =
    web3Sub (Json.Decode.decodeValue Block.decoder >> Result.toMaybe >> Maybe.map GotBlock)


fetchBlock : Platform.Cmd msg
fetchBlock =
    web3Cmd (Block.encode (Block.getBlockNumber "my-id"))
type Cmd
= RequestBlockNumber String
| RequestBlock BlockNumber String
| WatchBlockNumber String
| RequestBlockTxCount BlockNumber String

Commands to query block data via port.

type Msg
= GotBlockNumber String Int
| GotBlock String Block
| GotBlockTxCount String Int

Messages from the JS block port.

type alias Block =
{ number : Int
, hash : String
, timestamp : Int
, gasLimit : BigInt
, gasUsed : BigInt
, baseFeePerGas : Maybe BigInt
, parentHash : String
}

A block header with key fields.

getBlockNumber : String -> Cmd

Request the current block number. The id is echoed back in the response.

getBlock : BlockNumber -> String -> Cmd

Request a specific block by block number. The id is echoed back.

watchBlockNumber : String -> Cmd

Start polling the block number every 4 seconds. The id tags each GotBlockNumber response so you can distinguish multiple watchers.

Request the number of transactions in a block. The second argument is a request ID.

encode : Cmd -> Value

Encode a Cmd for the JS port.

decoder : Decoder Msg

Decode Msg responses from the JS port.

unwatchBlockNumber : String -> Value

Stop a block-number watcher started with watchBlockNumber. The id must match the one passed to watchBlockNumber. Clears the JS-side polling interval and/or the WebSocket newHeads subscription.

web3Cmd (Block.unwatchBlockNumber "block-watch")

This is a standalone encoder producing the port value directly -- it is deliberately NOT a new variant of Cmd, because adding a variant to an exposed custom type is a MAJOR change under Elm's enforced semver (consumer case expressions over Cmd would stop compiling). A standalone function keeps this addition MINOR-safe.