forked from jklepatch/eattheblocks
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
663 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const Compound = require('@compound-finance/compound-js'); | ||
|
||
const provider = 'INFURA URL HERE'; | ||
|
||
const comptroller = Compound.util.getAddress(Compound.Comptroller); | ||
const opf = Compound.util.getAddress(Compound.PriceFeed); | ||
|
||
const cTokenDecimals = 8; // always 8 | ||
const blocksPerDay = 4 * 60 * 24; // 4 blocks in 1 minute | ||
const daysPerYear = 365; | ||
const ethMantissa = Math.pow(10, 18); // 1 * 10 ^ 18 | ||
|
||
async function calculateSupplyApy(cToken, ticker) { | ||
const supplyRatePerBlock = await Compound.eth.read( | ||
cToken, | ||
'function supplyRatePerBlock() returns (uint)', | ||
[], | ||
{ provider } | ||
); | ||
|
||
return 100 * (Math.pow((supplyRatePerBlock / ethMantissa * blocksPerDay) + 1, daysPerYear - 1) - 1); | ||
} | ||
|
||
async function calculateCompApy(cToken, ticker, underlyingDecimals) { | ||
let compSpeed = await Compound.eth.read( | ||
comptroller, | ||
'function compSpeeds(address cToken) public returns (uint)', | ||
[ cToken ], | ||
{ provider } | ||
); | ||
|
||
let compPrice = await Compound.eth.read( | ||
opf, | ||
'function price(string memory symbol) external view returns (uint)', | ||
[ Compound.COMP ], | ||
{ provider } | ||
); | ||
|
||
let totalSupply = await Compound.eth.read( | ||
cToken, | ||
'function totalSupply() returns (uint)', | ||
[], | ||
{ provider } | ||
); | ||
|
||
let exchangeRate = await Compound.eth.read( | ||
cToken, | ||
'function exchangeRateCurrent() returns (uint)', | ||
[], | ||
{ provider } | ||
); | ||
|
||
exchangeRate = +exchangeRate.toString() / ethMantissa; | ||
compSpeed = compSpeed / 1e18; // COMP has 18 decimal places | ||
compPrice = compPrice / 1e6; // price feed is USD price with 6 decimal places | ||
totalSupply = (+totalSupply.toString() * exchangeRate) / (Math.pow(10, underlyingDecimals)); | ||
const compPerDay = compSpeed * blocksPerDay; | ||
|
||
return 100 * (compPrice * compPerDay / totalSupply) * 365; | ||
} | ||
|
||
async function calculateApy(cToken, ticker) { | ||
const underlyingDecimals = Compound.decimals[cToken.slice(1, 10)]; | ||
const cTokenAddress = Compound.util.getAddress(cToken); | ||
const [supplyApy, compApy] = await Promise.all([ | ||
calculateSupplyApy(cTokenAddress, ticker), | ||
calculateCompApy(cTokenAddress, ticker, underlyingDecimals) | ||
]); | ||
console.log(`Supply APY for ${ticker}: ${supplyApy}%`); | ||
console.log(`COMP APY for ${ticker}: ${compApy}%`); | ||
console.log(`Total APY for ${ticker}: ${supplyApy + compApy}%`); | ||
console.log('---'); | ||
} | ||
|
||
async function main() { | ||
await Promise.all([ | ||
calculateApy(Compound.cDAI, 'DAI'), | ||
calculateApy(Compound.cUSDC, 'USDC'), | ||
calculateApy(Compound.cUSDT, 'USDT'), | ||
]); | ||
} | ||
|
||
main(); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
const axios = require('axios'); | ||
|
||
async function main() { | ||
const results = await axios.get('https://api.compound.finance/api/v2/ctoken'); | ||
results.data.cToken.forEach(cToken => { | ||
const supplyRate = cToken.supply_rate.value * 100; | ||
const compRate = +cToken.comp_supply_apy.value; | ||
console.log(`Supply APY for ${cToken.underlying_symbol}: ${supplyRate}%`); | ||
console.log(`COMP Supply APY for ${cToken.underlying_symbol}: ${compRate}%`); | ||
console.log(`Total Supply APY for ${cToken.underlying_symbol}: ${compRate + supplyRate}`); | ||
console.log('---'); | ||
}); | ||
} | ||
|
||
main(); |
Oops, something went wrong.