Skip to content

Commit

Permalink
Use js instead of ts
Browse files Browse the repository at this point in the history
  • Loading branch information
aviggiano committed Jan 10, 2025
1 parent d2e11fd commit 4208984
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 47 deletions.
77 changes: 49 additions & 28 deletions src/adaptors/size-credit/api.ts → src/adaptors/size-credit/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type Fetch from 'node-fetch';
const fetch = require('node-fetch') as typeof Fetch;
const fetch = require('node-fetch');

/*
export interface Market {
id: string;
name: string;
Expand Down Expand Up @@ -47,7 +47,7 @@ export interface Market {
}
interface GetMarketsResponse {
available_markets: Market[];
markets_by_chain: Record<string, Market[]>
}
interface GetTvlResponse {
Expand Down Expand Up @@ -83,50 +83,71 @@ interface GetBestRateResponse {
depth_used_collateral_token: number;
}[][]
}
*/

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

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

export async function getTvl(market: Market): Promise<GetTvlResponse> {
const getTvlResponse: GetTvlResponse = await fetch(`${ENDPOINT}${market.api_base_url}/market-tvl`).then((res) => res.json());
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;
}

export 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());
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;
}

export 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());
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;
}

export type GetMarkets = typeof getMarkets;
export type GetTvl = typeof getTvl;
export type LendingAPR = typeof lendingAPR;
export type BorrowingAPR = typeof borrowingAPR;
module.exports = {
getMarkets,
getTvl,
lendingAPR,
borrowingAPR,
};
Original file line number Diff line number Diff line change
@@ -1,11 +1,4 @@
import type { Pool } from "../../types/Pool";
import type { BorrowingAPR, GetMarkets, LendingAPR, GetTvl } from "./api";
const { borrowingAPR, getMarkets, lendingAPR, getTvl } = require('./api') as {
borrowingAPR: BorrowingAPR;
getMarkets: GetMarkets;
lendingAPR: LendingAPR;
getTvl: GetTvl;
};
const { borrowingAPR, getMarkets, lendingAPR, getTvl } = require('./api');
const sdk = require('@defillama/sdk');
const AaveV3Pool = require('../aave-v3/poolAbi');

Expand All @@ -26,29 +19,30 @@ const AaveProtocolDataProvider = {
etherfi: '0xE7d490885A68f00d9886508DF281D67263ed5758', // on ethereum
};

const TENOR_SECONDS = 60 * 60 * 24 * 3
const TENOR_SECONDS = 60 * 60 * 24 * 3;

const DEPTH_BORROW_TOKEN = 10;

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

export async function apy(): Promise<Pool[]> {
async function apy() /*: Promise<Pool[]>*/ {
const markets = await getMarkets();

return Promise.all(
markets.map(async (market) => {
const tvl = await getTvl(market);
let apyBase = await lendingAPR(market, TENOR_SECONDS, DEPTH_BORROW_TOKEN)
if (apyBase === undefined) { // no limit borrow offers available, use Aave v3 as a variable-rate lending pool
let apyBase = await lendingAPR(market, TENOR_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
});
apyBase = getReserveData.liquidityRate / 10 ** 25;
}
return {
pool: market.id,
Expand All @@ -61,17 +55,20 @@ export async function apy(): Promise<Pool[]> {
underlyingTokens: [market.debt_token.address],
rewardTokens: [],
url: `https://app.size.credit/borrow?action=market&type=lend&market_id=${market.id}`,
apyBaseBorrow: await borrowingAPR(market, TENOR_SECONDS, DEPTH_BORROW_TOKEN),
apyBaseBorrow: await borrowingAPR(
market,
TENOR_SECONDS,
DEPTH_BORROW_TOKEN
),
apyRewardBorrow: 0,
totalSupplyUsd: tvl.debt_tvl_usd,
totalBorrowUsd: tvl.borrow_tvl_usd,
ltv: 1e20 / market.risk_config.cr_liquidation,
};
}),
})
);
};
}

module.exports = {
timetravel: false,
apy,
};

0 comments on commit 4208984

Please sign in to comment.