Created
November 24, 2022 10:01
-
-
Save nhancv/966c3adbfd62ee34a4a50d2092e964f5 to your computer and use it in GitHub Desktop.
Example query data of snapshot.org
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
DIR=snapshotdev | |
mkdir $DIR | |
cd $DIR | |
touch index.js | |
npm init -y | |
npm i isomorphic-unfetch | |
npm i @urql/core | |
# Docs: https://docs.snapshot.org/snapshot.js | |
# Query: | |
# - https://docs.snapshot.org/graphql-api | |
# - https://hub.snapshot.org/graphql?operationName=Votes | |
# GraphQL Client: | |
# - https://stepzen.com/docs/connecting-frontends/connecting-via-nodejs | |
# - https://formidable.com/open-source/urql/docs/basics/core/ | |
*/ | |
require("isomorphic-unfetch"); | |
const { createClient } = require("@urql/core"); | |
const client = new createClient({ | |
// url: "https://testnet.snapshot.org/graphql", // testnet | |
url: "https://hub.snapshot.org/graphql", | |
}); | |
// https://snapshot.org/#/cakevote.eth/proposal/0x637ce906e8a7be958b67b8d49e3a6ec52039811a9ed5c2aec51b7d3c125bd5f7 | |
(async () => { | |
const SPACE_ID = "cakevote.eth"; | |
const PROPOSAL_ID = | |
"0x637ce906e8a7be958b67b8d49e3a6ec52039811a9ed5c2aec51b7d3c125bd5f7"; | |
const QUERY_SPACE_INFO = ` | |
query { | |
space(id: "${SPACE_ID}") { | |
id | |
name | |
about | |
network | |
symbol | |
members | |
} | |
} | |
`; | |
const spaceInfo = await client.query(QUERY_SPACE_INFO).toPromise(); | |
console.log(spaceInfo.data); | |
const QUERY_PROPOSAL_INFO = ` | |
query { | |
proposals ( | |
first: 20, | |
skip: 0, | |
where: { | |
space: "${SPACE_ID}", | |
id: "${PROPOSAL_ID}" | |
}, | |
orderBy: "created", | |
orderDirection: desc | |
) { | |
id | |
title | |
body | |
choices | |
start | |
end | |
snapshot | |
state | |
scores | |
scores_by_strategy | |
scores_total | |
scores_updated | |
author | |
space { | |
id | |
name | |
} | |
} | |
} | |
`; | |
const proposalInfo = await client.query(QUERY_PROPOSAL_INFO).toPromise(); | |
console.log(proposalInfo.data.proposals[0]); | |
const QUERY_PROPOSAL_VOTES = ` | |
query { | |
votes ( | |
first: 1000 | |
skip: 0 | |
where: { | |
proposal: "${PROPOSAL_ID}" | |
} | |
orderBy: "created", | |
orderDirection: desc | |
) { | |
id | |
voter | |
vp | |
vp_by_strategy | |
vp_state | |
created | |
proposal { | |
id | |
} | |
choice | |
space { | |
id | |
} | |
} | |
} | |
`; | |
const proposalVotes = await client.query(QUERY_PROPOSAL_VOTES).toPromise(); | |
console.log(proposalVotes.data.votes); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment