Ethereum Haskell API

This is the Ethereum compatible Haskell API which implements the Generic JSON RPC spec.

Build Status Build status Hackage Haskell Programming Language BSD3 License Code Triagers Badge

Installation

$ git clone https://github.com/airalab/hs-web3 && cd hs-web3
$ stack setup
$ stack ghci

This library runs only paired with geth or parity Ethereum node, please start node first before using the library.

Web3 monad

Any Ethereum node communication wrapped with Web3 monadic type.

> import Network.Ethereum.Web3.Web3
> :t clientVersion
clientVersion :: Web3 Text

To run this computation used runWeb3' or runWeb3 functions.

> import Network.Ethereum.Web3
> runWeb3 clientVersion
Right "Parity//v1.4.5-beta-a028d04-20161126/x86_64-linux-gnu/rustc1.13.0"

Function runWeb3 use default Web3 provider at localhost:8545.

> :t runWeb3
runWeb3
  :: MonadIO m => Web3 a -> m (Either Web3Error a)

TemplateHaskell generator

Quasiquotation is used to parse contract ABI or load from JSON file. TemplateHaskell driven Haskell contract API generator can automatical create ABI encoding instances and contract method helpers.

> :set -XQuasiQuotes
> import Network.Ethereum.Contract.TH
> putStr [abiFrom|data/sample.json|]
Contract:
        Events:
                Action1(address,uint256)
                Action2(string,uint256)
        Methods:
                0x03de48b3 runA1()
                0x90126c7a runA2(string,uint256)

Use -ddump-splices to see generated code during compilation or in GHCi. See examples folder for more use cases.

Testing

Testing the web3 is split up into two suites: unit and live. The unit suite tests internal library facilities, while the live tests that the library adequately interacts with a Web3 provider.

One may simply run stack test to run both suites, or stack test web3:unit or stack test web3:live to run the test suites individually.

The unit suite has no external dependencies, while the live suite requires Truffle and jq to be available on your machine.

The live suite also requires a Web3 provider with Ethereum capabilities, as well as an unlocked account with ether to send transactions from. It uses Truffle to deploy testing contracts, generating ABIs for them in the process, then using said ABIs as part of a TemplateHaskell step in the suite. It is assumed that the provider is available at http://localhost:8545. If that’s not the case, you must update truffle.js so that Truffle can deploy the contracts correctly, and pass the WEB3_PROVIDER=http://host:port environment variable when running the tests so that the web3 library can interact with the chain that’s being tested against.

Changes

Changelog

All notable changes to this project will be documented in this file.

[0.7.3.0] 2018-05-22

Added

  • ‘Network.Ethereum.ABI.Prim’ meta-module as primitive types and instances aggregator.
  • Stackage nightly build compatibility.

Changed

  • Potential nullable web3 type (‘Change’, ‘Block’, ‘Transaction’, etc.) fields are encoded as ‘Maybe’.

[0.7.2.0] 2018-05-13

Added

  • Generic JSON-RPC API documentation improvements.
  • TH generator bug fixes.

Changed

  • Generic JSON-RPC API use fixed size byte arrays.

Removed

  • ‘BlockNumber’, ‘FilterId’ types replaced by ‘Quantity’ type

[0.7.1.0] 2018-05-02

Added

  • TxReceipt and Eth.getTransactionReceipt JSON-RPC method.
  • Keyword escaper for TH generated names.
  • Build flag to enable TLS support.

Changed

  • Library dependencies bounds fixed for GHC 8.2.2-8.4.2 (stackage LTS + Nightly).

[0.7.0.0] 2018-04-22

Added

  • This CHANGELOG.md file for significant changes tracking.
  • Descriptive types for all JSON-RPC method parameters and returned values (#15).
  • Widely use of basement:Word256 type for encoding.
  • Full list of ethereum abi encoding types:
    • bool: Bool
    • int256: IntN
    • uint256: UIntN
    • string: Text
    • bytes: Bytes
    • bytes32: BytesN
    • dynamic array: []
    • static array: ListN

Changed

  • Rewriten encoding engine for best performance, it now based on cereal:Serialize instead of parsec:Parser.
  • Renamed encoding type classes and methods: ABIEncode -> ABIPut, ABIDecode -> ABIGet.
  • Encoding related modules moved to Network.Ethereum.ABI.
  • Primitive abi encoding types are moved to separated modules in Network.Ethereum.ABI.Prim.
  • Contract interation related modules moved to Network.Ethereum.Contract.
  • Ethereum node communication modules stay in Network.Ethereum.Web3.
  • JSON-RPC tiny client is independent now and can be used separately.

Removed

  • Event type class, currently TH create Data.Default instance for Filter e.
  • Custom setup for live testing (it replaced by travis script).