Skip to content

cmditch/elm-ethereum

Repository files navigation

elm-ethereum elm-ethereum

Examples:
Simple starter example
Complex example SPA Dapp

Cool Feature: See here how you can easily track the block depth of transactions after they've been mined.


This library allows you to interact with the Ethereum blockchain much like purescript-web3, ethers.js, or web3.js. You can hook into web wallets like MetaMask and send transactions, as well as perform read-only operations on smart contracts.

See why elm?

Setup

  • Setup and define your node endpoint.
    import Eth
    import Eth.Types exposing (..)


    type alias Model =
        { ethNode : HttpProvider }

    init =
        { ethNode = "https://mainnet.infura.com/" }

It's good to keep the node url in your model. This way it can be kept in sync with MetaMask. Example code of this "sync" pattern to come.

Examples

  • Simple - Look at the blockchain

    Get an account balance at a specific block height.

    getMyBalanceInHistory : Int -> Task Http.Error BigInt
    getMyBalanceInHistory blockNum =
        Eth.getBalanceAtBlock model.ethNode myAddress (BlockNum blockNum)
  • Advanced - Chain tasks together

    Get all newly created contract addresses in the latest block. In a few lines of code.

    findNewestContracts : Task String (List Address)
    findNewestContracts =
        Eth.getBlockNumber model.ethNode
            |> Task.andThen (Eth.getBlock model.ethNode)
            |> Task.andThen
                (\block ->
                    block.transactions
                        |> List.map (Eth.getTxReceipt model.ethNode)
                        |> Task.sequence
                )
            |> Task.map (MaybeExtra.values << List.map .contractAddress)
            |> Task.mapError prettifyHttpError

This is an example of Railway Oriented Programming. A great video by Scott Wlaschin.

Why Elm

I'd sum up the experience of programming in Elm with two words: Fearless Refactoring

This is by no means the only pleasantry the fine tree has to offer.

Elm's claim to fame is zero runtime exceptions. It's compiler and static types are your best friends. Both from an error catching standpoint, but just as importantly, from a domain modeling standpoint.

Union Types allow you to fully leverage the compiler when modeling your business domain. See BlockId or NetworkId for instance.

Union types also allow you to hide implementation details by implementing "opaque types". An Address is just a string under the hood, but you can never directly touch that string.

Why else

  • Simplicity and cohesion
    Javascript                    Elm
    ---------------------------------
    npm/yarn                 built in
    Webpack                  built in
    React                    built in
    Redux                    built in
    Typescript/Flow          built in
    Immutable.JS             built in
  • Phenomenal tooling and resources

    Time traveling debugger - Import/Export history. QA like a champ.
    elm-format - Adds up to hours of tedius "work" saved.
    elm-reactor - Nice dev server.
    elm-test - Fuzz testing == legit.
    elm-benchmark - Clone this package and give it a whirl.
    Elm Package and Docs - Pleasant and consistent. Enforced semantic versioning.

  • Strong static types

    Find errors fast with readable compiler messages.
    Less millions of dollars lost from typos.

  • No null or undefined

    Never miss a potential problem.

  • Purely functional

    Leads to decoupled and easily refactorable code.

  • Great Community

    Thoughtful, responsive, intelligent, and kind.
    Great Slack and Discourse.

Contributing

Pull requests and issues are greatly appreciated!
If you think there's a better way to implement parts of this library, I'd love to hear your feedback.

Feed the tree some ether

🌳Ξ🌳Ξ🌳