Read from the Subgraph
Read most data from the blockchain, as well as some additional higher-level fields without querying contracts themselves
Last updated
Read most data from the blockchain, as well as some additional higher-level fields without querying contracts themselves
Last updated
npm install -D ts-node graphql-request graphql{ "compilerOptions": { "moduleResolution": "node" } }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'
},
...
*/