Skip to content

Commit 012d45d

Browse files
authored
Merge pull request #129 from cosmology-tech/refactor/folder-and-ui
refactor provide-liquidity example
2 parents a58a293 + 367ad8c commit 012d45d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1103
-1055
lines changed

examples/provide-liquidity/api/apr.ts

Lines changed: 0 additions & 19 deletions
This file was deleted.

examples/provide-liquidity/api/fees.ts

Lines changed: 0 additions & 10 deletions
This file was deleted.

examples/provide-liquidity/api/index.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

examples/provide-liquidity/api/prices.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

examples/provide-liquidity/api/rewards.ts

Lines changed: 0 additions & 23 deletions
This file was deleted.

examples/provide-liquidity/api/tokens.ts

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
export * from './types';
21
export * from './pools';
32
export * from './wallet';
43
export * from './common';

examples/provide-liquidity/components/pools/PoolCard.tsx

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,18 @@
11
import React from 'react';
22
import {
33
Box,
4-
Divider,
54
Flex,
65
Text,
76
Image,
87
Skeleton,
98
useMediaQuery,
109
useColorModeValue,
1110
} from '@chakra-ui/react';
12-
import { Pool } from './ProvideLiquidity';
1311
import { getLogoUrlFromDenom } from './PoolList';
1412
import BigNumber from 'bignumber.js';
1513
import { truncDecimals } from './modals/PoolDetailModal';
16-
import { getSymbolForDenom } from '../../utils';
14+
import { getSymbolForDenom, ExtendedPool } from '@/utils';
15+
import { PoolApr } from '@/hooks';
1716

1817
const formatNumber = (number: number | string) => {
1918
const formatter = Intl.NumberFormat('en', {
@@ -63,11 +62,13 @@ export const ChainLogo = ({
6362
const PoolCard = ({
6463
pool,
6564
setPool,
65+
poolApr,
6666
openPoolDetailModal,
6767
isFetchingApr,
6868
}: {
69-
pool: Pool;
70-
setPool: (pool: Pool) => void;
69+
pool: ExtendedPool;
70+
poolApr: PoolApr | undefined;
71+
setPool: (pool: ExtendedPool) => void;
7172
openPoolDetailModal: () => void;
7273
isFetchingApr: boolean;
7374
}) => {
@@ -125,7 +126,7 @@ const PoolCard = ({
125126
gap="2px"
126127
>
127128
<Text fontSize="22px" lineHeight="26px">
128-
{truncDecimals(pool.apr['14'].totalApr, 2)}
129+
{truncDecimals(poolApr?.['14'].totalApr, 2)}
129130
</Text>
130131
<Text fontSize="14px">%</Text>
131132
</Flex>
@@ -149,20 +150,6 @@ const PoolCard = ({
149150
<Text fontWeight="400">7D Fees</Text>
150151
<Text fontWeight="600">${pool.fees7D.toLocaleString()}</Text>
151152
</Flex>
152-
{/* <Divider mb="15px" /> */}
153-
{/* <Flex
154-
justifyContent="space-between"
155-
mb="6px"
156-
fontSize="14px"
157-
color="#2C3137"
158-
>
159-
<Text fontWeight="400">Your Liquidity</Text>
160-
<Text fontWeight="600">$1329.32</Text>
161-
</Flex>
162-
<Flex justifyContent="space-between" fontSize="14px" color="#2C3137">
163-
<Text fontWeight="400">Bonded</Text>
164-
<Text fontWeight="600">$600.00</Text>
165-
</Flex> */}
166153
</Box>
167154
);
168155
};

examples/provide-liquidity/components/pools/PoolList.tsx

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,14 @@ import {
2626
useColorModeValue,
2727
} from '@chakra-ui/react';
2828
import { SlOptionsVertical } from 'react-icons/sl';
29-
import { Pool } from './ProvideLiquidity';
3029
import BigNumber from 'bignumber.js';
3130
import { truncDecimals } from './modals/PoolDetailModal';
32-
import { getOsmoAssetByDenom, getSymbolForDenom } from '../../utils';
31+
import {
32+
getOsmoAssetByDenom,
33+
getSymbolForDenom,
34+
ExtendedPool,
35+
} from '../../utils';
36+
import { PoolsApr } from '@/hooks';
3337

3438
export const getLogoUrlFromDenom = (denom: string | undefined) => {
3539
if (!denom) return '';
@@ -72,7 +76,13 @@ export const ChainLogo = ({
7276
);
7377
};
7478

75-
const PoolName = ({ isMyPools, pool }: { isMyPools: boolean; pool: Pool }) => {
79+
const PoolName = ({
80+
isMyPools,
81+
pool,
82+
}: {
83+
isMyPools: boolean;
84+
pool: ExtendedPool;
85+
}) => {
7686
const myPoolsColor = useColorModeValue('#2C3137', '#EEF2F8');
7787
const allPoolsColor = useColorModeValue('#697584', '#A7B4C2');
7888
const poolIdColor = useColorModeValue('#697584', '#A7B4C2');
@@ -103,7 +113,7 @@ const ChainLogoGroup = ({
103113
style,
104114
logoWidth,
105115
}: {
106-
pool: Pool;
116+
pool: ExtendedPool;
107117
logoWidth: number;
108118
style?: React.CSSProperties;
109119
}) => {
@@ -174,7 +184,7 @@ const MenuPopover = ({
174184
handleRemoveLiquidityClick,
175185
handleViewDetailClick,
176186
}: {
177-
pool: Pool;
187+
pool: ExtendedPool;
178188
handleAddLiquidityClick: () => void;
179189
handleRemoveLiquidityClick: () => void;
180190
handleViewDetailClick: () => void;
@@ -257,12 +267,14 @@ const PoolList = ({
257267
openPoolDetailModal,
258268
isFetchingApr,
259269
openModals,
270+
poolsApr,
260271
}: {
261-
pools: Pool[];
272+
pools: ExtendedPool[];
262273
isMyPools?: boolean;
263-
setPool: (pool: Pool) => void;
274+
setPool: (pool: ExtendedPool) => void;
264275
openPoolDetailModal: () => void;
265276
isFetchingApr: boolean;
277+
poolsApr: PoolsApr | undefined;
266278
openModals: {
267279
onAddLiquidityOpen: () => void;
268280
onRemoveLiquidityOpen: () => void;
@@ -286,7 +298,7 @@ const PoolList = ({
286298
const hasMultiTokens = pools.some(({ poolAssets }) => poolAssets.length > 2);
287299
const [isMobile] = useMediaQuery('(max-width: 780px)');
288300

289-
const transformData = (isMyPools: boolean, pool: Pool) => {
301+
const transformData = (isMyPools: boolean, pool: ExtendedPool) => {
290302
const dataSource = isMyPools
291303
? [pool.myLiquidity, pool.bonded]
292304
: [pool.volume24H, pool.fees7D];
@@ -343,7 +355,12 @@ const PoolList = ({
343355
h="100%"
344356
>
345357
<PoolStat
346-
amount={truncDecimals(pool.apr['14'].totalApr, 2) + '%'}
358+
amount={
359+
truncDecimals(
360+
poolsApr?.[pool.denom]?.['14'].totalApr,
361+
2
362+
) + '%'
363+
}
347364
isMyPools={isMyPools}
348365
name="APR"
349366
/>
@@ -451,7 +468,10 @@ const PoolList = ({
451468
isMyPools ? myPoolsStatColor : allPoolsStatColor
452469
}
453470
>
454-
{truncDecimals(pool.apr['14'].totalApr, 2) + '%'}
471+
{truncDecimals(
472+
poolsApr?.[pool.denom]?.['14'].totalApr,
473+
2
474+
) + '%'}
455475
</Text>
456476
</Skeleton>
457477
</Td>

examples/provide-liquidity/components/pools/PoolsOverview.tsx

Lines changed: 21 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,13 @@ import {
88
Box,
99
useColorModeValue,
1010
} from '@chakra-ui/react';
11-
import { useChain, useManager } from '@cosmos-kit/react';
12-
import { EpochInfo } from 'osmojs/dist/codegen/osmosis/epochs/genesis';
13-
import { ReactElement, useCallback, useEffect, useRef, useState } from 'react';
14-
import { defaultChainName } from '../../config';
11+
import { useManager } from '@cosmos-kit/react';
12+
import { ReactElement, useEffect, useState } from 'react';
13+
import { coin, defaultChainName } from '@/config';
1514
import { RewardText } from './modals/ModalComponents';
1615
import dayjs from 'dayjs';
1716
import duration from 'dayjs/plugin/duration';
18-
import { osmosis } from 'osmojs';
17+
import { useEpochs, usePrices } from '@/hooks';
1918

2019
dayjs.extend(duration);
2120

@@ -51,56 +50,39 @@ const StatBox = ({
5150
};
5251

5352
export const PoolsOverview = ({
54-
osmoPrice,
55-
totalRewardPerDay,
53+
totalDayReward,
5654
}: {
57-
osmoPrice: string | number;
58-
totalRewardPerDay: number;
55+
totalDayReward: number;
5956
}) => {
6057
const [countdown, setCountdown] = useState(['00', '00', '00']);
61-
const isMountedRef = useRef(false);
58+
const { epochs, updateEpochs } = useEpochs();
59+
const { data: prices } = usePrices();
6260

63-
const { getRpcEndpoint } = useChain(defaultChainName);
6461
const { getChainLogo } = useManager();
65-
6662
const [isMobile] = useMediaQuery('(max-width: 660px)');
6763

68-
const getEpoch = useCallback(async () => {
69-
let rpcEndpoint = await getRpcEndpoint();
70-
71-
if (!rpcEndpoint) {
72-
console.log('no rpc endpoint — using a fallback');
73-
rpcEndpoint = `https://rpc.cosmos.directory/${defaultChainName}`;
74-
}
75-
76-
const client = await osmosis.ClientFactory.createRPCQueryClient({
77-
rpcEndpoint,
78-
});
79-
80-
const { epochs } = await client.osmosis.epochs.v1beta1.epochInfos();
81-
82-
const currentEpoch = epochs.find(
83-
(epoch) => epoch.identifier === 'day'
84-
) as EpochInfo;
64+
useEffect(() => {
65+
if (!epochs) return;
66+
const currentEpoch = epochs.find((epoch) => epoch.identifier === 'day')!;
8567

8668
const startTime = currentEpoch.currentEpochStartTime;
8769
const duration = Number(currentEpoch.duration?.seconds) || 60 * 60 * 24;
8870
const endTime = dayjs(startTime).add(duration, 'second');
8971

90-
const countdownInterval = setInterval(async () => {
91-
if (dayjs().isAfter(endTime)) clearInterval(countdownInterval);
72+
const countdownInterval = setInterval(() => {
73+
if (dayjs().isAfter(endTime)) {
74+
clearInterval(countdownInterval);
75+
updateEpochs();
76+
}
9277

9378
const leftTime = dayjs.duration(endTime.diff(dayjs())).format('HH:mm:ss');
9479
setCountdown(leftTime.split(':'));
9580
}, 1000);
96-
}, [getRpcEndpoint]);
9781

98-
useEffect(() => {
99-
if (!isMountedRef.current) {
100-
getEpoch();
101-
isMountedRef.current = true;
102-
}
103-
}, [getEpoch]);
82+
return () => clearInterval(countdownInterval);
83+
}, [epochs, updateEpochs]);
84+
85+
const osmoPrice = prices?.[coin.base] || 0;
10486

10587
const titleColor = useColorModeValue('#697584', '#A7B4C2');
10688
const amountColor = useColorModeValue('#2C3137', '#EEF2F8');
@@ -183,7 +165,7 @@ export const PoolsOverview = ({
183165
>
184166
Currently earning
185167
</Text>
186-
<RewardText reward={totalRewardPerDay} />
168+
<RewardText reward={totalDayReward} />
187169
</Flex>
188170
</StatBox>
189171
</GridItem>

0 commit comments

Comments
 (0)