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 Size Credit #1675

Merged
merged 9 commits into from
Jan 13, 2025
Merged
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
172 changes: 172 additions & 0 deletions src/adaptors/size-credit/api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
const fetch = require('node-fetch');

/*
export interface Market {
id: string;
name: string;
api_base_url: string;
documentation_url: string;
chain: string;
base_symbol: string;
quote_symbol: string;
collateral_token: {
address: string;
decimals: number;
};
debt_token: {
address: string;
decimals: number;
};
data_params: {
weth: string;
underlying_collateral_token: string;
underlying_borrow_token: string;
variable_pool: string;
borrow_a_token_v1_5: string;
};
fee_config: {
swap_fee_apr: number;
fragmentation_fee: number;
liquidation_reward_percent: number;
overdue_collateral_protocol_percent: number;
collateral_protocol_percent: number;
fee_recipient: string;
};
risk_config: {
cr_opening: number;
cr_liquidation: number;
minimum_credit_borrow_a_token: number;
borrow_a_token_cap: number;
min_tenor: number;
max_tenor: number;
};
oracle_params: {
price_feed: string;
variable_pool_borrow_rate_stale_rate_interval: number;
};
}

interface GetMarketsResponse {
markets_by_chain: Record<string, Market[]>
}

interface GetTvlResponse {
total_tvl_usd: number;
collateral_tvl: number;
collateral_tvl_usd: number;
debt_tvl: number;
debt_tvl_usd: number;
total_borrow: number;
total_borrow_usd: number;
}

interface GetBestRateResponse {
tenors: number[];
aprs: Array<number|string>;
contributing_curves:
{
curve: {
curve_relative_time_aprs: number[];
curve_relative_time_market_rate_multipliers: number[];
curve_relative_time_tenors: number[];
id: string;
block_number: number;
block_timestamp: string;
transaction_hash: string;
transfer_from: string;
max_due_date: number;
user_address: string;
depth: number;
},
apr: number;
depth_used_borrow_token: number;
depth_used_collateral_token: number;
}[][]
}

interface GetMarketsLiquidityResponse {
[market_id: string]: {
total_liquidity_usd: number;
sell_side_liquidity_usd: number;
buy_side_liquidity_usd: number;
sell_liquidity: number;
buy_liquidity: number;
};
}
*/

const ENDPOINT = 'https://api.size.credit';

async function getMarkets() /*: Promise<Market[]>*/ {
const getMarketsResponse /*: GetMarketsResponse*/ = await fetch(
`${ENDPOINT}/`
).then((res) => res.json());
return Object.values(getMarketsResponse.markets_by_chain).flat();
}

async function getMarketsLiquidity(
market /*: Market*/
) /*: Promise<GetMarketsLiquidityResponse>*/ {
const getMarketsLiquidityResponse /*: GetMarketsLiquidityResponse*/ =
await fetch(`${ENDPOINT}/markets-liquidity`).then((res) => res.json());
return getMarketsLiquidityResponse;
}

async function getTvl(market /*: Market*/) /*: Promise<GetTvlResponse>*/ {
const getTvlResponse /*: GetTvlResponse*/ = await fetch(
`${ENDPOINT}${market.api_base_url}/market-tvl`
).then((res) => res.json());
return getTvlResponse;
}

async function lendingAPR(
market /*: Market*/,
tenor /*: number*/,
depth /*: number*/
) /*: Promise<number|undefined>*/ {
const getBestRateResponse /*: GetBestRateResponse*/ = await fetch(
`${ENDPOINT}${market.api_base_url}/best-rate-for-tenors-scl/`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ tenors: [tenor], depth }),
}
).then((res) => res.json());
const apr18decimals = getBestRateResponse.aprs[0];
if (typeof apr18decimals === 'string') {
return undefined;
}
return apr18decimals / 1e16;
}

async function borrowingAPR(
market /*: Market*/,
tenor /*: number*/,
depth /*: number*/
) /*: Promise<number|undefined>*/ {
const getBestRateResponse /*: GetBestRateResponse*/ = await fetch(
`${ENDPOINT}${market.api_base_url}/best-rate-for-tenors-bcl/`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ tenors: [tenor], depth }),
}
).then((res) => res.json());
const apr18decimals = getBestRateResponse.aprs[0];
if (typeof apr18decimals === 'string') {
return undefined;
}
return apr18decimals / 1e16;
}

module.exports = {
getMarkets,
getTvl,
lendingAPR,
borrowingAPR,
getMarketsLiquidity,
};
87 changes: 87 additions & 0 deletions src/adaptors/size-credit/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
const {
borrowingAPR,
getMarkets,
lendingAPR,
getTvl,
getMarketsLiquidity,
} = require('./api');
const sdk = require('@defillama/sdk');
const AaveV3Pool = require('../aave-v3/poolAbi');

const AaveProtocolDataProvider = {
ethereum: '0x41393e5e337606dc3821075Af65AeE84D7688CBD',
optimism: '0x7F23D86Ee20D869112572136221e173428DD740B',
arbitrum: '0x7F23D86Ee20D869112572136221e173428DD740B',
polygon: '0x7F23D86Ee20D869112572136221e173428DD740B',
fantom: '0x69FA688f1Dc47d4B5d8029D5a35FB7a548310654',
avax: '0x374a2592f0265b3bb802d75809e61b1b5BbD85B7',
metis: '0xC01372469A17b6716A38F00c277533917B6859c0',
base: '0xd82a47fdebB5bf5329b09441C3DaB4b5df2153Ad',
xdai: '0x57038C3e3Fe0a170BB72DE2fD56E98e4d1a69717',
bsc: '0x23dF2a19384231aFD114b036C14b6b03324D79BC',
scroll: '0xe2108b60623C6Dcf7bBd535bD15a451fd0811f7b',
era: '0x5F2A704cE47B373c908fE8A29514249469b52b99',
lido: '0x08795CFE08C7a81dCDFf482BbAAF474B240f31cD', // on ethereum
etherfi: '0xE7d490885A68f00d9886508DF281D67263ed5758', // on ethereum
};

const TENOR_DAYS = 3;
const DAYS_TO_SECONDS = 24 * 60 * 60;

const DEPTH_BORROW_TOKEN = 10;

function uppercaseFirst(str /*: string*/) /*: string*/ {
return str.charAt(0).toUpperCase() + str.slice(1);
}

async function apy() /*: Promise<Pool[]>*/ {
const [markets, marketsLiquidity] = await Promise.all([
getMarkets(),
getMarketsLiquidity(),
]);

return Promise.all(
markets.map(async (market) => {
const tvl = await getTvl(market);
const uppercaseChain = uppercaseFirst(market.chain);
let apyBase = await lendingAPR(
market,
TENOR_DAYS * DAYS_TO_SECONDS,
DEPTH_BORROW_TOKEN
);
if (apyBase === undefined) {
// no limit borrow offers available, use Aave v3 as a variable-rate lending pool
const { output: getReserveData } = await sdk.api.abi.call({
target: AaveProtocolDataProvider[market.chain],
abi: AaveV3Pool.find((m) => m.name === 'getReserveData'),
params: [market.debt_token.address],
chain: market.chain,
});
apyBase = getReserveData.liquidityRate / 10 ** 25;
}
return {
pool: market.id,
chain: uppercaseFirst(market.chain),
project: 'size-credit',
symbol: market.base_symbol,
tvlUsd: marketsLiquidity[market.id].buy_side_liquidity_usd,
apyBase,
underlyingTokens: [market.debt_token.address],
url: `https://app.size.credit/borrow?action=market&type=lend&market_id=${market.id}`,
apyBaseBorrow: await borrowingAPR(
market,
TENOR_DAYS * DAYS_TO_SECONDS,
DEPTH_BORROW_TOKEN
),
totalSupplyUsd: tvl.debt_tvl_usd,
totalBorrowUsd: tvl.total_borrow_usd,
ltv: 1e18 / market.risk_config.cr_liquidation,
poolMeta: `${TENOR_DAYS}-day maturity`,
};
})
);
}

module.exports = {
apy,
};
19 changes: 19 additions & 0 deletions src/types/Pool.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
export interface Pool {
pool: string;
chain: string;
project: string;
symbol: string;
tvlUsd: number; // for lending protocols: tvlUsd = totalSupplyUsd - totalBorrowUsd
apyBase?: number;
apyReward?: number;
rewardTokens?: Array<string>;
underlyingTokens?: Array<string>;
poolMeta?: string;
url?: string;
// optional lending protocol specific fields:
apyBaseBorrow?: number;
apyRewardBorrow?: number;
totalSupplyUsd?: number;
totalBorrowUsd?: number;
ltv?: number; // btw [0, 1]
}
Loading