Read from the Subgraph

Read most data from the blockchain, as well as some additional higher-level fields without querying contracts themselves

Read Only: The subgraph

Most of the data from the blockchain, and additional higher level fields are readable from the subgraph (quick video).

Kovan: https://thegraph.com/explorer/subgraph/charged-particles/kovan-universe?query=Protons%20NFTs

We recommend digging into the Schema yourselves to figure out what is available to query.

Please also pay attention to some example queries we provide on the top left

Almost all the information available from the contracts is available somewhere on the subgraph, besides the current accrued interest (ie: how much my DAI at 12% APY in my ProtonNFT made me) which is currently available only by contract queryingcurrentParticleCharge(address,uint256,string,address)(uint256)

Sample Interaction using Node.js

The sample is in Typescript using ts-node but you can use CommonJs require as well.

npm install -D ts-node graphql-request graphql

graphql-request is the most minimal and simplest to use GraphQL client. It's perfect for small scripts or simple apps. But it doesn't allow you to listen for changes for this, please use something like apollo-client.

You might also need a tsconfig.json at the root of your project with

{ "compilerOptions": { "moduleResolution": "node" } }

until https://github.com/prisma-labs/graphql-request/issues/214 is resolved.

getFirst20Protons.ts
import { request, gql } from 'graphql-request';

const query = gql`
{
   universes {
     protonToken {
       tokens(first:20) {
         name,
         tokenId,
         id
         creator,
         owner,
         salePrice,
         lastSalePrice,
       }
     }
   }
 }`
 
 const doGraphRequest = (endpoint, query, variables={}) => new Promise((resolve, reject) => {
  request(endpoint, query, variables)
    .then((data) => resolve(data))
    .catch(err => reject(err));
});

async function run() {
  const endpoint = 'https://api.thegraph.com/subgraphs/name/charged-particles/kovan-universe';
  const res: any = await doGraphRequest(endpoint, query);
  const { protonToken } = res.universes[0];
  console.log(protonToken);
}

run();
# Run it
./node_modules/.bin/ts-node getFirst20Protons.ts

/* Outputs:


{
  tokens: [
    {
      creator: '0xb14d1a16f30db670097da86d4008640c6ccc2b76',
      id: '0xd4f7389297d9cea850777ea6ccbd7db5817a12b2-1',
      lastSalePrice: null,
      name: 'ETH on Mars',
      owner: '0xb14d1a16f30db670097da86d4008640c6ccc2b76',
      salePrice: '69000000000000000000',
      tokenId: '1'
    },
    {
      creator: '0x11113c2fba9ae08d7d16c817f03de51f29117db2',
      id: '0xd4f7389297d9cea850777ea6ccbd7db5817a12b2-10',
      lastSalePrice: null,
      name: 'ha',
      owner: '0x11113c2fba9ae08d7d16c817f03de51f29117db2',
      salePrice: '0',
      tokenId: '10'
    },
    ...
*/

If you want access to VSCode debugger and be able to set breakpoints check Debugging so you can use debugger; statement.

Last updated