GithubHelp home page GithubHelp logo

deltacamp / apollo-link-ethereum-example Goto Github PK

View Code? Open in Web Editor NEW
7.0 1.0 1.0 367 KB

An example project that uses apollo-link-ethereum https://github.com/DeltaCamp/apollo-link-ethereum

HTML 10.58% CSS 5.66% JavaScript 83.76%

apollo-link-ethereum-example's Introduction

Apollo Link Ethereum Example App

This app demonstrates the use of apollo-link-ethereum

Live here (not much to see though ;)

Setup

$ yarn

Usage

Start the Create React App dev server:

$ yarn start

Now navigate to http://localhost:3000 in your browser. The app is hard-coded to mainnet, so MetaMask will not affect the output.

Highlights

First you should take note of apolloClient.js. This is where the EthereumLink is setup:

// apolloClient.js

export const abiMapping = new AbiMapping()
abiMapping.addAbi('MKR', mkrAbi)
abiMapping.addAddress('MKR', 1, '0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2')

const provider = new ethers.getDefaultProvider('homestead')

const ethersResolver = new EthersResolver({
  abiMapping,
  provider
})

const ethereumLink = new EthereumLink(ethersResolver)

// ...

export const apolloClient = new ApolloClient({
  cache,
  link: ApolloLink.from([stateLink, ethereumLink])
})

Next is to look at how we query Ethereum in App.js:

// src/App.js

const MKR_QUERY = gql`
  query MkrQuery {
    MKR @contract {
      totalSupply
      someonesBalance: balanceOf(address: "0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2")
      allEvents @pastEvents(fromBlock: "4620855", toBlock: 4621855)
    }
  }
`

class App extends Component {
  render() {
    return (
      <Query query={MKR_QUERY}>
        {/* ... */}
      </Query>
    )
  }
}

You'll notice the custom directives @contract and @pastEvents. These are defined by apollo-link-ethereum and will send the requests to the Ethereum blockchain.

apollo-link-ethereum-example's People

Contributors

asselstine avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

isabella232

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.