intrepidshape/elm-web3/1.4.1

NOTE: the latest version is 3.0.0

Web3

Top-level module that composes wallet and transaction state.

This module bundles Web3.Wallet.State and a list of named Web3.Transaction.Status values into a single Model. Wire it into your app's model/update/subscriptions to get wallet connection, chain validation, and transaction tracking with a single port pair.

port web3Cmd : Json.Encode.Value -> Cmd msg
port web3Sub : (Json.Decode.Value -> msg) -> Sub msg


type alias Model =
    { web3 : Web3.Model
    , ...
    }


type Msg
    = Web3Msg Web3.Msg
    | ...


init : ( Model, Cmd Msg )
init =
    ( { web3 = Web3.init (Web3.Types.chainId 369), ... }, Cmd.none )


update msg model =
    case msg of
        Web3Msg subMsg ->
            ( { model | web3 = Web3.update subMsg model.web3 }, Cmd.none )


subscriptions model =
    web3Sub (Json.Decode.decodeValue Web3.Wallet.decoder
                >> Result.map Web3.WalletMsg
                >> Result.withDefault (Web3Msg (Web3.WalletMsg Web3.Wallet.WalletDisconnected))
            )

For individual modules without the combined wrapper, import Web3.Wallet, Web3.Transaction, Web3.Balance, Web3.Block, etc. directly.

type alias Model =
{ wallet : State
, expectedChain : ChainId
, transactions : List ( String, Status )
}

Combined wallet + transaction state.

type Msg
= WalletMsg Msg
| TxMsg String Msg
| TxCmdMsg String TxCmd

Messages for wallet and transaction updates.

init : ChainId -> Model

Initialize with the expected chain ID.

update : Msg -> Model -> Model

Update wallet and transaction state from a port message.

subscriptions : Model -> Sub Msg

Placeholder for port subscriptions. Wire your port decoder here.

encodeTxCmd : TxCmd -> Value

Encode a TxCmd (e.g. RequestReceipt) for sending via your port.

web3Cmd (Web3.encodeTxCmd (Tx.RequestReceipt hash "my-id"))