Skip to content

Instantly share code, notes, and snippets.

@Cooops
Created November 16, 2023 04:27
Show Gist options
  • Save Cooops/06b59640614ac3dba90e54f8d4242ea7 to your computer and use it in GitHub Desktop.
Save Cooops/06b59640614ac3dba90e54f8d4242ea7 to your computer and use it in GitHub Desktop.
Detecting caps on Lodestar markets
const tokenAddresses = [
"0x5d27cFf80dF09f28534bb37d386D43aA60f88e25", //DPX
"0xD12d43Cdf498e377D3bfa2c6217f05B466E14228", //FRAX
"0xf21Ef887CB667f84B8eC5934C1713A7Ade8c38Cf", //MAGIC
"0xeA0a73c17323d1a9457D722F10E7baB22dc0cB83", //plvGLP
"0x4C9aAed3b8c443b4b634D1A189a5e25C604768dE", //USDC
"0x1ca530f02DD0487cef4943c674342c5aEa08922F", //USDC.e
"0x9365181A7df82a1cC578eAE443EFd89f00dbb643", //USDT
"0xC37896BF3EE5a2c62Cdbd674035069776f721668", //wBTC
"0x4987782da9a63bC3ABace48648B15546D821c720", //DAI
"0x2193c45244AF12C280941281c8aa67dD08be0a64", //ETH
"0x8991d64fe388fA79A4f7Aa7826E8dA09F0c3C96a", //ARB
"0xfECe754D92bd956F681A941Cef4632AB65710495", //wstETH
"0x79B6c5e1A7C0aD507E1dB81eC7cF269062BAb4Eb" //GMXß
];
const tokenNames = [
"DPX",
"FRAX",
"MAGIC",
"plvGLP",
"USDC",
"USDC.e",
"USDT",
"WBTC",
"DAI",
"ETH",
"ARB",
"wstETH",
"GMX"
];
const decimals = [
"18", // DPX
"18", // FRAX
"18", // MAGIC
"18", // plvGLP
"6", // USDC
"6", // USDC.e
"6", // USDT
"8", // wBTC
"18", // DAI
"18", // ETH
"18", // ARB
"18", // wstETH
"18" // GMX
];
// gather votingpower breakdowns and compute with above base lode speed to find emission buckets
const comptrollerABI = require('./comptrollerABI.json');
const ctokenABI = require('./ctokenABI.json');
const dotenv = require('dotenv').config();
const ethers = require('ethers');
const provider = new ethers.AlchemyProvider("arbitrum", process.env.ALCHEMY_API_KEY_ARBITRUM);
const unitrollerAddress = '0xa86dd95c210dd186fa7639f93e4177e97d057576';
const unitrollerContract = new ethers.Contract(unitrollerAddress, comptrollerABI, provider);
async function gatherCapsAndCurrentSupply() {
try {
// Fetch borrow caps for all addresses
const borrowCapsResults = await Promise.all(
tokenAddresses.map(address => unitrollerContract.borrowCaps(address))
);
// Fetch supply caps for all addresses
const supplyCapsResults = await Promise.all(
tokenAddresses.map(address => unitrollerContract.supplyCaps(address))
);
// Fetch current supply for each token
const currentSupplyResults = await Promise.all(
tokenAddresses.map(async address => {
let contract = new ethers.Contract(address, ctokenABI, provider);
let tokenSupply = await contract.totalSupply();
return tokenSupply;
})
);
// Fetch current supply for each token
const currentBorrowsResults = await Promise.all(
tokenAddresses.map(async address => {
let contract = new ethers.Contract(address, ctokenABI, provider);
let tokenSupply = await contract.totalBorrows();
return tokenSupply;
})
);
// Fetch the current exchange rates for each token
const currentExchangeResults = await Promise.all(
tokenAddresses.map(async address => {
let contract = new ethers.Contract(address, ctokenABI, provider);
let exchangeRate = await contract.exchangeRateStored();
return exchangeRate;
})
);
// Combine the token names with their corresponding borrow, supply cap, and current supply results
const combinedResults = tokenAddresses.map((address, index) => {
const borrowCap = borrowCapsResults[index] / BigInt(10 ** parseInt(decimals[index]));
const supplyCap = supplyCapsResults[index] / BigInt(10 ** parseInt(decimals[index]));
const currentSupply = ((currentSupplyResults[index] * currentExchangeResults[index]) / 1000000000000000000n) / BigInt(10 ** parseInt(decimals[index]));
const currentBorrow = ((currentBorrowsResults[index])) / BigInt(10 ** parseInt(decimals[index]));
let borrowCapPercent = 0;
let supplyCapPercent = Number((BigInt(currentSupply) * 100n) / BigInt(supplyCap));
if (tokenNames[index] != 'plvGLP') {
borrowCapPercent = Number((BigInt(currentBorrow) * 100n) / BigInt(borrowCap));
}
return {
tokenName: tokenNames[index],
address: address,
borrowCap: borrowCap.toString(),
supplyCap: supplyCap.toString(),
currentBorrows: currentBorrow.toString(),
currentSupply: currentSupply.toString(),
borrowCapPercent: `${borrowCapPercent.toString()}%`,
supplyCapPercent: `${supplyCapPercent.toString()}%`
};
});
// Sort the combined results by supplyCapPercent from highest to lowest
combinedResults.sort((a, b) => {
// Convert percentage strings to numbers for comparison
let supplyCapPercentA = parseFloat(a.supplyCapPercent.replace('%', ''));
let supplyCapPercentB = parseFloat(b.supplyCapPercent.replace('%', ''));
return supplyCapPercentB - supplyCapPercentA;
});
// Log the combined results
console.table(combinedResults)
} catch(error) {
console.error('Error gathering caps and current supply:', error);
}
}
(async function main() {
await gatherCapsAndCurrentSupply();
// await gatherVotingResults(); // If you need to call this as well
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment