Skip to content

Commit

Permalink
add borrow related fields (DefiLlama#368)
Browse files Browse the repository at this point in the history
  • Loading branch information
sbbutler authored Oct 11, 2022
1 parent b01ef82 commit 00fbc73
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 19 deletions.
29 changes: 29 additions & 0 deletions src/adaptors/damm-finance/abis.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,35 @@
"stateMutability": "view",
"type": "function"
},
"markets": {
"inputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"name": "markets",
"outputs": [
{
"internalType": "bool",
"name": "isListed",
"type": "bool"
},
{
"internalType": "uint256",
"name": "collateralFactorMantissa",
"type": "uint256"
},
{
"internalType": "bool",
"name": "isComped",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
"compSpeeds": {
"inputs": [
{
Expand Down
56 changes: 37 additions & 19 deletions src/adaptors/damm-finance/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,25 @@ const poolInfo = async (chain) => {

const allMarkets = await sdk.api.abi.call({ target: unitroller, chain, abi: abi.getAllMarkets });

const compSpeedsPerBlock = (await sdk.api.abi.multiCall({
abi: abi.compSpeeds,
target: unitroller,
calls: allMarkets.output.map(token => ({
params: token
})),
chain
})).output.map((speeds) => speeds.output);

const yieldMarkets = allMarkets.output.map((pool) => {
return { pool };
});

const getOutput = ({ output }) => output.map(({ output }) => output);
const [supplyRatePerBlock, getCash, totalBorrows, totalReserves, underlyingToken, tokenSymbol] = await Promise.all(
['supplyRatePerBlock', 'getCash', 'totalBorrows', 'totalReserves', 'underlying', 'symbol'].map((method) => sdk.api.abi.multiCall({
const [markets, compSpeeds] = await Promise.all(
['markets', 'compSpeeds'].map((method) => sdk.api.abi.multiCall({
abi: abi[method],
target: unitroller,
calls: yieldMarkets.map((pool) => ({
params: pool.pool
})),
chain
}))
).then((data) => data.map(getOutput));
const collateralFactor = markets.map((data) => data.collateralFactorMantissa);

const [borrowRatePerBlock, supplyRatePerBlock, getCash, totalBorrows, totalReserves, underlyingToken, tokenSymbol] = await Promise.all(
['borrowRatePerBlock', 'supplyRatePerBlock', 'getCash', 'totalBorrows', 'totalReserves', 'underlying', 'symbol'].map((method) => sdk.api.abi.multiCall({
abi: abi[method],
calls: yieldMarkets.map((address) => ({
target: address.pool
Expand All @@ -45,8 +48,10 @@ const poolInfo = async (chain) => {
const price = await getPrices('ethereum', underlyingToken);

yieldMarkets.map((data, i) => {
data.collateralFactor = collateralFactor[i];
data.borrowRate = borrowRatePerBlock[i];
data.supplyRate = supplyRatePerBlock[i];
data.compSpeeds = compSpeedsPerBlock[i];
data.compSpeeds = compSpeeds[i];
data.getCash = getCash[i];
data.totalBorrows = totalBorrows[i];
data.totalReserves = totalReserves[i];
Expand Down Expand Up @@ -90,30 +95,38 @@ function calculateApy(rate, price = 1, tvl = 1) {
// supply rate per block * number of blocks per year
const BLOCK_TIME = 12;
const YEARLY_BLOCKS = 365 * 24 * 60 * 60 / BLOCK_TIME;
const apy = ((rate / 1e18) * YEARLY_BLOCKS * price / tvl) * 100;
const safeTvl = (tvl === 0) ? 1 : tvl;
const apy = ((rate / 1e18) * YEARLY_BLOCKS * price / safeTvl) * 100;
return apy;
};

function calculateTvl(cash, borrows, price, decimals) {
function calculateTvl(cash, borrows, reserves, price, decimals) {
// ( cash + totalBorrows - reserve value ) * underlying price = balance
const tvl = (parseFloat(cash) + parseFloat(borrows)) / decimals * price;
const tvl = ((parseFloat(cash) + parseFloat(borrows) - parseFloat(reserves)) / decimals) * price;
return tvl;
};

const getApy = async () => {
const bdammPrice = await getTerminalPrices();
const yieldPools = (await poolInfo('ethereum')).yieldMarkets.map((pool, i) => {
const tvl = calculateTvl(pool.getCash, pool.totalBorrows, pool.price, pool.underlyingTokenDecimals);
const totalSupplyUsd = calculateTvl(pool.getCash, pool.totalBorrows, pool.totalReserves, pool.price, pool.underlyingTokenDecimals);
const totalBorrowUsd = calculateTvl(0, pool.totalBorrows, 0, pool.price, pool.underlyingTokenDecimals);
const tvlUsd = totalSupplyUsd - totalBorrowUsd;
const apyBase = calculateApy(pool.supplyRate);
const apyReward = calculateApy(pool.compSpeeds, bdammPrice, tvl);
const readyToExport = exportFormatter(pool.pool, 'Ethereum', pool.tokenSymbol, tvl, apyBase, apyReward, pool.underlyingToken, [bdAMM]);
const apyReward = calculateApy(pool.compSpeeds, bdammPrice, totalSupplyUsd);
const apyBaseBorrow = calculateApy(pool.borrowRate);
const apyRewardBorrow = calculateApy(pool.compSpeeds, bdammPrice, totalBorrowUsd);
const ltv = parseInt(pool.collateralFactor) / 1e18;
const readyToExport = exportFormatter(pool.pool, 'Ethereum', pool.tokenSymbol, tvlUsd, apyBase, apyReward,
pool.underlyingToken, [bdAMM], apyBaseBorrow, apyRewardBorrow, totalSupplyUsd, totalBorrowUsd, ltv);
return readyToExport;
});

return yieldPools;
};

function exportFormatter(pool, chain, symbol, tvlUsd, apyBase, apyReward, underlyingTokens, rewardTokens) {
function exportFormatter(pool, chain, symbol, tvlUsd, apyBase, apyReward,
underlyingTokens, rewardTokens, apyBaseBorrow, apyRewardBorrow, totalSupplyUsd, totalBorrowUsd, ltv) {

return {
pool: `${pool}-${chain}`.toLowerCase(),
Expand All @@ -125,6 +138,11 @@ function exportFormatter(pool, chain, symbol, tvlUsd, apyBase, apyReward, underl
apyReward,
underlyingTokens: [underlyingTokens],
rewardTokens,
apyBaseBorrow,
apyRewardBorrow,
totalSupplyUsd,
totalBorrowUsd,
ltv,
};
};

Expand Down

0 comments on commit 00fbc73

Please sign in to comment.