Versions |
0.3.2.0, 0.3.2.1, 0.3.3.0, 0.3.4.0, 0.4.0.0, 0.4.1.0, 0.5.0.0, 0.5.1.0, 0.5.2.0, 0.5.2.1, 0.5.3.0, 0.5.4.0, 0.5.5.0, 0.6.0.0, 0.7.0.0, 0.7.0.0, 0.7.1.0, 0.7.2.0, 0.7.3.0, 0.8.0.0, 0.8.1.0, 0.8.2.0, 0.8.2.1, 0.8.3.0, 0.8.3.1, 0.8.3.2, 0.8.4.0, 0.9.0.0, 0.9.1.0, 1.0.0.0, 1.0.1.0 |
Change log |
None available |
Dependencies |
aeson (>=1.2.4.0 && <1.3), async (>=2.1.1.1 && <2.2), base (>4.8 && <4.11), basement (>=0.0.7 && <0.1), bytestring (>=0.10.8.2 && <0.11), cereal (>=0.5.5.0 && <0.6), cryptonite (>=0.25 && <0.26), data-default (>=0.7.1.1 && <0.8), exceptions (>=0.8.3 && <0.9), generics-sop (>=0.3.2.0 && <0.4), http-client (>=0.5.12 && <0.6), http-client-tls (>=0.3.5.3 && <0.4), machines (>=0.6.3 && <0.7), memory (>=0.14.16 && <0.15), mtl (>=2.2.2 && <2.3), parsec (>=3.1.13.0 && <3.2), tagged (>=0.8.5 && <0.9), template-haskell (>=2.12.0.0 && <2.13), text (>=1.2.3.0 && <1.3), transformers (>=0.5.2.0 && <0.6) [details] |
License |
BSD-3-Clause |
Copyright |
Alexander Krupenkin |
Author |
Alexander Krupenkin |
Maintainer |
mail@akru.me |
Category |
Network |
Home page |
https://github.com/airalab/hs-web3#readme
|
Source repo |
head: git clone https://github.com/airalab/hs-web3 |
Uploaded |
by akru at 2018-04-22T18:11:52Z |
Ethereum Haskell API
This is the Ethereum compatible Haskell API which implements the Generic JSON RPC spec.
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)
See example of usage below. Use -ddump-splices
to see generated code during compilation or in GHCi.
{-# LANGUAGE DataKinds #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE QuasiQuotes #-}
module Main where
import Data.Default (def)
import Data.Text (unpack)
import Text.Printf (printf)
import Network.Ethereum.Contract.TH
import Network.Ethereum.Web3 hiding (name)
[abiFrom|data/ERC20.json|]
main :: IO ()
main = do
result <- runWeb3 $ do
n <- name tokenCall
s <- symbol tokenCall
d <- decimals tokenCall
return $ printf "Token %s with symbol %s and decimals %d"
(unpack n) (unpack s) (fromIntegral d :: Int)
case result of
Left error -> print error
Right info -> putStrLn info
where
token :: Address
token = "0xA2f4FCb0FDe2dD59f7a1873e121bc5623e3164Eb"
tokenCall :: Call
tokenCall = def { callTo = Just token }
Additional minimalistic example of JSON ABI code genration available in data/ERC20.hs
.
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.