Skip to content
This repository was archived by the owner on Sep 2, 2024. It is now read-only.

Commit a3e365c

Browse files
committed
feat: fetch TVL across all networks using subgraphs
1 parent 9317a18 commit a3e365c

File tree

14 files changed

+241
-115
lines changed

14 files changed

+241
-115
lines changed

.env.template

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
VITE_SUBGRAPH_MARKETS_URL_ETHEREUM=
2+
VITE_SUBGRAPH_MARKETS_URL_ARBITRUM_ONE=
3+
VITE_SUBGRAPH_MARKETS_URL_BSC_MAINNET=
4+
VITE_SUBGRAPH_MARKETS_URL_OPBNB_MAINNET=

README.md

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,49 @@
1-
# Getting Started with Create React App
1+
# Venus Protocol Landing Page
22

3-
This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app).
3+
<p align="center">
4+
<img src="https://venus.io/share.png">
5+
</p>
46

5-
## Available Scripts
7+
Official repository for the [Venus protocol](https://venus.io) landing page.
68

7-
In the project directory, you can run:
9+
## Getting started
810

9-
### `yarn start`
11+
Install dependencies with yarn
1012

11-
Runs the app in the development mode.\
12-
Open [http://localhost:3000](http://localhost:3000) to view it in the browser.
13+
```ssh
14+
yarn
15+
```
1316

14-
The page will reload if you make edits.\
15-
You will also see any lint errors in the console.
17+
Define environment variables by making a copy of `.env.example` and renaming it to `.env` (make sure to add values for each environment variable).
1618

17-
### `yarn test`
19+
Start the development server
1820

19-
Launches the test runner in the interactive watch mode.\
20-
See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information.
21+
```ssh
22+
yarn start
23+
```
2124

22-
### `yarn build`
25+
## Useful commands
2326

24-
Builds the app for production to the `build` folder.\
25-
It correctly bundles React in production mode and optimizes the build for the best performance.
27+
Check Typescript code
2628

27-
The build is minified and the filenames include the hashes.\
28-
Your app is ready to be deployed!
29+
```ssh
30+
yarn tsc
31+
```
2932

30-
See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information.
33+
Lint code
3134

32-
### `yarn eject`
35+
```ssh
36+
yarn lint
37+
```
3338

34-
**Note: this is a one-way operation. Once you `eject`, you can’t go back!**
39+
Generate production build
3540

36-
If you aren’t satisfied with the build tool and configuration choices, you can `eject` at any time. This command will remove the single build dependency from your project.
41+
```ssh
42+
yarn build
43+
```
3744

38-
Instead, it will copy all the configuration files and the transitive dependencies (webpack, Babel, ESLint, etc) right into your project so you have full control over them. All of the commands except `eject` will still work, but they will point to the copied scripts so you can tweak them. At this point you’re on your own.
45+
Start application with production build
3946

40-
You don’t have to ever use `eject`. The curated feature set is suitable for small and middle deployments, and you shouldn’t feel obligated to use this feature. However we understand that this tool wouldn’t be useful if you couldn’t customize it when you are ready for it.
41-
42-
## Learn More
43-
44-
You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started).
45-
46-
To learn React, check out the [React documentation](https://reactjs.org/).
47+
```ssh
48+
yarn preview
49+
```

package.json

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"@testing-library/user-event": "^13.5.0",
1212
"axios": "^0.26.1",
1313
"classnames": "^2.3.1",
14+
"graphql": "^16.8.1",
15+
"graphql-request": "^7.0.1",
1416
"normalize.css": "^8.0.1",
1517
"react": "^18.3.1",
1618
"react-dom": "^18.3.1",
@@ -26,7 +28,9 @@
2628
"preview": "vite preview",
2729
"lint:fix": "yarn lint --fix && yarn pretty --write",
2830
"lint": "yarn run eslint .",
29-
"pretty": "yarn prettier \"src/**/*.{js,jsx,ts,tsx,html,json,md}\" public/**/*.html --check"
31+
"pretty": "yarn prettier \"src/**/*.{js,jsx,ts,tsx,html,json,md}\" public/**/*.html --check",
32+
"generate-subgraph-types": "dotenv -- graphql-codegen --config src/subgraph/codegen.ts",
33+
"postinstall": "yarn run generate-subgraph-types"
3034
},
3135
"browserslist": {
3236
"production": [
@@ -41,6 +45,8 @@
4145
]
4246
},
4347
"devDependencies": {
48+
"@graphql-codegen/cli": "5.0.2",
49+
"@graphql-codegen/client-preset": "4.2.6",
4450
"@types/jest": "^29.5.12",
4551
"@types/node": "^20.12.12",
4652
"@types/react": "^18.3.3",
@@ -49,6 +55,7 @@
4955
"@typescript-eslint/parser": "^7.11.0",
5056
"@vitejs/plugin-react": "^4.3.1",
5157
"autoprefixer": "^9.0.0",
58+
"dotenv-cli": "^7.4.2",
5259
"eslint": "^8",
5360
"eslint-config-airbnb": "^19.0.4",
5461
"eslint-config-airbnb-typescript": "^18.0.0",

src/api/hooks/useProposals.ts

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,8 @@
1-
import { useEffect, useState } from 'react';
1+
import { useQuery } from '@tanstack/react-query';
22
import { fetchProposalCount } from '../index';
33

4-
export const useProposalsCountFromApi = () => {
5-
const [data, setData] = useState<number>();
6-
const [isLoading, setIsLoading] = useState(false);
7-
const [error, setError] = useState<Error | undefined>();
8-
9-
const fetchData = () => {
10-
fetchProposalCount()
11-
.then(res => {
12-
setData(res);
13-
setIsLoading(false);
14-
})
15-
.catch(e => {
16-
setError(e);
17-
setIsLoading(false);
18-
});
19-
};
20-
21-
useEffect(() => {
22-
setIsLoading(true);
23-
fetchData();
24-
}, []);
25-
26-
return {
27-
data,
28-
isLoading,
29-
error,
30-
fetchData,
31-
};
32-
};
4+
export const useProposalsCountFromApi = () =>
5+
useQuery({
6+
queryKey: ['proposalCount'],
7+
queryFn: fetchProposalCount,
8+
});

src/api/hooks/useVenusApi.ts

Lines changed: 78 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,86 @@
1-
import { useEffect, useState } from 'react';
2-
import { MarketMapped } from '../types';
3-
import { getMarketsToRender, getTotal } from '../utils';
4-
import fetchMarkets from '../index';
1+
import { useQuery } from '@tanstack/react-query';
2+
import { convertCentsToUsd, getMarketsToRender, scale } from '../utils';
3+
import getLegacyPoolMarkets from '../index';
4+
import { getIsolatedMarkets } from '../../subgraph/queries/getIsolatedMarkets';
5+
import { MainChainId } from '../../subgraph/types';
6+
7+
const chainIds = Object.values(MainChainId).filter(
8+
(chainId): chainId is MainChainId => !Number.isNaN(Number(chainId)),
9+
);
510

611
export const useVenusApi = () => {
7-
const [data, setData] = useState<MarketMapped[]>();
8-
const [isLoading, setIsLoading] = useState(false);
9-
const [error, setError] = useState<Error | undefined>();
10-
11-
const fetchData = () => {
12-
fetchMarkets()
13-
.then(res => {
14-
setData(res);
15-
setIsLoading(false);
16-
})
17-
.catch(e => {
18-
setError(e);
19-
setIsLoading(false);
20-
});
21-
};
12+
const {
13+
data: getLegacyPoolMarketsData,
14+
isLoading: isGetLegacyPoolMarketsLoading,
15+
error: getLegacyPoolMarketsError,
16+
refetch,
17+
} = useQuery({
18+
queryKey: ['legacyPoolMarkets'],
19+
queryFn: getLegacyPoolMarkets,
20+
});
21+
22+
const {
23+
data: getIsolatedPoolMarketsData,
24+
isLoading: isGetIsolatedPoolMarketsLoading,
25+
error: getIsolatedPoolMarketsError,
26+
} = useQuery({
27+
queryKey: ['isolatedPoolMarkets'],
28+
queryFn: async () => {
29+
const results = await Promise.all(chainIds.map(chainId => getIsolatedMarkets({ chainId })));
30+
return results.map(result => result.markets).flat();
31+
},
32+
});
33+
34+
const topMarkets = getMarketsToRender(getLegacyPoolMarketsData);
35+
36+
const marketCount =
37+
(getLegacyPoolMarketsData?.length || 0) + (getIsolatedPoolMarketsData?.length || 0);
38+
39+
const legacyPool = (getLegacyPoolMarketsData ?? []).reduce(
40+
(acc, data) => ({
41+
marketSize: acc.marketSize + data.totalSupplyUsd,
42+
borrowedSum: acc.borrowedSum + data.totalBorrowsUsd,
43+
liquiditySum: acc.liquiditySum + data.liquidity,
44+
}),
45+
{
46+
marketSize: 0,
47+
borrowedSum: 0,
48+
liquiditySum: 0,
49+
},
50+
);
51+
52+
const isolatedPools = (getIsolatedPoolMarketsData ?? []).reduce(
53+
(acc, data) => {
54+
const underlyingTokenPriceUsd = convertCentsToUsd(data.underlyingPriceCents);
55+
56+
const totalSupplyTokens = scale(data.totalSupplyMantissa, data.underlyingDecimals);
57+
const totalSupplyUsd = totalSupplyTokens * underlyingTokenPriceUsd;
2258

23-
useEffect(() => {
24-
setIsLoading(true);
25-
fetchData();
26-
}, []);
59+
const totalBorrowsTokens = scale(data.totalBorrowsMantissa, data.underlyingDecimals);
60+
const totalBorrowsUsd = totalBorrowsTokens * underlyingTokenPriceUsd;
2761

28-
const markets = getMarketsToRender(data);
29-
const marketSize = getTotal('totalSupplyUsd', data);
30-
const borrowedSum = getTotal('totalBorrowsUsd', data);
31-
const liquiditySum = getTotal('liquidity', data);
62+
return {
63+
marketSize: acc.marketSize + totalSupplyUsd,
64+
borrowedSum: acc.borrowedSum + totalBorrowsUsd,
65+
liquiditySum: acc.liquiditySum + (totalSupplyUsd - totalBorrowsUsd),
66+
};
67+
},
68+
{
69+
marketSize: 0,
70+
borrowedSum: 0,
71+
liquiditySum: 0,
72+
},
73+
);
3274

3375
return {
34-
marketSize,
35-
borrowedSum,
36-
liquiditySum,
37-
markets,
38-
isLoading,
39-
error,
40-
fetchData,
76+
marketSize: legacyPool.marketSize + isolatedPools.marketSize,
77+
borrowedSum: legacyPool.borrowedSum + isolatedPools.borrowedSum,
78+
liquiditySum: legacyPool.liquiditySum + isolatedPools.liquiditySum,
79+
topMarkets,
80+
marketCount,
81+
chainCount: chainIds.length,
82+
isLoading: isGetLegacyPoolMarketsLoading || isGetIsolatedPoolMarketsLoading,
83+
error: getLegacyPoolMarketsError || getIsolatedPoolMarketsError,
84+
refetch,
4185
};
4286
};

src/api/utils.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,22 +65,14 @@ export const getMarketsToRender = (markets?: MarketMapped[]) => {
6565
return sortedMarkets.slice(0, 5).sort(sortBySupplyApy);
6666
};
6767

68-
const sumArray = (numbers: number[]) => numbers.reduce((partialSum, a) => partialSum + a, 0);
69-
7068
const addSpaceBeforeUSDSymbol = (string: string) => string.replace(/^(\D+)/, '$\u00a0');
7169

72-
export const getTotal = (
73-
key: 'totalSupplyUsd' | 'totalBorrowsUsd' | 'liquidity',
74-
markets?: MarketMapped[],
75-
) => {
76-
if (!markets) return [];
77-
const totalSupplyUsd = markets.map(i => i[key]);
78-
const sum = sumArray(totalSupplyUsd);
79-
const formattedSum = new Intl.NumberFormat('en-EN', {
70+
export const formatUsd = (value: number) => {
71+
const formattedValue = new Intl.NumberFormat('en-EN', {
8072
style: 'currency',
8173
currency: 'USD',
82-
}).format(sum);
83-
return addSpaceBeforeUSDSymbol(formattedSum);
74+
}).format(value);
75+
return addSpaceBeforeUSDSymbol(formattedValue);
8476
};
8577

8678
export const nFormatter = (num: number, digits = 2) => {

src/components/Market/Market.module.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@
5656
flex-direction: column;
5757
justify-content: space-between;
5858
width: 100%;
59+
border-bottom: 1px solid var(--color-card-border);
60+
padding-bottom: 16px;
61+
margin-bottom: 16px;
5962

6063
@media (min-width: 640px) {
6164
flex-direction: row;
@@ -115,6 +118,16 @@
115118
}
116119
}
117120

121+
.totalDescription {
122+
width: 100%;
123+
text-align: center;
124+
font-size: 0.75rem;
125+
126+
span {
127+
color: #00C38E;
128+
}
129+
}
130+
118131
.launchBtnDesktop {
119132
display: none;
120133
@media (min-width: 1280px) {

0 commit comments

Comments
 (0)