forked from pancakeswap/pancake-frontend
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.tsx
140 lines (124 loc) · 4.02 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { formatEther } from '@ethersproject/units'
import { FACTORY_ADDRESS } from '@pancakeswap/sdk'
import { getUnixTime, sub } from 'date-fns'
import { gql } from 'graphql-request'
import { GetStaticProps } from 'next'
import { SWRConfig } from 'swr'
import { getCakeVaultAddress } from 'utils/addressHelpers'
import { getCakeContract } from 'utils/contractHelpers'
import { getBlocksFromTimestamps } from 'utils/getBlocksFromTimestamps'
import { bitQueryServerClient, infoServerClient } from 'utils/graphql'
import Home from '../views/Home'
const IndexPage = ({ totalTx30Days, addressCount30Days, tvl }) => {
return (
<SWRConfig
value={{
fallback: {
totalTx30Days,
addressCount30Days,
tvl,
},
}}
>
<Home />
</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($id: ID!, $block: Block_height) {
pancakeFactory(id: $id, block: $block) {
totalTransactions
}
}
`
const days30Ago = sub(new Date(), { days: 30 })
const results = {
totalTx30Days: txCount,
addressCount30Days: addressCount,
tvl,
}
if (process.env.SF_HEADER) {
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, {
id: FACTORY_ADDRESS,
})
const totalTx30DaysAgo = await infoServerClient.request(totalTxQuery, {
block: {
number: days30AgoBlock.number,
},
id: FACTORY_ADDRESS,
})
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)
}
}
}
try {
const result = await infoServerClient.request(gql`
query tvl {
pancakeFactories(first: 1) {
totalLiquidityUSD
}
}
`)
const cake = await (await fetch('https://farms.pancake-swap.workers.dev/price/cake')).json()
const { totalLiquidityUSD } = result.pancakeFactories[0]
const cakeVaultV2 = getCakeVaultAddress()
const cakeContract = getCakeContract()
const totalCakeInVault = await cakeContract.balanceOf(cakeVaultV2)
results.tvl = parseFloat(formatEther(totalCakeInVault)) * cake.price + parseFloat(totalLiquidityUSD)
} catch (error) {
if (process.env.NODE_ENV === 'production') {
console.error('Error when fetching tvl stats', error)
}
}
return {
props: results,
revalidate: 60 * 60 * 24 * 30, // 30 days
}
}
IndexPage.chains = []
export default IndexPage