Skip to content

Commit 33b2ef6

Browse files
refac: refactor ekubo store
1 parent 7bbf980 commit 33b2ef6

File tree

1 file changed

+78
-124
lines changed

1 file changed

+78
-124
lines changed

src/store/ekobu.store.ts

Lines changed: 78 additions & 124 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,7 @@ import fetchWithRetry from '@/utils/fetchWithRetry';
55
import { atom } from 'jotai';
66
import { atomWithQuery, AtomWithQueryResult } from 'jotai-tanstack-query';
77
import { IDapp } from './IDapp.store';
8-
import {
9-
APRSplit,
10-
Category,
11-
PoolInfo,
12-
PoolMetadata,
13-
PoolType,
14-
ProtocolAtoms,
15-
StrkDexIncentivesAtom,
16-
} from './pools';
8+
import { Category, PoolInfo, PoolType, ProtocolAtoms } from './pools';
179

1810
interface EkuboBaseAprDoc {
1911
tokens: Token[];
@@ -81,82 +73,88 @@ export class Ekubo extends IDapp<EkuboBaseAprDoc> {
8173
name = 'Ekubo';
8274
link = 'https://app.ekubo.org/positions';
8375
logo = 'https://app.ekubo.org/favicon.ico';
84-
8576
incentiveDataKey = 'Ekubo';
77+
supportedPools = [
78+
'ETH/USDC',
79+
'STRK/USDC',
80+
'STRK/ETH',
81+
'kSTRK/STRK',
82+
'USDC/USDT',
83+
'USDC',
84+
'USDT',
85+
'ETH',
86+
'STRK',
87+
];
8688

8789
_computePoolsInfo(data: any) {
8890
try {
89-
const myData = data[0][this.incentiveDataKey];
90-
const baseInfo = data[1];
91-
if (!myData) return [];
91+
const poolsInfo = data;
92+
if (!poolsInfo) return [];
93+
94+
const poolData = this.calcBaseAPY(poolsInfo);
9295
const pools: PoolInfo[] = [];
9396

94-
Object.keys(myData)
95-
.filter(this.commonVaultFilter)
96-
.forEach((poolName) => {
97-
const arr = myData[poolName];
98-
let category = Category.Others;
99-
let riskFactor = 3;
100-
if (poolName === 'USDC/USDT') {
101-
category = Category.Stable;
102-
riskFactor = 0.5;
103-
} else if (poolName.includes('STRK')) {
104-
category = Category.STRK;
105-
}
97+
poolData?.forEach((pool) => {
98+
if (!this.supportedPools.includes(pool.pool)) return;
10699

107-
const tokens: TokenName[] = <TokenName[]>poolName.split('/');
108-
const logo1 = CONSTANTS.LOGOS[tokens[0]];
109-
const logo2 = CONSTANTS.LOGOS[tokens[1]];
100+
console.log('ekubo', pool);
110101

111-
const poolInfo: PoolInfo = {
112-
pool: {
113-
id: this.getPoolId(this.name, poolName),
114-
name: poolName,
115-
logos: [logo1, logo2],
116-
},
117-
protocol: {
118-
name: this.name,
119-
link: this.link,
120-
logo: this.logo,
121-
},
122-
apr: arr[arr.length - 1].apr,
123-
tvl: arr[arr.length - 1].tvl_usd,
124-
aprSplits: [
125-
{
126-
apr: arr[arr.length - 1].apr,
127-
title: 'STRK rewards',
128-
description: 'Starknet DeFi Spring incentives',
129-
},
130-
],
131-
category,
132-
type: PoolType.DEXV3,
133-
lending: {
134-
collateralFactor: 0,
135-
},
136-
borrow: {
137-
borrowFactor: 0,
138-
apr: 0,
102+
let category = Category.Others;
103+
let riskFactor = 3;
104+
if (pool.pool === 'USDC/USDT') {
105+
category = Category.Stable;
106+
riskFactor = 0.5;
107+
} else if (pool.pool.includes('STRK')) {
108+
category = Category.STRK;
109+
}
110+
111+
const tokens: TokenName[] = <TokenName[]>pool.pool.split('/');
112+
const logo1 = CONSTANTS.LOGOS[tokens[0]];
113+
const logo2 = CONSTANTS.LOGOS[tokens[1]];
114+
115+
const poolInfo: PoolInfo = {
116+
pool: {
117+
id: this.getPoolId(this.name, pool.pool),
118+
name: pool.pool,
119+
logos: [logo1, logo2],
120+
},
121+
protocol: {
122+
name: this.name,
123+
link: this.link,
124+
logo: this.logo,
125+
},
126+
apr: pool.apyBase + pool.apyReward,
127+
tvl: pool.tvlUsd,
128+
aprSplits: [
129+
{
130+
apr: pool.apyBase,
131+
title: 'Base APR',
132+
description: '',
139133
},
140-
additional: {
141-
tags: [StrategyLiveStatus.ACTIVE],
142-
riskFactor,
143-
isAudited: false, // TODO: Update this
134+
{
135+
apr: pool?.apyReward,
136+
title: 'STRK rewards',
137+
description: 'Starknet DeFi Spring incentives',
144138
},
145-
};
146-
147-
const { rewardAPY } = this.getBaseAPY(poolInfo, baseInfo);
148-
if (rewardAPY) {
149-
poolInfo.apr = rewardAPY;
150-
poolInfo.aprSplits = [
151-
{
152-
apr: rewardAPY,
153-
title: 'STRK rewards',
154-
description: 'Starknet DeFi Spring incentives',
155-
},
156-
];
157-
}
158-
pools.push(poolInfo);
159-
});
139+
],
140+
category,
141+
type: PoolType.DEXV3,
142+
lending: {
143+
collateralFactor: 0,
144+
},
145+
borrow: {
146+
borrowFactor: 0,
147+
apr: 0,
148+
},
149+
additional: {
150+
tags: [StrategyLiveStatus.ACTIVE],
151+
riskFactor,
152+
isAudited: false, // TODO: Update this
153+
},
154+
};
155+
156+
pools.push(poolInfo);
157+
});
160158

161159
return pools;
162160
} catch (err) {
@@ -165,32 +163,8 @@ export class Ekubo extends IDapp<EkuboBaseAprDoc> {
165163
}
166164
}
167165

168-
commonVaultFilter(poolName: string) {
169-
const supportedPools = [
170-
'ETH/USDC',
171-
'STRK/USDC',
172-
'STRK/ETH',
173-
'kSTRK/STRK',
174-
'USDC/USDT',
175-
'USDC',
176-
'USDT',
177-
'ETH',
178-
'STRK',
179-
'kSTRK',
180-
];
181-
console.log('filter2', poolName, supportedPools.includes(poolName));
182-
return supportedPools.includes(poolName);
183-
}
184-
185-
getBaseAPY(p: PoolInfo, data: AtomWithQueryResult<EkuboBaseAprDoc, Error>) {
186-
let rewardAPY: number = 0;
187-
let baseAPY: number | 'Err' = 'Err';
188-
let splitApr: APRSplit | null = null;
189-
const metadata: PoolMetadata | null = null;
190-
166+
calcBaseAPY(data: AtomWithQueryResult<EkuboBaseAprDoc, Error>) {
191167
if (data.isSuccess) {
192-
const poolName = p.pool.name;
193-
194168
const {
195169
tokens,
196170
defiSpringData,
@@ -215,7 +189,7 @@ export class Ekubo extends IDapp<EkuboBaseAprDoc> {
215189
}
216190
};
217191

218-
const pools = pairData.topPairs
192+
return pairData.topPairs
219193
.map((p) => {
220194
const t0 = BigInt(p.token0);
221195
const t1 = BigInt(p.token1);
@@ -247,7 +221,7 @@ export class Ekubo extends IDapp<EkuboBaseAprDoc> {
247221
Math.pow(10, token1.decimals);
248222

249223
const apyBase = (feesUsd * 365) / tvlUsd;
250-
const apyReward = springPair ? springPair.currentApr : undefined;
224+
const apyReward = springPair ? springPair.currentApr : 0;
251225

252226
return {
253227
pool: `${token0.symbol}/${token1.symbol}`,
@@ -265,25 +239,7 @@ export class Ekubo extends IDapp<EkuboBaseAprDoc> {
265239
})
266240
.filter((p) => !!p)
267241
.sort((a, b) => b.tvlUsd - a.tvlUsd);
268-
269-
const pool = pools.find((p) => p.pool === poolName);
270-
271-
baseAPY = pool ? pool.apyBase : 0;
272-
rewardAPY = pool && pool.apyReward ? pool.apyReward : 0;
273-
274-
splitApr = {
275-
apr: baseAPY,
276-
title: 'Base APR',
277-
description: 'Subject to position range',
278-
};
279242
}
280-
281-
return {
282-
baseAPY,
283-
rewardAPY,
284-
splitApr,
285-
metadata,
286-
};
287243
}
288244

289245
async getData(): Promise<EkuboBaseAprDoc> {
@@ -344,13 +300,11 @@ const EkuboAtoms: ProtocolAtoms = {
344300
},
345301
})),
346302
pools: atom((get) => {
347-
const poolsInfo = get(StrkDexIncentivesAtom);
348303
const empty: PoolInfo[] = [];
349304
if (!EkuboAtoms.baseAPRs) return empty;
350-
const baseInfo = get(EkuboAtoms.baseAPRs);
305+
const poolsInfo = get(EkuboAtoms.baseAPRs);
351306
if (poolsInfo.data) {
352-
const pools = ekubo._computePoolsInfo([poolsInfo.data, baseInfo]);
353-
return ekubo.addBaseAPYs(pools, baseInfo);
307+
return ekubo._computePoolsInfo(poolsInfo);
354308
}
355309

356310
return empty;

0 commit comments

Comments
 (0)