GithubHelp home page GithubHelp logo

syngraph / hs-web3 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from airalab/hs-web3

0.0 1.0 0.0 394 KB

Ethereum API for Haskell.

Home Page: http://hackage.haskell.org/package/web3

License: BSD 3-Clause "New" or "Revised" License

Haskell 98.91% Shell 0.72% JavaScript 0.37%

hs-web3's Introduction

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.

> :t web3_clientVersion
web3_clientVersion :: Provider a => Web3 a Text

To run this computation used runWeb3' or runWeb3 functions.

> runWeb3 web3_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 DefaultProvider b -> m (Either Web3Error b)

TemplateHaskell generator

Quasiquotation is used to parse contract ABI or load from JSON file. TemplateHaskell driven Haskell contract API generator can automatical create instances for Event and Method typeclasses and function helpers.

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

See example of usage.

import Data.Text (unpack)
import Text.Printf

[abiFrom|data/ERC20.json|]

main :: IO ()
main = do
    Right s <- runWeb3 $ do
        n <- name token
        s <- symbol token
        d <- decimals token
        return $ printf "Token %s with symbol %s and decimals %d"
                        (unpack n) (unpack s) d
    putStrLn s
  where token = "0x237D60A8b41aFD2a335305ed458B609D7667D789"

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.

hs-web3's People

Contributors

akru avatar aupiff avatar benweitzman avatar hvr avatar iostat avatar kejace avatar martyall avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.