Skip to content

Instantly share code, notes, and snippets.

@riordant
Last active November 11, 2020 04:51
Show Gist options
  • Save riordant/a4a46f33b3047e9f3122df0f47e32a11 to your computer and use it in GitHub Desktop.
Save riordant/a4a46f33b3047e9f3122df0f47e32a11 to your computer and use it in GitHub Desktop.
Get Liquidity (in USD) for all token pairings for an account from the UniSwap subgraph.
// npm install @apollo/client node-fetch apollo-link-http
// node account_liquidity_uniswap.js
apolloClient = require('@apollo/client')
fetch = require('node-fetch')
httpLink = require('apollo-link-http')
ApolloClient = apolloClient.ApolloClient
HttpLink = httpLink.HttpLink
InMemoryCache = apolloClient.InMemoryCache
gql = apolloClient.gql
const USER_POSITIONS = gql`
query liquidityPositions($user: Bytes!) {
liquidityPositions(where: { user: $user }) {
pair {
id
reserve0
reserve1
reserveUSD
token0 {
id
symbol
derivedETH
}
token1 {
id
symbol
derivedETH
}
totalSupply
}
liquidityTokenBalance
}
}
`
const client = new ApolloClient({
link: new HttpLink({
uri: 'https://api.thegraph.com/subgraphs/name/ianlapham/uniswapv2',
}),
cache: new InMemoryCache(),
shouldBatch: true,
fetch: fetch
})
async function WaitForResults(){
await client.query({
query: USER_POSITIONS,
variables: { // ACCOUNT MUST BE LOWER CASE, NOT EIP55 ENCODED
user: "0x85cf0a560260729b82c299b9117be117f7ee1c07",
},
fetchPolicy: 'no-cache',
})
.then(result => {
result.data.liquidityPositions.forEach(position => {
const valueUSD = (position.liquidityTokenBalance / position.pair.totalSupply) * position.pair.reserveUSD
console.log('pair: ' + position.pair.token0.symbol + "/" + position.pair.token1.symbol)
console.log('liquidity: ' + valueUSD)
})
});
}
WaitForResults();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment