Skip to content

Commit

Permalink
feat(frontend): show all-time volume
Browse files Browse the repository at this point in the history
fix: pnpm

fixup! feat(frontend): show all-time volume

fix: make requests concurrently

fixup! feat(frontend): show all-time volume

fix: font size and layout shift

fixup! feat(frontend): show all-time volume

fix: reduce request frequency
  • Loading branch information
boriskubrik committed Oct 25, 2023
1 parent e0754bb commit 26026a0
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"bn.js": "5.2.1",
"debounce": "^1.2.1",
"ethers": "^5.6.1",
"graphql-request": "^6.1.0",
"hash.js": "1.1.7",
"js-sha3": "0.8.0",
"react": "^18.2.0",
Expand Down
2 changes: 2 additions & 0 deletions packages/frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import { Header } from './components/Header/Header';
import { Hero } from './components/Hero/Hero';
import { Route, Routes } from 'react-router-dom';
import { SpaceTravelCanvas } from './space-travel/SpaceTravelCanvas';
import { Stats } from './components/Stats/Stats';

const Layout: FunctionComponent<{ children: ReactNode }> = ({ children }) => {
return (
<>
<SpaceTravelCanvas />
<div className="relative">
<Header />
<Stats />
<main className="px-2 sm:px-8 relative">{children}</main>
<Footer />
</div>
Expand Down
23 changes: 23 additions & 0 deletions packages/frontend/src/components/Stats/Stats.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import { useAllTimeStats } from 'src/hooks/useAllTimeStats';
import { numberWithCommas } from 'src/utils/numberWithCommas';

const Stats = () => {
const { error, data } = useAllTimeStats();

if (error) {
console.error('Failed to fetch all-time stats:', error);
}

return (
<div className="font-display m-auto text-center text-sm p-2 flex place-items-center justify-center relative">
{data ? (
`All-time volume: $${numberWithCommas(data)}`
) : (
// To avoid layout shifts
<span className="invisible">Placehodler</span>
)}
</div>
);
};

export { Stats };
48 changes: 48 additions & 0 deletions packages/frontend/src/hooks/useAllTimeStats.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { useQuery } from '@tanstack/react-query';
import { request, gql } from 'graphql-request';
import GRAPH_URLS from 'src/subgraph.json';

const ALL_TIME_STATS_QUERY = gql`
query {
allTimeStats(id: "current") {
volumeUsd
}
}
`;

type AllTimeStatsResponse = {
allTimeStats: {
volumeUsd: string;
};
};

const useAllTimeStats = () => {
return useQuery(
['allTimeStats'],
async () => {
const responses = (await Promise.all(
Object.values(GRAPH_URLS).map(url =>
request(url, ALL_TIME_STATS_QUERY).catch((error: any) => {
console.error(`Failed to fetch data from ${url}:`, error);

return { allTimeStats: { volumeUsd: '0' } };
})
)
)) as AllTimeStatsResponse[];

const totalVolumeUsd = responses.reduce(
(sum: number, response: AllTimeStatsResponse) =>
sum + Number(response.allTimeStats?.volumeUsd ?? '0'),
0
);

return Math.round(totalVolumeUsd);
},
{
staleTime: 1000 * 60 * 5, // 5 minutes
refetchOnWindowFocus: false,
}
);
};

export { useAllTimeStats };
9 changes: 9 additions & 0 deletions packages/frontend/src/subgraph.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"1": "https://gateway-arbitrum.network.thegraph.com/api/3e9d2f58d24bcd5d854ba4c4c8df5c3b/subgraphs/id/G316MkVLSw1ksstmonpSQ1UEBUWmRSZf4QkoRA5CN4TA",
"10": "https://api.thegraph.com/subgraphs/name/sifiorg/sifi-optimism",
"56": "https://api.thegraph.com/subgraphs/name/sifiorg/sifi-bsc",
"137": "https://gateway-arbitrum.network.thegraph.com/api/3e9d2f58d24bcd5d854ba4c4c8df5c3b/subgraphs/id/VJesTAc98ochTTwDQViVgnM4jWhyRC4pyWHBpDQkCjj",
"8453": "https://api.thegraph.com/subgraphs/name/sifiorg/sifi-base",
"42161": "https://gateway-arbitrum.network.thegraph.com/api/3e9d2f58d24bcd5d854ba4c4c8df5c3b/subgraphs/id/BVexPGe3pnB7Mw7ajFkEDQJ8XY8XdVKbAL37ydmorhSQ",
"43114": "https://gateway-arbitrum.network.thegraph.com/api/3e9d2f58d24bcd5d854ba4c4c8df5c3b/subgraphs/id/4cn8JGVKUJBWRJNt4n5KQNMKyMLjRATUBLQ9oSCbL9QA"
}
2 changes: 1 addition & 1 deletion packages/frontend/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@
"composite": true,
"outDir": "dist"
},
"include": ["src/**/*", ".storybook/decorators/*", "src"],
"include": ["src/**/*", ".storybook/decorators/*", "src", "src/subgraph.json"],
"exclude": ["node_modules", "dist"]
}
23 changes: 23 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 26026a0

Please sign in to comment.