Skip to content

Commit e9982e5

Browse files
authored
Merge pull request #1801 from bob-collective/bob-avalon-finance
add Avalon on BOB
2 parents e170cff + aba7bce commit e9982e5

File tree

1 file changed

+157
-0
lines changed

1 file changed

+157
-0
lines changed

src/adaptors/avalon-finance/index.js

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
const axios = require('axios');
2+
const sdk = require('@defillama/sdk');
3+
4+
const utils = require('../utils');
5+
const poolAbi = require('../aave-v3/poolAbi');
6+
7+
const protocolDataProviders = {
8+
bob: '0xfabb0fDca4348d5A40EB1BB74AEa86A1C4eAd7E2',
9+
};
10+
11+
const getApy = async (market) => {
12+
const chain = market;
13+
14+
const protocolDataProvider = protocolDataProviders[market];
15+
const reserveTokens = (
16+
await sdk.api.abi.call({
17+
target: protocolDataProvider,
18+
abi: poolAbi.find((m) => m.name === 'getAllReservesTokens'),
19+
chain,
20+
})
21+
).output;
22+
23+
const aTokens = (
24+
await sdk.api.abi.call({
25+
target: protocolDataProvider,
26+
abi: poolAbi.find((m) => m.name === 'getAllATokens'),
27+
chain,
28+
})
29+
).output;
30+
31+
const poolsReserveData = (
32+
await sdk.api.abi.multiCall({
33+
calls: reserveTokens.map((p) => ({
34+
target: protocolDataProvider,
35+
params: p.tokenAddress,
36+
})),
37+
abi: poolAbi.find((m) => m.name === 'getReserveData'),
38+
chain,
39+
})
40+
).output.map((o) => o.output);
41+
42+
const poolsReservesConfigurationData = (
43+
await sdk.api.abi.multiCall({
44+
calls: reserveTokens.map((p) => ({
45+
target: protocolDataProvider,
46+
params: p.tokenAddress,
47+
})),
48+
abi: poolAbi.find((m) => m.name === 'getReserveConfigurationData'),
49+
chain,
50+
})
51+
).output.map((o) => o.output);
52+
53+
const totalSupply = (
54+
await sdk.api.abi.multiCall({
55+
chain,
56+
abi: 'erc20:totalSupply',
57+
calls: aTokens.map((t) => ({
58+
target: t.tokenAddress,
59+
})),
60+
})
61+
).output.map((o) => o.output);
62+
63+
const underlyingBalances = (
64+
await sdk.api.abi.multiCall({
65+
chain,
66+
abi: 'erc20:balanceOf',
67+
calls: aTokens.map((t, i) => ({
68+
target: reserveTokens[i].tokenAddress,
69+
params: [t.tokenAddress],
70+
})),
71+
})
72+
).output.map((o) => o.output);
73+
74+
const underlyingDecimals = (
75+
await sdk.api.abi.multiCall({
76+
chain,
77+
abi: 'erc20:decimals',
78+
calls: aTokens.map((t) => ({
79+
target: t.tokenAddress,
80+
})),
81+
})
82+
).output.map((o) => o.output);
83+
84+
const priceKeys = reserveTokens
85+
.map((t) => `${chain}:${t.tokenAddress}`)
86+
.join(',');
87+
const prices = (
88+
await axios.get(`https://coins.llama.fi/prices/current/${priceKeys}`)
89+
).data.coins;
90+
91+
92+
return reserveTokens
93+
.map((pool, i) => {
94+
const frozen = poolsReservesConfigurationData[i].isFrozen;
95+
if (frozen) return null;
96+
97+
const p = poolsReserveData[i];
98+
const price = prices[`${chain}:${pool.tokenAddress}`]?.price;
99+
100+
const supply = totalSupply[i];
101+
let totalSupplyUsd = (supply / 10 ** underlyingDecimals[i]) * price;
102+
103+
const currentSupply = underlyingBalances[i];
104+
let tvlUsd = (currentSupply / 10 ** underlyingDecimals[i]) * price;
105+
106+
const totalBorrowUsd = totalSupplyUsd - tvlUsd;
107+
108+
const marketUrlParam =
109+
market === 'ethereum'
110+
? 'mainnet'
111+
: market === 'avax'
112+
? 'avalanche'
113+
: market === 'xdai'
114+
? 'gnosis'
115+
: market === 'bsc'
116+
? 'bnb'
117+
: market;
118+
119+
const url = `https:/lend.avalonfinance.xyz/reserve-overview/?underlyingAsset=${pool.tokenAddress.toLowerCase()}&marketName=proto_${marketUrlParam}_v3`;
120+
121+
return {
122+
pool: `${aTokens[i].tokenAddress}-${
123+
market === 'avax' ? 'avalanche' : market
124+
}`.toLowerCase(),
125+
chain,
126+
project: 'avalon-finance',
127+
symbol: pool.symbol,
128+
tvlUsd,
129+
apyBase: (p.liquidityRate / 10 ** 27) * 100,
130+
underlyingTokens: [pool.tokenAddress],
131+
totalSupplyUsd,
132+
totalBorrowUsd,
133+
apyBaseBorrow: Number(p.variableBorrowRate) / 1e25,
134+
ltv: poolsReservesConfigurationData[i].ltv / 10000,
135+
url,
136+
borrowable: poolsReservesConfigurationData[i].borrowingEnabled,
137+
};
138+
})
139+
.filter((i) => Boolean(i));
140+
};
141+
142+
143+
const apy = async () => {
144+
const pools = await Promise.allSettled(
145+
Object.keys(protocolDataProviders).map(async (market) => getApy(market))
146+
);
147+
148+
return pools
149+
.filter((i) => i.status === 'fulfilled')
150+
.map((i) => i.value)
151+
.flat()
152+
.filter((p) => utils.keepFinite(p));
153+
};
154+
155+
module.exports = {
156+
apy,
157+
};

0 commit comments

Comments
 (0)