@@ -4,16 +4,16 @@ import { Trade, TradeReversion, EventWithBlockInfo } from 'api/exchange/Exchange
4
4
5
5
import { Actions } from 'reducers-actions'
6
6
7
- import { logDebug , flattenMapOfLists , dateToBatchId , toBN } from 'utils'
7
+ import { logDebug , flattenMapOfLists , dateToBatchId , toBN , setStorageItem } from 'utils'
8
8
import { TRADES_LOCAL_STORAGE_KEY } from 'const'
9
9
10
10
// ******** TYPES/INTERFACES
11
11
12
12
export type ActionTypes = 'OVERWRITE_TRADES' | 'APPEND_TRADES' | 'UPDATE_BLOCK'
13
13
14
- export type TradesState = Record < number , TradesStateSingleNetwork >
14
+ export type TradesState = Record < string , TradesStatePerAccount >
15
15
16
- interface TradesStateSingleNetwork {
16
+ interface TradesStatePerAccount {
17
17
trades : Trade [ ]
18
18
pendingTrades : Map < string , Trade [ ] >
19
19
lastCheckedBlock ?: number
@@ -23,27 +23,28 @@ interface WithReverts {
23
23
reverts : TradeReversion [ ]
24
24
}
25
25
26
- interface WithNetworkId {
26
+ interface WithAccountInfo {
27
27
networkId : number
28
+ userAddress : string
28
29
}
29
30
30
31
// ******** ACTION TYPES
31
32
32
33
type OverwriteTradesActionType = Actions <
33
34
'OVERWRITE_TRADES' ,
34
- Omit < TradesStateSingleNetwork , 'pendingTrades' > & WithReverts & WithNetworkId
35
+ Omit < TradesStatePerAccount , 'pendingTrades' > & WithReverts & WithAccountInfo
35
36
>
36
37
type AppendTradesActionType = Actions <
37
38
'APPEND_TRADES' ,
38
- Required < Omit < TradesStateSingleNetwork , 'pendingTrades' > > & WithReverts & WithNetworkId
39
+ Required < Omit < TradesStatePerAccount , 'pendingTrades' > > & WithReverts & WithAccountInfo
39
40
>
40
41
type UpdateBlockActionType = Actions <
41
42
'UPDATE_BLOCK' ,
42
- Required < Pick < TradesStateSingleNetwork , 'lastCheckedBlock' > > & WithNetworkId
43
+ Required < Pick < TradesStatePerAccount , 'lastCheckedBlock' > > & WithAccountInfo
43
44
>
44
- type ReducerActionType = Actions < ActionTypes , TradesStateSingleNetwork & WithReverts & WithNetworkId >
45
+ type ReducerActionType = Actions < ActionTypes , TradesStatePerAccount & WithReverts & WithAccountInfo >
45
46
46
- interface Params extends WithNetworkId {
47
+ interface Params extends WithAccountInfo {
47
48
trades : Trade [ ]
48
49
reverts : TradeReversion [ ]
49
50
lastCheckedBlock ?: number
@@ -61,11 +62,17 @@ export const appendTrades = (params: Required<Params>): AppendTradesActionType =
61
62
payload : params ,
62
63
} )
63
64
64
- export const updateLastCheckedBlock = ( lastCheckedBlock : number , networkId : number ) : UpdateBlockActionType => ( {
65
+ export const updateLastCheckedBlock = (
66
+ params : Required < Pick < Params , 'lastCheckedBlock' > > & WithAccountInfo ,
67
+ ) : UpdateBlockActionType => ( {
65
68
type : 'UPDATE_BLOCK' ,
66
- payload : { lastCheckedBlock , networkId } ,
69
+ payload : params ,
67
70
} )
68
71
72
+ export function buildAccountKey ( { networkId, userAddress } : WithAccountInfo ) : string {
73
+ return networkId + '|' + userAddress
74
+ }
75
+
69
76
function buildTradeRevertKey ( batchId : number , orderId : string ) : string {
70
77
return batchId + '|' + orderId
71
78
}
@@ -186,55 +193,6 @@ function applyRevertsToTrades(
186
193
return [ flattenMapOfLists ( tradesByRevertKey ) , getPendingTrades ( tradesByRevertKey ) ]
187
194
}
188
195
189
- // ******** REDUCER
190
-
191
- export const reducer = ( state : TradesState , action : ReducerActionType ) : TradesState => {
192
- switch ( action . type ) {
193
- case 'APPEND_TRADES' : {
194
- const { trades : newTrades , reverts, lastCheckedBlock, networkId } = action . payload
195
- const { trades : currTrades , pendingTrades : currPendingTrades } = state [ networkId ]
196
-
197
- const [ trades , pendingTrades ] = applyRevertsToTrades ( newTrades , reverts , currPendingTrades )
198
-
199
- return { ...state , [ networkId ] : { trades : currTrades . concat ( trades ) , lastCheckedBlock, pendingTrades } }
200
- }
201
- case 'OVERWRITE_TRADES' : {
202
- const { trades : newTrades , reverts, lastCheckedBlock, networkId } = action . payload
203
-
204
- const [ trades , pendingTrades ] = applyRevertsToTrades ( newTrades , reverts )
205
-
206
- return { ...state , [ networkId ] : { trades, lastCheckedBlock, pendingTrades } }
207
- }
208
- case 'UPDATE_BLOCK' : {
209
- const { networkId, lastCheckedBlock } = action . payload
210
-
211
- return { ...state , [ networkId ] : { ...state [ networkId ] , lastCheckedBlock } }
212
- }
213
- default : {
214
- return state
215
- }
216
- }
217
- }
218
-
219
- // TODO: use the one from David once his changes are merged https://github.com/gnosis/dex-react/pull/1091
220
- function setStorageItem ( key : string , data : unknown ) : void {
221
- // localStorage API accepts only strings
222
- // TODO: consider switching to localForage API (accepts all types)
223
- const formattedData = JSON . stringify ( data )
224
- return localStorage . setItem ( key , formattedData )
225
- }
226
-
227
- // ******** SIDE EFFECT
228
-
229
- export async function sideEffect ( state : TradesState , action : ReducerActionType ) : Promise < void > {
230
- switch ( action . type ) {
231
- case 'APPEND_TRADES' :
232
- case 'OVERWRITE_TRADES' :
233
- case 'UPDATE_BLOCK' :
234
- setStorageItem ( TRADES_LOCAL_STORAGE_KEY , state )
235
- }
236
- }
237
-
238
196
// ******** INITIAL STATE / LOCAL STORAGE
239
197
240
198
const INITIAL_TRADES_STATE_SINGLE_NETWORK = { trades : [ ] , pendingTrades : new Map < string , Trade [ ] > ( ) }
@@ -264,7 +222,7 @@ function reviver(key: string, value: unknown): unknown {
264
222
}
265
223
266
224
function loadInitialState ( ) : TradesState {
267
- let state = { 1 : INITIAL_TRADES_STATE_SINGLE_NETWORK , 4 : INITIAL_TRADES_STATE_SINGLE_NETWORK }
225
+ let state = { }
268
226
269
227
const localStorageOrders = localStorage . getItem ( TRADES_LOCAL_STORAGE_KEY )
270
228
@@ -280,3 +238,50 @@ function loadInitialState(): TradesState {
280
238
}
281
239
282
240
export const initialState = loadInitialState ( )
241
+
242
+ // ******** REDUCER
243
+
244
+ export const reducer = ( state : TradesState , action : ReducerActionType ) : TradesState => {
245
+ switch ( action . type ) {
246
+ case 'APPEND_TRADES' : {
247
+ const { trades : newTrades , reverts, lastCheckedBlock, networkId, userAddress } = action . payload
248
+
249
+ const accountKey = buildAccountKey ( { networkId, userAddress } )
250
+
251
+ const { trades : currTrades , pendingTrades : currPendingTrades } =
252
+ state [ accountKey ] || INITIAL_TRADES_STATE_SINGLE_NETWORK
253
+
254
+ const [ trades , pendingTrades ] = applyRevertsToTrades ( newTrades , reverts , currPendingTrades )
255
+
256
+ return { ...state , [ accountKey ] : { trades : currTrades . concat ( trades ) , lastCheckedBlock, pendingTrades } }
257
+ }
258
+ case 'OVERWRITE_TRADES' : {
259
+ const { trades : newTrades , reverts, lastCheckedBlock, networkId, userAddress } = action . payload
260
+
261
+ const accountKey = buildAccountKey ( { networkId, userAddress } )
262
+
263
+ const [ trades , pendingTrades ] = applyRevertsToTrades ( newTrades , reverts )
264
+
265
+ return { ...state , [ accountKey ] : { trades, lastCheckedBlock, pendingTrades } }
266
+ }
267
+ case 'UPDATE_BLOCK' : {
268
+ const { networkId, lastCheckedBlock } = action . payload
269
+
270
+ return { ...state , [ networkId ] : { ...state [ networkId ] , lastCheckedBlock } }
271
+ }
272
+ default : {
273
+ return state
274
+ }
275
+ }
276
+ }
277
+
278
+ // ******** SIDE EFFECT
279
+
280
+ export async function sideEffect ( state : TradesState , action : ReducerActionType ) : Promise < void > {
281
+ switch ( action . type ) {
282
+ case 'APPEND_TRADES' :
283
+ case 'OVERWRITE_TRADES' :
284
+ case 'UPDATE_BLOCK' :
285
+ setStorageItem ( TRADES_LOCAL_STORAGE_KEY , state )
286
+ }
287
+ }
0 commit comments