-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathindex.ts
108 lines (106 loc) · 3.74 KB
/
index.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
/* eslint-disable no-param-reassign */
import { createReducer } from '@reduxjs/toolkit'
import { InfoState } from './types'
import {
updateProtocolData,
updateProtocolChartData,
updateProtocolTransactions,
updatePoolData,
addPoolKeys,
updatePoolChartData,
updatePoolTransactions,
updateTokenData,
addTokenKeys,
addTokenPoolAddresses,
updateTokenChartData,
updateTokenPriceData,
updateTokenTransactions,
} from './actions'
const initialState: InfoState = {
protocol: {},
pools: { byAddress: {} },
tokens: { byAddress: {} },
}
export default createReducer(initialState, (builder) =>
builder
// Protocol actions
.addCase(updateProtocolData, (state, { payload: { protocolData } }) => {
state.protocol.overview = protocolData
})
.addCase(updateProtocolChartData, (state, { payload: { chartData } }) => {
state.protocol.chartData = chartData
})
.addCase(updateProtocolTransactions, (state, { payload: { transactions } }) => {
state.protocol.transactions = transactions
})
// Pools actions
.addCase(updatePoolData, (state, { payload: { pools } }) => {
pools.forEach((poolData) => {
state.pools.byAddress[poolData.address] = {
...state.pools.byAddress[poolData.address],
data: poolData,
}
})
})
.addCase(addPoolKeys, (state, { payload: { poolAddresses } }) => {
poolAddresses.forEach((address) => {
if (!state.pools.byAddress[address]) {
state.pools.byAddress[address] = {
data: undefined,
chartData: undefined,
transactions: undefined,
}
}
})
})
.addCase(updatePoolChartData, (state, { payload: { poolAddress, chartData } }) => {
state.pools.byAddress[poolAddress] = { ...state.pools.byAddress[poolAddress], chartData }
})
.addCase(updatePoolTransactions, (state, { payload: { poolAddress, transactions } }) => {
state.pools.byAddress[poolAddress] = { ...state.pools.byAddress[poolAddress], transactions }
})
// Tokens actions
.addCase(updateTokenData, (state, { payload: { tokens } }) => {
tokens.forEach((tokenData) => {
state.tokens.byAddress[tokenData.address] = {
...state.tokens.byAddress[tokenData.address],
data: tokenData,
}
})
})
.addCase(addTokenKeys, (state, { payload: { tokenAddresses } }) => {
tokenAddresses.forEach((address) => {
if (!state.tokens.byAddress[address]) {
state.tokens.byAddress[address] = {
poolAddresses: undefined,
data: undefined,
chartData: undefined,
priceData: {},
transactions: undefined,
}
}
})
})
.addCase(addTokenPoolAddresses, (state, { payload: { tokenAddress, poolAddresses } }) => {
state.tokens.byAddress[tokenAddress] = { ...state.tokens.byAddress[tokenAddress], poolAddresses }
})
.addCase(updateTokenChartData, (state, { payload: { tokenAddress, chartData } }) => {
state.tokens.byAddress[tokenAddress] = { ...state.tokens.byAddress[tokenAddress], chartData }
})
.addCase(updateTokenTransactions, (state, { payload: { tokenAddress, transactions } }) => {
state.tokens.byAddress[tokenAddress] = { ...state.tokens.byAddress[tokenAddress], transactions }
})
.addCase(
updateTokenPriceData,
(state, { payload: { tokenAddress, secondsInterval, priceData, oldestFetchedTimestamp } }) => {
state.tokens.byAddress[tokenAddress] = {
...state.tokens.byAddress[tokenAddress],
priceData: {
...state.tokens.byAddress[tokenAddress]?.priceData,
[secondsInterval]: priceData,
oldestFetchedTimestamp,
},
}
},
),
)