Skip to content

Commit

Permalink
optimization: load strkfarm apy from api
Browse files Browse the repository at this point in the history
  • Loading branch information
akiraonstarknet committed Sep 30, 2024
1 parent 3c9a7f3 commit 75490a4
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 22 deletions.
16 changes: 13 additions & 3 deletions src/app/api/strategies/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import ZkLendAtoms from '@/store/zklend.store';
import { PoolInfo } from '@/store/pools';
import NostraLendingAtoms from '@/store/nostralending.store';
import { RpcProvider } from 'starknet';
import { getStrategies } from '@/store/strategies.atoms';
import { getLiveStatusNumber, getStrategies } from '@/store/strategies.atoms';
import { MY_STORE } from '@/store';
import MyNumber from '@/utils/MyNumber';
import { IStrategy, NFTInfo, TokenInfo } from '@/strategies/IStrategy';
Expand Down Expand Up @@ -42,15 +42,25 @@ async function getStrategyInfo(strategy: IStrategy) {
id: strategy.id,
apy: strategy.netYield,
depositToken: strategy
.depositMethods(MyNumber.fromZero(), '', provider)
.depositMethods({
amount: MyNumber.fromZero(),
address: '',
provider,
isMax: false,
})
.map((t) => t.tokenInfo.token),
leverage: strategy.leverage,
contract: strategy.holdingTokens.map((t) => ({
name: t.name,
address: (<any>t).token ? (<TokenInfo>t).token : (<NFTInfo>t).address,
})),
tvlUsd: tvl.usdValue || 0,
status: strategy.liveStatus,
status: {
number: getLiveStatusNumber(strategy.liveStatus),
value: strategy.liveStatus,
},
riskFactor: strategy.riskFactor,
logo: strategy.holdingTokens[0].logo,
};
}

Expand Down
5 changes: 5 additions & 0 deletions src/app/strategy/[strategyId]/_components/Strategy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -440,6 +440,11 @@ const Strategy = ({ params }: StrategyParams) => {
</Text>
</Box>
))}
{strategy.actions.length == 0 && (
<Center width={'100%'} padding={'10px'}>
<Spinner size={'xs'} color="white" />
</Center>
)}
</Card>
<Grid width={'100%'} templateColumns="repeat(5, 1fr)" gap={2}>
<GridItem colSpan={colSpan1} bg="highlight">
Expand Down
29 changes: 27 additions & 2 deletions src/components/HarvestTime.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { useMemo } from 'react';
import {
Box,
Flex,
Spinner,
Stat,
StatLabel,
StatNumber,
Expand All @@ -15,6 +16,9 @@ import { HarvestTimeAtom } from '@/store/harvest.atom';
import { useAtomValue } from 'jotai';
import { formatTimediff, getDisplayCurrencyAmount, timeAgo } from '@/utils';
import { isMobile } from 'react-device-detect';
import STRKFarmAtoms, {
STRKFarmStrategyAPIResult,
} from '@/store/strkfarm.atoms';

interface HarvestTimeProps {
strategy: StrategyInfo;
Expand Down Expand Up @@ -70,6 +74,24 @@ const HarvestTime: React.FC<HarvestTimeProps> = ({ strategy, balData }) => {
return formatTimediff(nextHarvest);
}, [data?.timestamp, lastHarvest]);

const strategiesInfo = useAtomValue(STRKFarmAtoms.baseAPRs!);

const strategyInfo = useMemo(() => {
if (!strategiesInfo || !strategiesInfo.data) return null;

const strategiesList: STRKFarmStrategyAPIResult[] =
strategiesInfo.data.strategies;
const strategyInfo = strategiesList.find(
(strat) => strat.id == strategy.id,
);
return strategyInfo ? strategyInfo : null;
}, [strategiesInfo]);

const leverage = useMemo(() => {
if (!strategyInfo) return 0;
return strategyInfo.leverage || 0;
}, [strategyInfo]);

return (
<Box>
<Flex justifyContent="space-between">
Expand All @@ -82,7 +104,7 @@ const HarvestTime: React.FC<HarvestTimeProps> = ({ strategy, balData }) => {
>
<StatLabel>APY</StatLabel>
<StatNumber color="cyan" lineHeight="24px">
{(strategy.netYield * 100).toFixed(2)}%
{((strategyInfo?.apy || 0) * 100).toFixed(2)}%
</StatNumber>
</Stat>
<Flex flexDirection={'column'} justifyContent={'flex-end'}>
Expand All @@ -93,7 +115,10 @@ const HarvestTime: React.FC<HarvestTimeProps> = ({ strategy, balData }) => {
fontSize={'12px'}
padding={'2px 5px'}
>
🔥{strategy.leverage.toFixed(2)}x boosted
🔥{leverage.toFixed(2)}x boosted
{leverage == 0 && (
<Spinner size="xs" color="white" ml={'5px'} />
)}
</Tag>
</Tooltip>
</Flex>
Expand Down
3 changes: 3 additions & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ const CONSTANTS = {
HAIKO: {
BASE_APR_API: 'haiko/markets?network=mainnet',
},
STRKFarm: {
BASE_APR_API: '/api/strategies',
},
MY_SWAP: {
POOLS_API: '/myswap/data/pools/all.json',
BASE_APR_API: '/myswap/data/pools',
Expand Down
20 changes: 10 additions & 10 deletions src/store/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@ import { Category, PoolInfo, PoolType } from './pools';
import { strategiesAtom } from './strategies.atoms';
import strkfarmLogo from '@public/logo.png';
import { IStrategyProps } from '@/strategies/IStrategy';
import STRKFarmAtoms, { strkfarm } from './strkfarm.atoms';

export const PROTOCOLS = [
{
name: strkfarm.name,
class: strkfarm,
atoms: STRKFarmAtoms,
},
{
name: ekubo.name,
class: ekubo,
Expand Down Expand Up @@ -98,16 +104,10 @@ export const PROTOCOLS = [

export const ALL_FILTER = 'All';

const allProtocols = [
{
name: 'STRKFarm',
logo: strkfarmLogo.src,
},
...PROTOCOLS.map((p) => ({
name: p.name,
logo: p.class.logo,
})),
];
const allProtocols = PROTOCOLS.map((p) => ({
name: p.name,
logo: p.class.logo,
}));
export const filters = {
categories: [...Object.values(Category)],
types: [...Object.values(PoolType)],
Expand Down
13 changes: 12 additions & 1 deletion src/store/strategies.atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ export const strategiesAtom = atom<StrategyInfo[]>((get) => {
return strategies;
});

function getLiveStatusNumber(status: StrategyLiveStatus) {
export function getLiveStatusNumber(status: StrategyLiveStatus) {
if (status == StrategyLiveStatus.NEW) {
return 1;
} else if (status == StrategyLiveStatus.ACTIVE) {
Expand All @@ -120,3 +120,14 @@ function getLiveStatusNumber(status: StrategyLiveStatus) {
}
return 4;
}

export function getLiveStatusEnum(status: number) {
if (status == 1) {
return StrategyLiveStatus.NEW;
} else if (status == 2) {
return StrategyLiveStatus.ACTIVE;
} else if (status == 3) {
return StrategyLiveStatus.COMING_SOON;
}
return StrategyLiveStatus.RETIRED;
}
16 changes: 10 additions & 6 deletions src/store/strkfarm.atoms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { AtomWithQueryResult, atomWithQuery } from 'jotai-tanstack-query';
import strkfarmLogo from '@public/logo.png';
import { getLiveStatusEnum } from './strategies.atoms';

interface MyBaseAprDoc {
export interface STRKFarmStrategyAPIResult {
name: string;
id: string;
apy: number;
Expand All @@ -29,19 +29,19 @@ interface MyBaseAprDoc {
logo: string;
}

export class STRKFarm extends IDapp<MyBaseAprDoc> {
export class STRKFarm extends IDapp<STRKFarmStrategyAPIResult> {
name = 'STRKFarm';
link = strkfarmLogo.src;
logo = 'https://app.jediswap.xyz/favicon/favicon-32x32.png';
incentiveDataKey = 'Jediswap_v1';

_computePoolsInfo(data: any) {
const rawPools: MyBaseAprDoc[] = data.strategies;
const rawPools: STRKFarmStrategyAPIResult[] = data.strategies;
const pools: PoolInfo[] = [];
return rawPools.map((rawPool) => {
let category = Category.Others;
const poolName = rawPool.name;
const riskFactor = 0.75; // todo
const riskFactor = rawPool.riskFactor;
if (poolName.includes('USDC') || poolName.includes('USDT')) {
category = Category.Stable;
} else if (poolName.includes('STRK')) {
Expand Down Expand Up @@ -81,7 +81,7 @@ export class STRKFarm extends IDapp<MyBaseAprDoc> {
}

getBaseAPY(p: PoolInfo, data: AtomWithQueryResult<any, Error>) {
const aprData: MyBaseAprDoc[] = data.data.strategies;
const aprData: STRKFarmStrategyAPIResult[] = data.data.strategies;
let baseAPY: number | 'Err' = 'Err';
let splitApr: APRSplit | null = null;
const metadata: PoolMetadata | null = null;
Expand All @@ -108,7 +108,11 @@ export const strkfarm = new STRKFarm();
const STRKFarmAtoms: ProtocolAtoms = {
baseAPRs: atomWithQuery((get) => ({
queryKey: ['strkfarm_base_aprs'],
queryFn: async ({ queryKey }) => {
queryFn: async ({
queryKey,
}): Promise<{
strategies: STRKFarmStrategyAPIResult[];
}> => {
const response = await fetch(`${CONSTANTS.STRKFarm.BASE_APR_API}`);
const data = await response.json();
return data;
Expand Down

0 comments on commit 75490a4

Please sign in to comment.