-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathindex.tsx
122 lines (107 loc) · 3.4 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import { formatEther } from '@ethersproject/units'
import { getUnixTime, sub } from 'date-fns'
import { gql } from 'graphql-request'
import { GetStaticProps } from 'next'
import { SWRConfig } from 'swr'
import { NotFound } from '@pancakeswap/uikit'
import { getCakeContract } from 'utils/contractHelpers'
import { getBlocksFromTimestamps } from 'utils/getBlocksFromTimestamps'
import { bitQueryServerClient, infoServerClient } from 'utils/graphql'
import { CHAIN_IDS } from 'utils/wagmi'
import Home from 'views/Home'
import Swap from 'views/Swap'
import { SwapFeaturesProvider } from 'views/Swap/SwapFeaturesContext'
import NeedHelp from 'components/Menu/NeedHelp'
const IndexPage = ({ totalTx30Days, addressCount30Days, tvl }) => {
return (
<SWRConfig
value={{
fallback: {
totalTx30Days,
addressCount30Days,
tvl,
},
}}
>
<Home />
{/* <SwapFeaturesProvider>
<Swap />
</SwapFeaturesProvider> */}
{/* <NotFound /> */}
{/* <NeedHelp /> */}
</SWRConfig>
)
}
// Values fetched from TheGraph and BitQuery jan 24, 2022
const txCount = 54780336
const addressCount = 4425459
const tvl = 6082955532.115718
export const getStaticProps: GetStaticProps = async () => {
const totalTxQuery = gql`
query TotalTransactions($block: Block_height) {
pancakeFactory(block: $block) {
totalTransactions
}
}
`
const days30Ago = sub(new Date(), { days: 30 })
const results = {
totalTx30Days: txCount,
addressCount30Days: addressCount,
}
try {
const [days30AgoBlock] = await getBlocksFromTimestamps([getUnixTime(days30Ago)])
if (!days30AgoBlock) {
throw new Error('No block found for 30 days ago')
}
const totalTx = await infoServerClient.request(totalTxQuery)
const totalTx30DaysAgo = await infoServerClient.request(totalTxQuery, {
block: {
number: days30AgoBlock.number,
},
})
if (
totalTx?.pancakeFactory?.totalTransactions &&
totalTx30DaysAgo?.pancakeFactory?.totalTransactions &&
parseInt(totalTx.pancakeFactory.totalTransactions) > parseInt(totalTx30DaysAgo.pancakeFactory.totalTransactions)
) {
results.totalTx30Days =
parseInt(totalTx.pancakeFactory.totalTransactions) - parseInt(totalTx30DaysAgo.pancakeFactory.totalTransactions)
}
} catch (error) {
if (process.env.NODE_ENV === 'production') {
console.error('Error when fetching total tx count', error)
}
}
const usersQuery = gql`
query userCount($since: ISO8601DateTime, $till: ISO8601DateTime) {
ethereum(network: bsc) {
dexTrades(exchangeName: { in: ["Pancake", "Pancake v2"] }, date: { since: $since, till: $till }) {
count(uniq: senders)
}
}
}
`
if (process.env.BIT_QUERY_HEADER) {
try {
const result = await bitQueryServerClient.request(usersQuery, {
since: days30Ago.toISOString(),
till: new Date().toISOString(),
})
if (result?.ethereum?.dexTrades?.[0]?.count) {
results.addressCount30Days = result.ethereum.dexTrades[0].count
}
} catch (error) {
if (process.env.NODE_ENV === 'production') {
console.error('Error when fetching address count', error)
}
}
}
return {
props: results,
revalidate: 60 * 60 * 24 * 30, // 30 days
}
}
// IndexPage.chains = []
IndexPage.chains = CHAIN_IDS
export default IndexPage