Skip to content

Commit 38a9fad

Browse files
Create index.ts
1 parent f35eb4e commit 38a9fad

File tree

1 file changed

+141
-0
lines changed

1 file changed

+141
-0
lines changed

src/adaptors/looped-hype/index.ts

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
2+
const axios = require('axios');
3+
const utils = require('../utils');
4+
const sdk = require('@defillama/sdk');
5+
6+
const poolAbi = require('./poolAbi.json');
7+
const accountantAbi = require('./accountantAbi.json');
8+
9+
const LHYPE = '0x5748ae796AE46A4F1348a1693de4b50560485562';
10+
const LHYPE_ACCOUNTANT = '0xcE621a3CA6F72706678cFF0572ae8d15e5F001c3';
11+
const UNDERLYING = '0x5555555555555555555555555555555555555555'; // WHYPE on Hyperliquid
12+
const CHAIN = 'hyperliquid';
13+
14+
/* ---------------------------------
15+
TVL Calculation Section
16+
----------------------------------*/
17+
const calculateTVL = async (chain = CHAIN) => {
18+
const totalSupplyCall = sdk.api.abi.call({
19+
target: LHYPE,
20+
abi: poolAbi.find((m) => m.name === 'totalSupply'),
21+
chain,
22+
});
23+
24+
const decimalsCall = sdk.api.abi.call({
25+
target: LHYPE,
26+
abi: poolAbi.find((m) => m.name === 'decimals'),
27+
chain,
28+
});
29+
30+
const priceKey = `${chain}:${UNDERLYING}`;
31+
const underlyingPriceCall = axios.get(
32+
`https://coins.llama.fi/prices/current/${priceKey}?searchWidth=24h`
33+
);
34+
35+
const currentRateCall = sdk.api.abi.call({
36+
target: LHYPE_ACCOUNTANT,
37+
abi: accountantAbi.find((m) => m.name === 'getRate'),
38+
chain,
39+
});
40+
41+
const [
42+
totalSupplyResponse,
43+
decimalsResponse,
44+
underlyingPriceResponse,
45+
currentRateResponse,
46+
] = await Promise.all([
47+
totalSupplyCall,
48+
decimalsCall,
49+
underlyingPriceCall,
50+
currentRateCall,
51+
]);
52+
53+
const decimals = Number(decimalsResponse.output);
54+
const scalingFactor = 10 ** decimals;
55+
56+
const totalSupply = Number(totalSupplyResponse.output) / scalingFactor;
57+
const currentRate = Number(currentRateResponse.output);
58+
59+
const priceObj = underlyingPriceResponse.data?.coins?.[priceKey];
60+
if (!priceObj || typeof priceObj.price !== 'number') {
61+
throw new Error(`No price found for ${priceKey}`);
62+
}
63+
const underlyingPrice = priceObj.price;
64+
const tvlUsd = totalSupply * currentRate * underlyingPrice / scalingFactor;
65+
66+
return {
67+
tvlUsd,
68+
underlyingPrice,
69+
decimals,
70+
scalingFactor,
71+
totalSupply,
72+
currentRate,
73+
};
74+
};
75+
76+
/* ---------------------------------
77+
APY Calculation Section
78+
----------------------------------*/
79+
const calculateAPY = async (currentRate, scalingFactor, chain = CHAIN) => {
80+
const now = Math.floor(Date.now() / 1000);
81+
const t1d = now - 86400;
82+
const t7d = now - 86400 * 7;
83+
84+
const [b1, b7] = await Promise.all([
85+
axios.get(`https://coins.llama.fi/block/${chain}/${t1d}`),
86+
axios.get(`https://coins.llama.fi/block/${chain}/${t7d}`),
87+
]);
88+
89+
const block1d = b1.data?.height;
90+
const block7d = b7.data?.height;
91+
92+
const [r1, r7] = await Promise.all([
93+
sdk.api.abi.call({
94+
target: LHYPE_ACCOUNTANT,
95+
abi: accountantAbi.find((m) => m.name === 'getRate'),
96+
block: block1d,
97+
chain,
98+
}),
99+
sdk.api.abi.call({
100+
target: LHYPE_ACCOUNTANT,
101+
abi: accountantAbi.find((m) => m.name === 'getRate'),
102+
block: block7d,
103+
chain,
104+
}),
105+
]);
106+
107+
const apy1d = ((Number(currentRate) - Number(r1.output)) / scalingFactor) * 365 * 100;
108+
const apy7d = ((Number(currentRate) - Number(r7.output)) / scalingFactor / 7) * 365 * 100;
109+
110+
return {
111+
apy1d,
112+
apy7d,
113+
};
114+
};
115+
116+
/* ---------------------------------
117+
Adapter Export
118+
----------------------------------*/
119+
const apy = async () => {
120+
const { tvlUsd, currentRate, scalingFactor } = await calculateTVL(CHAIN);
121+
const { apy1d, apy7d } = await calculateAPY(currentRate, scalingFactor, CHAIN);
122+
123+
const pool = {
124+
pool: `${LHYPE}-${CHAIN}`.toLowerCase(),
125+
project: 'looped-hype',
126+
chain: utils.formatChain(CHAIN),
127+
symbol: 'LHYPE',
128+
tvlUsd,
129+
apyBase: apy1d,
130+
apyBase7d: apy7d,
131+
underlyingTokens: [UNDERLYING],
132+
};
133+
134+
return [pool];
135+
};
136+
137+
module.exports = {
138+
apy,
139+
timetravel: false,
140+
url: 'https://app.loopingcollective.org/product/lhype',
141+
};

0 commit comments

Comments
 (0)