Giter Site home page Giter Site logo

purescript-web3's Introduction

purescript-web3

Latest release purescript-web3 on Pursuit Build status

A Purescript Client for the Web3 API

purescript-web3 is a library for interacting with an ethereum node purescript. At the moment it covers most endpoints of the web3 api, which means it is suitable for sending transactions, querying blockchain state and metadata, and monitoring events.

Using purescript-web3-generator it is also possible (and recommended) to generate a library from a set of smart contract abis which is capable of templating transactions and event filters/watchers. The README has instructions for getting started.

We do not yet have a build tool similar to truffle, but if you are looking for a template of how to use truffle and write your tests using purescript, check out out the purescript-web3-tests

To see an example project using all of the purescript-web3 tools and with thermite/react ui, check out purescript-web3-example.

Build Instructions

> npm install
> bower install
> npm run build
> npm run test

Documentation

Module documentation is published on Pursuit.

Examples

Suppose we have the following solidity smart contract:

contract TupleStorage {
    
    uint x;
    uint y;
    
    event TupleSet(uint newX, uint newY);
    
    function setTuple(uint _x, uint _y) public {
        x = _x;
        y = _y;
        TupleSet(_x, _y);
    }
    
}

If we used purescript-web3-generator, we are given a function with the following signature:

setTuple :: forall e.
            TransactionOptions NoPay 
         -> {_x :: UInt (D2 :& D5 :& D6), _y :: UInt (D2 :& D5 :& D6)} 
         -> Web3 e HexString 

It's pretty clear what this function is doing, but let's look at the TransactionOptions. This record keeps track of, for example, who is the transaction from, what contract address is it going to, is there ether being sent, etc. In this case, the function is not "payable", so this is indicated in the type of the TransactionOptions. It is set using lenses like:

setTupleOpts = defaultTransactionOptions
             # _from ?~ myAddress
             # _to ?~ tupleStorageAddress

Now for the TupleSet event. In order to start an event watcher, we need to establish the Filter, which specifies things like the range of blocks we are interested in, and how to find that particular contract and topic. Again, if you're using web3-generator, things are a lot simpler:

tupleFilter = eventFilter (Proxy :: Proxy TupleSet) tupleStorageAddress 
           # _fromBlock .~ BN 100

We also need to pass a callback to the event watcher that performs some action and decides whether or not to unregister the filter. For example, we could set up an event monitor starting from block 100 and continuing until the two coordinates that are set are equal:

event tupleFilter $ \(TupleSet {newX,newY} -> do
  liftAff <<< log $ "Received New Tuple : " <> show (Tuple newX newY) 
  if newX == newY
    then pure TerminateEvent
    else do
      _ <- performAction newX newY
      pure ContinueEvent

For more examples, check out the foam kitty monitor, the example project using thermite, or the purescript-web3-tests repo.

Resources

purescript-web3's People

Contributors

martyall avatar kejace avatar safareli avatar xertrov avatar iostat avatar cmditch avatar wildmolasses avatar sectore avatar

Watchers

James Cloos avatar Josh Burgess avatar  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.