Created
November 27, 2020 07:26
-
-
Save riordant/264663b0664fc7ab847b3472ff717e27 to your computer and use it in GitHub Desktop.
Get info on provided address for provided Balancer pool.
This file contains hidden or 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
// npm install axios | |
// node account_liquidity_balancer.js | |
axios = require('axios') | |
// variables | |
const poolId = "0xd3d14de568990854705c40221f75efbad8c0c981"; | |
const userAddress = "0x64a63ef1b19519be8afb9080e5725bd608d6f389"; | |
async function getPool(poolId, userAddress) { | |
const response = await axios({ | |
url: 'https://api.thegraph.com/subgraphs/name/balancer-labs/balancer', | |
method: 'post', | |
data: { | |
query: ` | |
{ | |
pool (id: "${poolId}") | |
{ | |
id | |
publicSwap | |
finalized | |
liquidity | |
swapFee | |
totalWeight | |
totalShares | |
totalSwapVolume | |
tokensList | |
tokens | |
{ | |
id | |
address | |
balance | |
decimals | |
symbol | |
denormWeight | |
} | |
shares (orderBy: balance, orderDirection: desc, where:{id: "${poolId + "-" + userAddress}", balance_gt:"0"}) | |
{ | |
id | |
userAddress { | |
id | |
} | |
balance | |
} | |
} | |
} | |
` | |
} | |
}); | |
return response; | |
}; | |
async function WaitForResults(){ | |
const response = await getPool(poolId, userAddress); | |
const pool = response.data.data.pool; | |
// Calculations | |
const firstToken = pool.tokens[0].symbol; | |
const secondToken = pool.tokens[1].symbol; | |
const userBPTBalance = parseFloat(pool.shares[0].balance); | |
const poolBPTBalance = parseFloat(pool.totalShares); | |
const percentageShares = userBPTBalance / poolBPTBalance; | |
const userValue = percentageShares * parseFloat(pool.liquidity); | |
const userFirstTokenShares = percentageShares * parseFloat(pool.tokens[0].balance); | |
const userSecondTokenShares = percentageShares * parseFloat(pool.tokens[1].balance); | |
console.log("User Address: " + userAddress); | |
console.log("Pool ID and Pair: " + poolId + ", " + firstToken + "-" + secondToken + "\n"); | |
console.log("Wallet BPT Balance: " + pool.shares[0].balance); | |
console.log("Wallet Percentage Shares: " + percentageShares * 100); | |
console.log("Wallet liquidity: " + userValue); | |
console.log( firstToken + " amount: " + userFirstTokenShares); | |
console.log(secondToken + " amount: " + userSecondTokenShares); | |
} | |
WaitForResults(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment