Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add blend pool yields #1600

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions src/adaptors/blend-pools/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
const {
PoolConfig,
ReserveData,
BackstopConfig,
Pool,
BackstopToken,
Backstop,
FixedMath,
} = require('@blend-capital/blend-sdk');
const { getPrices, keepFinite, formatChain } = require('../utils');

const BACKSTOP_ID = 'CAO3AGAMZVRMHITL36EJ2VZQWKYRPWMQAPDQD5YEOF3GIF7T44U4JAL3';
const BLND_ID = 'CD25MNVTZDL4Y3XBCPCJXGXATV5WUHHOWMYFF4YBEGU5FCPGMYTVG5JY';
const network = {
rpc: 'https://soroban-rpc.creit.tech/',
passphrase: 'Public Global Stellar Network ; September 2015',
};

const getApy = async (poolId, backstop) => {
const pool = await Pool.load(network, poolId);
// Skip pools that have been admin frozen - Pool is very likely to be broken
if (pool.config.status === 4) return [];
const prices = await getPrices(pool.config.reserveList, 'stellar');
let pools = [];
for (const reserve of pool.reserves.values()) {
const price = prices.pricesByAddress[reserve.assetId.toLowerCase()];
if (price) {
let supplyEmissionsPerAsset = reserve.emissionsPerYearPerSuppliedAsset();
let borrowEmissionsPerAsset = reserve.emissionsPerYearPerBorrowedAsset();
let supplyEmissionsAPR = undefined;
let borrowEmissionsAPR = undefined;
// The backstop token is an 80/20 weighted lp token of blnd and usdc respectively
// (Calculated using balancer spot equation)
// @TODO replace with coingecko price after listing
const usdcPerBlnd =
FixedMath.toFloat(backstop.backstopToken.usdc, 7) /
0.2 /
(FixedMath.toFloat(backstop.backstopToken.blnd, 7) / 0.8);
if (supplyEmissionsPerAsset > 0) {
supplyEmissionsAPR = (supplyEmissionsPerAsset * usdcPerBlnd) / price;
}
if (borrowEmissionsPerAsset > 0) {
borrowEmissionsAPR = (borrowEmissionsPerAsset * usdcPerBlnd) / price;
}
// Estimate borrow APY compoumded daily
const borrowApy = (1 + reserve.borrowApr / 365) ** 365 - 1;
let totalSupply = reserve.totalSupplyFloat() * price;
let totalBorrow = reserve.totalLiabilitiesFloat() * price;

const url = `https://mainnet.blend.capital/dashboard/?poolId=${poolId}`;

pools.push({
pool: `${poolId}-${reserve.assetId}-stellar`.toLowerCase(),
chain: formatChain('stellar'),
project: 'blend-pools',
symbol: reserve.tokenMetadata.symbol,
tvlUsd: totalSupply - totalBorrow,
// Supply is kept as APR to prevent overestimation of APY
apyBase: reserve.supplyApr,
apyReward: supplyEmissionsAPR,
underlyingTokens: [reserve.assetId],
rewardTokens: borrowEmissionsAPR || supplyEmissionsAPR ? [BLND_ID] : [],
totalSupplyUsd: totalSupply,
totalBorrowUsd: totalBorrow,
apyBaseBorrow: borrowApy,
apyRewardBorrow: borrowEmissionsAPR,
ltv: totalBorrow / totalSupply,
url,
});
}
}
return pools;
};

const apy = async () => {
let backstop = await Backstop.load(network, BACKSTOP_ID);
const pools = await Promise.allSettled(
backstop.config.rewardZone.map(async (poolId) => getApy(poolId, backstop))
);
return pools
.filter((i) => i.status === 'fulfilled' && i.value !== undefined)
.map((i) => i.value)
.flat()
.filter((p) => p !== undefined && keepFinite(p));
};

module.exports = {
apy,
};
154 changes: 146 additions & 8 deletions src/adaptors/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 5 additions & 4 deletions src/adaptors/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@
"author": "",
"license": "ISC",
"dependencies": {
"@blend-capital/blend-sdk": "^2.1.1",
"@defillama/sdk": "^5.0.60",
"@stacks/network": "^6.13.0",
"@stacks/transactions": "^6.15.0",
"@ton/ton": "14.0.0",
"@types/jest": "^28.1.6",
"@uniswap/sdk-core": "^5.3.0",
"@uniswap/v3-sdk": "^3.13.0",
Expand All @@ -24,10 +28,7 @@
"lodash": "^4.17.21",
"node-fetch": "^2.6.1",
"superagent": "^6.1.0",
"web3": "^4.9.0",
"@ton/ton": "14.0.0",
"@stacks/network": "^6.13.0",
"@stacks/transactions": "^6.15.0"
"web3": "^4.9.0"
},
"devDependencies": {
"jest": "^28.1.3",
Expand Down
Loading