• Stars
    star
    127
  • Rank 282,790 (Top 6 %)
  • Language PureScript
  • License
    Apache License 2.0
  • Created about 7 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

a purescript library for the web3 api

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
> 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 :: UIntN (D2 :& D5 :& DOne D6), _y :: UIntN (D2 :& D5 :& DOne D6)} 
         -> Web3 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 :: TransactionOptions NoPay
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 :: Filter TupleSet
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

More Repositories

1

cliquebait

Scientists are SHOCKED by how easy it is to disrupt a $550B industry with this one weird trick!
Shell
106
star
2

chanterelle

Chanterelle -- a more functional truffle
PureScript
79
star
3

public-research

Public Repository for Documents related to FOAM Research
54
star
4

parking-dao

An Example Solidity Application Using the Chanterelle Build Tool
PureScript
43
star
5

kepler

A Haskell framework that facilitates writing ABCI applications
Haskell
34
star
6

purescript-web3-generator

A code generation tool for purescript-web3
PureScript
22
star
7

purescript-web3-example

PureScript
21
star
8

foam.lite

foam.lite - Ethereum transaction relayer over LoRa
C
16
star
9

purescript-servant

a servant like DSL for templating requests
PureScript
15
star
10

foam.developer

FOAM Developer portal
HTML
13
star
11

purescript-react-map-gl

purescript-react wrappers for map gl
PureScript
12
star
12

sample-nft-project

FOAM Signal Marketplace
PureScript
12
star
13

purescript-deck-gl

purescript wrapper for Uber's deck.gl library
PureScript
11
star
14

purescript-kitty-monitor

purescript-web3 + kitties
PureScript
7
star
15

purescript-eth-core

Common types and utility functions for all web3 libraries
PureScript
6
star
16

gaspasser

HTML
5
star
17

csc-explorer

JavaScript
4
star
18

foam.token-server

Haskell
4
star
19

plasma-demo

FOAM + plasma
PureScript
4
star
20

clash-yosys-demo-nix

Haskell
3
star
21

community-portal

A bridge between the community and the FOAM team.
3
star
22

web3-psql-example

storing event types in postgres
Haskell
3
star
23

nyc-haskell-presentation

HTML
2
star
24

chanterelle-halogen-template

Chanterelle + Halogen demo
PureScript
1
star
25

foam.css

JavaScript
1
star
26

hs-trillian

Haskell Bindings to the Trillian Authenticated Log and Maps
Haskell
1
star