Skip to content

sprusr/ethpollo

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Ethpollo logo

RPC is dead, long live GraphQL!

npm version

Interact with Ethereum contracts using GraphQL. Please note: this is very new, and probably broken. Full docs available at docs.ethpollo.dev.

Why does this exist?

Ethereum nodes recently got a standard (EIP 1767) for introducing GraphQL APIs, intended as a more modern alternative to the traditional RPC API. This is great, because it means we can take all the fantastic tooling that already exists for GQL and use it in our dApps! But what this API doesn't include is any kind of decoding for contract data. That's where ethpollo comes in...

This project is a custom "link" for apollo-client which transforms requests to something Ethereum nodes understand, and responses back to what we can understand - all on the client side. For example, you could write a query similar the following:

{
  SimpleStorage @contract {
    get
  }
}

The request will get turned into something like:

{
  call(data="0x...")
}

And then the response will be converted back to:

{
  "SimpleStorage": {
    "get": 1337
  }
}

Because it's totally client side, we can reap the benefits of the new API without adding in any element of centralisation. If that's not a concern for you, consider looking into EthQL. They're also the folks that helped get this project kick-started!

Get started

First of all, you'll need an Ethereum client which supports EIP 1797 (the GraphQL API). Your best bet for now is probably a development build of geth; you can get one from here.

Then you can start building! Here's a very unassuming example with a pretty standard apollo-client setup.

import { ApolloClient } from 'apollo-client'
import { InMemoryCache } from 'apollo-cache-inmemory'
import { HttpLink } from 'apollo-link-http'
import { concat } from 'apollo-link'
import gql from 'graphql-tag'

const CONTRACT_NAME = 'SimpleStorage'
const CONTRACT_ADDRESS = '0x...'
const CONTRACT_ABI = [...]
const ETHQL_ENDPOINT = 'http://localhost:4000/graphql'

const ethpollo = new Ethpollo(
  CONTRACT_NAME,
  CONTRACT_ADDRESS,
  CONTRACT_ABI,
)

const http = new HttpLink({ uri: ETHQL_ENDPOINT })

const client = new ApolloClient({
  link: concat(ethpollo, http),
  cache: new InMemoryCache(),
})

const query = gql`{
  SimpleStorage @contract {
    get
  }
}`

client.query({ query }).then(console.log)

Interactive example coming soon to ethpollo.dev.

Future plans

Right now ethpollo has some way to go! It definitely feels like this could be a great alternative to web3.js, but it first needs to support everything that can do. The short term roadmap looks something like the following:

  • Support parsing event logs
  • Suport calling non-constant functions (signing and sending transactions)
  • Support fetching additional data about addresses in the same query

If you have longer term ideas, it'd be great to hear them. You could consider contributing...

Contributing

If you're interested in contributing, that's great! Come hang out on the EthQL Gitter and let's get some ideas going.