Ethereum node API

Any Ethereum node can export their Generic JSON-RPC. For connection with node hs-web3 use internal tiny JSON-RPC client.

Note

Tiny client library placed at Network.JsonRpc.TinyClient. It exports special monad JsonRpc and function remote to define JSON-RPC methods. When developing tiny client I was inspired HaXR library.

Providers

To handle connection with Ethereum node some thing named provider is required. Provider data type define the endpoint of Ethereum node API. Module that export this type placed at Network.Ethereum.Api.Provider.

data Provider = HttpProvider String

Note

Currently hs-web3 support HTTP(S) providers only.

Another interesting thing in this module is Web3 type.

newtype Web3 a = ...
instance Monad Web3
instance JsonRpc Web3

As you can see Web3 is monad that can handle JSON-RPC. It’s very important because it can be used for any Ethereum node communication.

Note

Web3 is a state monad with JsonRpcClient type as a state. This is mean that you can modify JSON-RPC server URI in runtime using MTL lenses for example.

Finally provider module exports runWeb3 and party functions.

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

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

Note

Function runWeb3 run default provider at http://localhost:8545.

Lets try to call custom Ethereum node URI with runWeb3' function using ghci.

> import Network.Ethereum.Api.Provider
> import qualified Network.Ethereum.Api.Eth as Eth
> runWeb3' (HttpProvider "http://localhost:9545") Eth.blockNumber

It can be useful to define function with custom Ethereum node endpoint location.

myNode :: Web3 a -> Either Web3Error a
myNode = runWeb3' (HttpProvider "http://my-host-name:8545")

API Reference

Currently implemented the following Ethereum APIs in modules:

All modules use descriptive types according to official Ethereum specification. It placed at Network.Ethereum.Api.Types.

Note

See classic API reference at Hackage web3 page.