forked from pancakeswap/pancake-frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers.ts
271 lines (248 loc) · 9.01 KB
/
helpers.ts
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
import { ChainId } from '@pancakeswap/sdk'
import fromPairs from 'lodash/fromPairs'
import chunk from 'lodash/chunk'
import uniq from 'lodash/uniq'
import mapValues from 'lodash/mapValues'
import { ONE_DAY_UNIX } from 'config/constants/info'
import IPancakePairABI from 'config/abi/IPancakePair.json'
import bep20Abi from 'config/abi/erc20.json'
import { multicallv2 } from 'utils/multicall'
import { getUnixTime } from 'date-fns'
import { TransactionType } from 'state/info/types'
import { ChartEntry } from '../types'
import { MultiChainName, multiChainStartTime } from '../constant'
import { MintResponse, SwapResponse, BurnResponse, TokenDayData, PairDayData, PancakeDayData } from './types'
export const mapMints = (mint: MintResponse) => {
return {
type: TransactionType.MINT,
hash: mint.id.split('-')[0],
timestamp: mint.timestamp,
sender: mint.to,
token0Symbol: mint.pair.token0.symbol,
token1Symbol: mint.pair.token1.symbol,
token0Address: mint.pair.token0.id,
token1Address: mint.pair.token1.id,
amountUSD: parseFloat(mint.amountUSD),
amountToken0: parseFloat(mint.amount0),
amountToken1: parseFloat(mint.amount1),
}
}
export const mapBurns = (burn: BurnResponse) => {
return {
type: TransactionType.BURN,
hash: burn.id.split('-')[0],
timestamp: burn.timestamp,
sender: burn.sender,
token0Symbol: burn.pair.token0.symbol,
token1Symbol: burn.pair.token1.symbol,
token0Address: burn.pair.token0.id,
token1Address: burn.pair.token1.id,
amountUSD: parseFloat(burn.amountUSD),
amountToken0: parseFloat(burn.amount0),
amountToken1: parseFloat(burn.amount1),
}
}
export const mapSwaps = (swap: SwapResponse) => {
return {
type: TransactionType.SWAP,
hash: swap.id.split('-')[0],
timestamp: swap.timestamp,
sender: swap.from,
token0Symbol: swap.pair.token0.symbol,
token1Symbol: swap.pair.token1.symbol,
token0Address: swap.pair.token0.id,
token1Address: swap.pair.token1.id,
amountUSD: parseFloat(swap.amountUSD),
amountToken0: parseFloat(swap.amount0In) - parseFloat(swap.amount0Out),
amountToken1: parseFloat(swap.amount1In) - parseFloat(swap.amount1Out),
}
}
export const mapDayData = (tokenDayData: TokenDayData | PancakeDayData): ChartEntry => ({
date: tokenDayData.date,
volumeUSD: parseFloat(tokenDayData.dailyVolumeUSD),
liquidityUSD: parseFloat(tokenDayData.totalLiquidityUSD),
})
export const mapPairDayData = (pairDayData: PairDayData): ChartEntry => ({
date: pairDayData.date,
volumeUSD: parseFloat(pairDayData.dailyVolumeUSD),
liquidityUSD: parseFloat(pairDayData.reserveUSD),
})
type PoolOrTokenFetchFn = (
chainName: MultiChainName,
skip: number,
address: string,
) => Promise<{ data?: ChartEntry[]; error: boolean }>
type OverviewFetchFn = (chianName: MultiChainName, skip: number) => Promise<{ data?: ChartEntry[]; error: boolean }>
// Common helper function to retrieve chart data
// Used for both Pool and Token charts
export const fetchChartData = async (
chainName: MultiChainName,
getEntityDayDatas: OverviewFetchFn,
): Promise<{ data?: ChartEntry[]; error: boolean }> => {
let chartEntries: ChartEntry[] = []
let error = false
let skip = 0
let allFound = false
while (!allFound && !error) {
// eslint-disable-next-line no-await-in-loop
const { data, error: fetchError } = await getEntityDayDatas(chainName, skip)
skip += 1000
allFound = data?.length < 1000 || skip > 2000
error = fetchError
if (data) {
chartEntries = chartEntries.concat(data)
}
}
if (error || chartEntries.length === 0) {
return {
error: true,
}
}
const formattedDayDatas = fromPairs(
chartEntries.map((dayData) => {
// At this stage we track unix day ordinal for each data point to check for empty days later
const dayOrdinal = parseInt((dayData.date / ONE_DAY_UNIX).toFixed(0))
return [dayOrdinal, dayData]
}),
)
console.warn(formattedDayDatas)
const availableDays = Object.keys(formattedDayDatas).map((dayOrdinal) => parseInt(dayOrdinal, 10))
const firstAvailableDayData = formattedDayDatas[availableDays[0]]
// fill in empty days ( there will be no day datas if no trades made that day )
let timestamp = firstAvailableDayData?.date ?? multiChainStartTime[chainName]
let latestLiquidityUSD = firstAvailableDayData?.liquidityUSD ?? 0
const endTimestamp = getUnixTime(new Date())
while (timestamp < endTimestamp - ONE_DAY_UNIX) {
timestamp += ONE_DAY_UNIX
const dayOrdinal = parseInt((timestamp / ONE_DAY_UNIX).toFixed(0), 10)
if (!Object.keys(formattedDayDatas).includes(dayOrdinal.toString())) {
formattedDayDatas[dayOrdinal] = {
date: timestamp,
volumeUSD: 0,
liquidityUSD: latestLiquidityUSD,
}
} else {
latestLiquidityUSD = formattedDayDatas[dayOrdinal].liquidityUSD
}
}
return {
data: Object.values(formattedDayDatas),
error: false,
}
}
export const fetchChartDataWithAddress = async (
chainName: MultiChainName,
getEntityDayDatas: PoolOrTokenFetchFn,
address: string,
): Promise<{ data?: ChartEntry[]; error: boolean }> => {
let chartEntries: ChartEntry[] = []
let error = false
let skip = 0
let allFound = false
while (!allFound && !error) {
// eslint-disable-next-line no-await-in-loop
const { data, error: fetchError } = await getEntityDayDatas(chainName, skip, address)
skip += 1000
allFound = data?.length < 1000 || skip > 2000
error = fetchError
if (data) {
chartEntries = chartEntries.concat(data)
}
}
if (error || chartEntries.length === 0) {
return {
error: true,
}
}
const formattedDayDatas = fromPairs(
chartEntries.map((dayData) => {
// At this stage we track unix day ordinal for each data point to check for empty days later
const dayOrdinal = parseInt((dayData.date / ONE_DAY_UNIX).toFixed(0))
return [dayOrdinal, dayData]
}),
)
console.warn(formattedDayDatas)
const availableDays = Object.keys(formattedDayDatas).map((dayOrdinal) => parseInt(dayOrdinal, 10))
const firstAvailableDayData = formattedDayDatas[availableDays[0]]
// fill in empty days ( there will be no day datas if no trades made that day )
let timestamp = firstAvailableDayData?.date ?? multiChainStartTime[chainName]
let latestLiquidityUSD = firstAvailableDayData?.liquidityUSD ?? 0
const endTimestamp = getUnixTime(new Date())
while (timestamp < endTimestamp - ONE_DAY_UNIX) {
timestamp += ONE_DAY_UNIX
const dayOrdinal = parseInt((timestamp / ONE_DAY_UNIX).toFixed(0), 10)
if (!Object.keys(formattedDayDatas).includes(dayOrdinal.toString())) {
formattedDayDatas[dayOrdinal] = {
date: timestamp,
volumeUSD: 0,
liquidityUSD: latestLiquidityUSD,
}
} else {
latestLiquidityUSD = formattedDayDatas[dayOrdinal].liquidityUSD
}
}
return {
data: Object.values(formattedDayDatas),
error: false,
}
}
export async function getPairTokenMap(poolAddresses: string[], chainName: 'ETH' | 'BSC') {
let rawPairTokenResults: string[][]
const calls = poolAddresses
.map((poolAddress) => {
return [
{ address: poolAddress, name: 'token0' },
{ address: poolAddress, name: 'token1' },
]
})
.flat()
try {
rawPairTokenResults = await multicallv2({
abi: IPancakePairABI,
calls,
options: { requireSuccess: false },
chainId: chainName === 'ETH' ? ChainId.ETHEREUM : ChainId.BSC,
})
} catch (error) {
console.info('Error fetching tokenIds from pair')
}
const pairTokenResults = rawPairTokenResults
? rawPairTokenResults.map((rawPairTokenResult) => rawPairTokenResult[0].toLowerCase())
: []
const pairTokenMap: { [tokenAddress: string]: { token0: string; token1: string } } = chunk(
pairTokenResults,
2,
).reduce((acc, tokenResult, index) => {
return { ...acc, [poolAddresses[index].toLowerCase()]: { token0: tokenResult[0], token1: tokenResult[1] } }
}, {})
let rawTokenResults
const uniquePairTokenResults = uniq(pairTokenResults)
const tokenCalls = uniquePairTokenResults
.map((pairToken) => {
return [
{ address: pairToken, name: 'name' },
{ address: pairToken, name: 'symbol' },
]
})
.flat()
try {
rawTokenResults = await multicallv2({
abi: bep20Abi,
calls: tokenCalls,
options: { requireSuccess: false },
chainId: chainName === 'ETH' ? ChainId.ETHEREUM : ChainId.BSC,
})
} catch (error) {
console.info('Error fetching tokenIds from pair')
}
const tokenResults = rawTokenResults ? rawTokenResults.map((rawTokenResult) => rawTokenResult[0]) : []
const tokenMap = chunk(tokenResults, 2).reduce((acc, tokenResult, index) => {
return { ...acc, [uniquePairTokenResults[index].toLowerCase()]: { name: tokenResult[0], symbol: tokenResult[1] } }
}, {})
return mapValues(pairTokenMap, (pairTokenValue) => {
return {
token0: { id: pairTokenValue.token0, ...tokenMap[pairTokenValue.token0] },
token1: { id: pairTokenValue.token1, ...tokenMap[pairTokenValue.token1] },
}
})
}