Skip to content

Commit

Permalink
add how to calculate apy defi
Browse files Browse the repository at this point in the history
  • Loading branch information
jklepatch committed Dec 11, 2020
1 parent cb36da4 commit 40f82d5
Show file tree
Hide file tree
Showing 4 changed files with 663 additions and 0 deletions.
83 changes: 83 additions & 0 deletions screencast/266-how-to-calculate-apy-defi/apy1.js
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();
15 changes: 15 additions & 0 deletions screencast/266-how-to-calculate-apy-defi/apy2.js
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();
Loading

0 comments on commit 40f82d5

Please sign in to comment.