diff --git a/.env.template b/.env.template new file mode 100644 index 0000000..cd0ca45 --- /dev/null +++ b/.env.template @@ -0,0 +1,4 @@ +VITE_SUBGRAPH_MARKETS_URL_ETHEREUM= +VITE_SUBGRAPH_MARKETS_URL_ARBITRUM_ONE= +VITE_SUBGRAPH_MARKETS_URL_BSC_MAINNET= +VITE_SUBGRAPH_MARKETS_URL_OPBNB_MAINNET= diff --git a/README.md b/README.md index b58e0af..13d2ff8 100644 --- a/README.md +++ b/README.md @@ -1,46 +1,49 @@ -# Getting Started with Create React App +# Venus Protocol Landing Page -This project was bootstrapped with [Create React App](https://github.com/facebook/create-react-app). +

+ +

-## Available Scripts +Official repository for the [Venus protocol](https://venus.io) landing page. -In the project directory, you can run: +## Getting started -### `yarn start` +Install dependencies with yarn -Runs the app in the development mode.\ -Open [http://localhost:3000](http://localhost:3000) to view it in the browser. +```ssh +yarn +``` -The page will reload if you make edits.\ -You will also see any lint errors in the console. +Define environment variables by making a copy of `.env.example` and renaming it to `.env` (make sure to add values for each environment variable). -### `yarn test` +Start the development server -Launches the test runner in the interactive watch mode.\ -See the section about [running tests](https://facebook.github.io/create-react-app/docs/running-tests) for more information. +```ssh +yarn start +``` -### `yarn build` +## Useful commands -Builds the app for production to the `build` folder.\ -It correctly bundles React in production mode and optimizes the build for the best performance. +Check Typescript code -The build is minified and the filenames include the hashes.\ -Your app is ready to be deployed! +```ssh +yarn tsc +``` -See the section about [deployment](https://facebook.github.io/create-react-app/docs/deployment) for more information. +Lint code -### `yarn eject` +```ssh +yarn lint +``` -**Note: this is a one-way operation. Once you `eject`, you can’t go back!** +Generate production build -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. +```ssh +yarn build +``` -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. +Start application with production build -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. - -## Learn More - -You can learn more in the [Create React App documentation](https://facebook.github.io/create-react-app/docs/getting-started). - -To learn React, check out the [React documentation](https://reactjs.org/). +```ssh +yarn preview +``` diff --git a/package.json b/package.json index 1d66d0a..9af8bfc 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,8 @@ "@testing-library/user-event": "^13.5.0", "axios": "^0.26.1", "classnames": "^2.3.1", + "graphql": "^16.8.1", + "graphql-request": "^7.0.1", "normalize.css": "^8.0.1", "react": "^18.3.1", "react-dom": "^18.3.1", @@ -26,7 +28,9 @@ "preview": "vite preview", "lint:fix": "yarn lint --fix && yarn pretty --write", "lint": "yarn run eslint .", - "pretty": "yarn prettier \"src/**/*.{js,jsx,ts,tsx,html,json,md}\" public/**/*.html --check" + "pretty": "yarn prettier \"src/**/*.{js,jsx,ts,tsx,html,json,md}\" public/**/*.html --check", + "generate-subgraph-types": "dotenv -- graphql-codegen --config src/subgraph/codegen.ts", + "postinstall": "yarn run generate-subgraph-types" }, "browserslist": { "production": [ @@ -41,6 +45,8 @@ ] }, "devDependencies": { + "@graphql-codegen/cli": "5.0.2", + "@graphql-codegen/client-preset": "4.2.6", "@types/jest": "^29.5.12", "@types/node": "^20.12.12", "@types/react": "^18.3.3", @@ -49,6 +55,7 @@ "@typescript-eslint/parser": "^7.11.0", "@vitejs/plugin-react": "^4.3.1", "autoprefixer": "^9.0.0", + "dotenv-cli": "^7.4.2", "eslint": "^8", "eslint-config-airbnb": "^19.0.4", "eslint-config-airbnb-typescript": "^18.0.0", diff --git a/src/api/hooks/useProposals.ts b/src/api/hooks/useProposals.ts index d61469b..c39cf57 100644 --- a/src/api/hooks/useProposals.ts +++ b/src/api/hooks/useProposals.ts @@ -1,32 +1,8 @@ -import { useEffect, useState } from 'react'; +import { useQuery } from '@tanstack/react-query'; import { fetchProposalCount } from '../index'; -export const useProposalsCountFromApi = () => { - const [data, setData] = useState(); - const [isLoading, setIsLoading] = useState(false); - const [error, setError] = useState(); - - const fetchData = () => { - fetchProposalCount() - .then(res => { - setData(res); - setIsLoading(false); - }) - .catch(e => { - setError(e); - setIsLoading(false); - }); - }; - - useEffect(() => { - setIsLoading(true); - fetchData(); - }, []); - - return { - data, - isLoading, - error, - fetchData, - }; -}; +export const useProposalsCountFromApi = () => + useQuery({ + queryKey: ['proposalCount'], + queryFn: fetchProposalCount, + }); diff --git a/src/api/hooks/useVenusApi.ts b/src/api/hooks/useVenusApi.ts index 819cc40..ab571a8 100644 --- a/src/api/hooks/useVenusApi.ts +++ b/src/api/hooks/useVenusApi.ts @@ -1,42 +1,86 @@ -import { useEffect, useState } from 'react'; -import { MarketMapped } from '../types'; -import { getMarketsToRender, getTotal } from '../utils'; -import fetchMarkets from '../index'; +import { useQuery } from '@tanstack/react-query'; +import { convertCentsToUsd, getMarketsToRender, scale } from '../utils'; +import getLegacyPoolMarkets from '../index'; +import { getIsolatedMarkets } from '../../subgraph/queries/getIsolatedMarkets'; +import { MainChainId } from '../../subgraph/types'; + +const chainIds = Object.values(MainChainId).filter( + (chainId): chainId is MainChainId => !Number.isNaN(Number(chainId)), +); export const useVenusApi = () => { - const [data, setData] = useState(); - const [isLoading, setIsLoading] = useState(false); - const [error, setError] = useState(); - - const fetchData = () => { - fetchMarkets() - .then(res => { - setData(res); - setIsLoading(false); - }) - .catch(e => { - setError(e); - setIsLoading(false); - }); - }; + const { + data: getLegacyPoolMarketsData, + isLoading: isGetLegacyPoolMarketsLoading, + error: getLegacyPoolMarketsError, + refetch, + } = useQuery({ + queryKey: ['legacyPoolMarkets'], + queryFn: getLegacyPoolMarkets, + }); + + const { + data: getIsolatedPoolMarketsData, + isLoading: isGetIsolatedPoolMarketsLoading, + error: getIsolatedPoolMarketsError, + } = useQuery({ + queryKey: ['isolatedPoolMarkets'], + queryFn: async () => { + const results = await Promise.all(chainIds.map(chainId => getIsolatedMarkets({ chainId }))); + return results.map(result => result.markets).flat(); + }, + }); + + const topMarkets = getMarketsToRender(getLegacyPoolMarketsData); + + const marketCount = + (getLegacyPoolMarketsData?.length || 0) + (getIsolatedPoolMarketsData?.length || 0); + + const legacyPool = (getLegacyPoolMarketsData ?? []).reduce( + (acc, data) => ({ + marketSize: acc.marketSize + data.totalSupplyUsd, + borrowedSum: acc.borrowedSum + data.totalBorrowsUsd, + liquiditySum: acc.liquiditySum + data.liquidity, + }), + { + marketSize: 0, + borrowedSum: 0, + liquiditySum: 0, + }, + ); + + const isolatedPools = (getIsolatedPoolMarketsData ?? []).reduce( + (acc, data) => { + const underlyingTokenPriceUsd = convertCentsToUsd(data.underlyingPriceCents); + + const totalSupplyTokens = scale(data.totalSupplyMantissa, data.underlyingDecimals); + const totalSupplyUsd = totalSupplyTokens * underlyingTokenPriceUsd; - useEffect(() => { - setIsLoading(true); - fetchData(); - }, []); + const totalBorrowsTokens = scale(data.totalBorrowsMantissa, data.underlyingDecimals); + const totalBorrowsUsd = totalBorrowsTokens * underlyingTokenPriceUsd; - const markets = getMarketsToRender(data); - const marketSize = getTotal('totalSupplyUsd', data); - const borrowedSum = getTotal('totalBorrowsUsd', data); - const liquiditySum = getTotal('liquidity', data); + return { + marketSize: acc.marketSize + totalSupplyUsd, + borrowedSum: acc.borrowedSum + totalBorrowsUsd, + liquiditySum: acc.liquiditySum + (totalSupplyUsd - totalBorrowsUsd), + }; + }, + { + marketSize: 0, + borrowedSum: 0, + liquiditySum: 0, + }, + ); return { - marketSize, - borrowedSum, - liquiditySum, - markets, - isLoading, - error, - fetchData, + marketSize: legacyPool.marketSize + isolatedPools.marketSize, + borrowedSum: legacyPool.borrowedSum + isolatedPools.borrowedSum, + liquiditySum: legacyPool.liquiditySum + isolatedPools.liquiditySum, + topMarkets, + marketCount, + chainCount: chainIds.length, + isLoading: isGetLegacyPoolMarketsLoading || isGetIsolatedPoolMarketsLoading, + error: getLegacyPoolMarketsError || getIsolatedPoolMarketsError, + refetch, }; }; diff --git a/src/api/utils.ts b/src/api/utils.ts index 6775be6..c57eb06 100644 --- a/src/api/utils.ts +++ b/src/api/utils.ts @@ -65,22 +65,14 @@ export const getMarketsToRender = (markets?: MarketMapped[]) => { return sortedMarkets.slice(0, 5).sort(sortBySupplyApy); }; -const sumArray = (numbers: number[]) => numbers.reduce((partialSum, a) => partialSum + a, 0); - const addSpaceBeforeUSDSymbol = (string: string) => string.replace(/^(\D+)/, '$\u00a0'); -export const getTotal = ( - key: 'totalSupplyUsd' | 'totalBorrowsUsd' | 'liquidity', - markets?: MarketMapped[], -) => { - if (!markets) return []; - const totalSupplyUsd = markets.map(i => i[key]); - const sum = sumArray(totalSupplyUsd); - const formattedSum = new Intl.NumberFormat('en-EN', { +export const formatUsd = (value: number) => { + const formattedValue = new Intl.NumberFormat('en-EN', { style: 'currency', currency: 'USD', - }).format(sum); - return addSpaceBeforeUSDSymbol(formattedSum); + }).format(value); + return addSpaceBeforeUSDSymbol(formattedValue); }; export const nFormatter = (num: number, digits = 2) => { diff --git a/src/components/Market/Market.module.css b/src/components/Market/Market.module.css index a06579c..cfbe19b 100644 --- a/src/components/Market/Market.module.css +++ b/src/components/Market/Market.module.css @@ -56,6 +56,9 @@ flex-direction: column; justify-content: space-between; width: 100%; + border-bottom: 1px solid var(--color-card-border); + padding-bottom: 16px; + margin-bottom: 16px; @media (min-width: 640px) { flex-direction: row; @@ -115,6 +118,16 @@ } } +.totalDescription { + width: 100%; + text-align: center; + font-size: 0.75rem; + + span { + color: #00C38E; + } +} + .launchBtnDesktop { display: none; @media (min-width: 1280px) { diff --git a/src/components/Market/Market.tsx b/src/components/Market/Market.tsx index bb39f09..d448d38 100644 --- a/src/components/Market/Market.tsx +++ b/src/components/Market/Market.tsx @@ -2,7 +2,7 @@ import React from 'react'; import cn from 'classnames'; import { useVenusApi } from '../../api/hooks/useVenusApi'; import Container from '../Container/Container'; -import { nFormatter } from '../../api/utils'; +import { formatUsd, nFormatter } from '../../api/utils'; import s from './Market.module.css'; interface IMarketProps { @@ -12,14 +12,23 @@ interface IMarketProps { const loadingState = 'Loading...'; const Market: React.FC = ({ className }) => { - const { marketSize, borrowedSum, liquiditySum, markets, isLoading, error, fetchData } = - useVenusApi(); + const { + marketSize, + marketCount, + chainCount, + borrowedSum, + liquiditySum, + topMarkets, + isLoading, + error, + refetch, + } = useVenusApi(); if (error) { return (

{error.message}

-
@@ -33,24 +42,29 @@ const Market: React.FC = ({ className }) => {
  • Market size

    -

    {isLoading ? loadingState : marketSize}

    +

    {isLoading ? loadingState : formatUsd(marketSize)}

  • Total Borrowed

    -

    {isLoading ? loadingState : borrowedSum}

    +

    {isLoading ? loadingState : formatUsd(borrowedSum)}

  • Total Liquidity

    -

    {isLoading ? loadingState : liquiditySum}

    +

    {isLoading ? loadingState : formatUsd(liquiditySum)}

  • + +

    + Our liquidities are distributed across {marketCount} markets on{' '} + {chainCount} networks +

    {isLoading ? ( @@ -66,7 +80,7 @@ const Market: React.FC = ({ className }) => {
      - {markets.map(i => ( + {topMarkets.map(i => (
    • {i.underlyingSymbol} diff --git a/src/subgraph/codegen.ts b/src/subgraph/codegen.ts new file mode 100644 index 0000000..0bd7424 --- /dev/null +++ b/src/subgraph/codegen.ts @@ -0,0 +1,17 @@ +import type { CodegenConfig } from '@graphql-codegen/cli'; +import { SUBGRAPH_URLS } from './subgraphUrls'; +import { MainChainId } from './types'; + +const config: CodegenConfig = { + overwrite: true, + schema: SUBGRAPH_URLS[MainChainId.BSC], + documents: 'src/subgraph/queries/**/*.graphql', + generates: { + 'src/subgraph/client/': { + preset: 'client', + plugins: [], + }, + }, +}; + +export default config; diff --git a/src/subgraph/queries/getIsolatedMarkets/index.ts b/src/subgraph/queries/getIsolatedMarkets/index.ts new file mode 100644 index 0000000..261f633 --- /dev/null +++ b/src/subgraph/queries/getIsolatedMarkets/index.ts @@ -0,0 +1,11 @@ +import { request } from 'graphql-request'; + +import { IsolatedMarketsDocument } from '../../client/graphql'; +import { MainChainId } from '../../types'; +import { SUBGRAPH_URLS } from '../../subgraphUrls'; + +export const getIsolatedMarkets = ({ + chainId, +}: { + chainId: MainChainId; +}) => request(SUBGRAPH_URLS[chainId], IsolatedMarketsDocument); diff --git a/src/subgraph/queries/getIsolatedMarkets/isolatedMarkets.graphql b/src/subgraph/queries/getIsolatedMarkets/isolatedMarkets.graphql new file mode 100644 index 0000000..2c31e74 --- /dev/null +++ b/src/subgraph/queries/getIsolatedMarkets/isolatedMarkets.graphql @@ -0,0 +1,10 @@ +query IsolatedMarkets { + markets { + id + exchangeRateMantissa + totalSupplyMantissa + totalBorrowsMantissa + underlyingPriceCents + underlyingDecimals + } +} diff --git a/src/subgraph/subgraphUrls.ts b/src/subgraph/subgraphUrls.ts new file mode 100644 index 0000000..67f9000 --- /dev/null +++ b/src/subgraph/subgraphUrls.ts @@ -0,0 +1,14 @@ +import { MainChainId } from './types'; + +export const SUBGRAPH_URLS: { + [chainId in MainChainId]: string; +} = { + [MainChainId.BSC]: + import.meta.env.VITE_SUBGRAPH_MARKETS_URL_BSC_MAINNET, + [MainChainId.OPBNB]: + import.meta.env.VITE_SUBGRAPH_MARKETS_URL_OPBNB_MAINNET, + [MainChainId.ETHEREUM]: + import.meta.env.VITE_SUBGRAPH_MARKETS_URL_ETHEREUM, + [MainChainId.ARBITRUM_ONE]: + import.meta.env.VITE_SUBGRAPH_MARKETS_URL_ARBITRUM_ONE, +}; diff --git a/src/subgraph/types.ts b/src/subgraph/types.ts new file mode 100644 index 0000000..6305763 --- /dev/null +++ b/src/subgraph/types.ts @@ -0,0 +1,6 @@ +export enum MainChainId { + BSC = 56, + ETHEREUM = 1, + OPBNB = 204, + ARBITRUM_ONE = 42161, +} diff --git a/yarn.lock b/yarn.lock index b7b063d..42e1662 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3402,7 +3402,22 @@ dot-case@^3.0.4: no-case "^3.0.4" tslib "^2.0.3" -dotenv@^16.0.0: +dotenv-cli@^7.4.2: + version "7.4.2" + resolved "https://registry.yarnpkg.com/dotenv-cli/-/dotenv-cli-7.4.2.tgz#c158a818de08e1fbc51d310f628cbace9075b734" + integrity sha512-SbUj8l61zIbzyhIbg0FwPJq6+wjbzdn9oEtozQpZ6kW2ihCcapKVZj49oCT3oPM+mgQm+itgvUQcG5szxVrZTA== + dependencies: + cross-spawn "^7.0.3" + dotenv "^16.3.0" + dotenv-expand "^10.0.0" + minimist "^1.2.6" + +dotenv-expand@^10.0.0: + version "10.0.0" + resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-10.0.0.tgz#12605d00fb0af6d0a592e6558585784032e4ef37" + integrity sha512-GopVGCpVS1UKH75VKHGuQFqS1Gusej0z4FyQkPdwjil2gNIv+LNsqBlboOzpJFZKVT95GkCyWJbBSdFEFUWI2A== + +dotenv@^16.0.0, dotenv@^16.3.0: version "16.4.5" resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.5.tgz#cdd3b3b604cb327e286b4762e13502f717cb099f" integrity sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==