Created
March 7, 2021 11:49
-
-
Save guifel/be49c6e341e96e611937b0ee1a2294ba to your computer and use it in GitHub Desktop.
Get RAI from LP token balances and debts
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
import Axios from "axios"; | |
import * as fs from "fs"; | |
const SUBGRAPH_URL = | |
"https://subgraph.reflexer.finance/subgraphs/name/reflexer-labs/rai"; | |
const main = async () => { | |
// RAI reserve / LP total supply | |
const lpToRaiCoefficient = 28660219.789 / 1283759.973; | |
const accumulatedRate = 1.0009347896036; | |
// Get RAI LP balances | |
let users: { [key: string]: { lpBalance: number; debt: number } } = {}; | |
let skip = 0; | |
do { | |
const query = ` | |
{ | |
erc20Balances(where: {label: "UNISWAP_POOL_TOKEN_COIN"}, first: 1000, skip: ${skip}) { | |
balance | |
address | |
} | |
} | |
`; | |
const data: { | |
erc20Balances: { balance: string; address: string }[]; | |
} = await subgraphQuery(query, SUBGRAPH_URL); | |
skip = data.erc20Balances.length >= 1000 ? skip + 1000 : 0; | |
for (let x of data.erc20Balances) { | |
if (x.balance && Number(x.balance)) { | |
users[x.address] = { | |
lpBalance: Number(x.balance) * lpToRaiCoefficient, | |
debt: 0, | |
}; | |
} | |
} | |
} while (skip); | |
// Get safe debt | |
skip = 0; | |
do { | |
const query = ` | |
{ | |
safes(first: 1000, skip: ${skip}) { | |
debt | |
owner { | |
address | |
} | |
} | |
} | |
`; | |
const data: { | |
safes: { debt: string; owner: { address: string } }[]; | |
} = await subgraphQuery(query, SUBGRAPH_URL); | |
skip = data.safes.length >= 1000 ? skip + 1000 : 0; | |
for (let x of data.safes) { | |
if (x.debt && Number(x.debt)) { | |
if (users[x.owner.address]) { | |
users[x.owner.address].debt = Number(x.debt) * accumulatedRate; | |
} else { | |
users[x.owner.address] = { | |
lpBalance: 0, | |
debt: Number(x.debt) * accumulatedRate, | |
}; | |
} | |
} | |
} | |
} while (skip); | |
// Dump data | |
let w = "Address, RAI LP balance, Debt\n"; | |
for (let x in users) { | |
w += `${x},${users[x].lpBalance},${users[x].debt}\n`; | |
} | |
fs.writeFileSync("lp-minter.csv", w); | |
let minterLpCount = 0; | |
let lpCount = 0; | |
let debtCount = 0; | |
let zeroDebtLp = 0; | |
for (let x in users) { | |
const debt = users[x].debt; | |
const lp = users[x].lpBalance; | |
if (lp > 0) { | |
lpCount++; | |
} | |
if (debt > 0) { | |
debtCount++; | |
} | |
if (lp > 0 && debt > 0) { | |
minterLpCount++; | |
} | |
if (lp > 0 && debt === 0) { | |
zeroDebtLp++; | |
} | |
} | |
console.log(`Addresses with LP tokens: ${lpCount}`); | |
console.log(`Addresses with debt: ${debtCount}`); | |
console.log(`Addresses with debt and LP tokens: ${minterLpCount}`); | |
console.log(`Addresses with LP tokens and zero debt: ${zeroDebtLp}`); | |
}; | |
const subgraphQuery = async ( | |
query: string, | |
url: string | |
): Promise<any> => { | |
const prom = Axios.post(url, { | |
query, | |
}); | |
let resp: any; | |
try { | |
resp = await prom; | |
} catch (err) { | |
throw Error("Error with subgraph query: " + err); | |
} | |
if (!resp.data || !resp.data.data) { | |
throw Error("No data"); | |
} | |
return resp.data.data; | |
}; | |
main(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment