import { currentTimestamp } from './../../utils/index' ; import { updateTokenData , addTokenKeys , addPoolAddresses , updateChartData , updatePriceData , updateTransactions , } from './actions' ; import { createReducer } from '@reduxjs/toolkit' ; import { PriceChartEntry , Transaction } from 'types' ; import { SupportedNetwork } from 'constants/networks' ; export type TokenData = { exists : boolean ; name : string ; symbol : string ; address : string ; volumeUSD : number ; volumeUSDChange : number ; volumeUSDWeek : number ; txCount : number ; feesUSD : number ; tvlToken : number ; tvlUSD : number ; tvlUSDChange : number ; priceUSD : number ; priceUSDChange : number ; priceUSDChangeWeek : number ; } ; export interface TokenChartEntry { date : number ; volumeUSD : number ; totalValueLockedUSD : number ; } export interface TokensState { byAddress : { [ networkId ] : { [ address ] : { data : TokenData | undefined ; poolAddresses : string [ ] | undefined ; chartData : TokenChartEntry [ ] | undefined ; priceData : { oldestFetchedTimestamp ? : number | undefined ; [ secondsInterval ] : PriceChartEntry [ ] | undefined ; } ; transactions : Transaction [ ] | undefined ; lastUpdated : number | undefined ; } ; } ; } ; } export const initialState = { byAddress : { [ SupportedNetwork . ETHEREUM ] : { } , [ SupportedNetwork . ARBITRUM ] : { } , [ SupportedNetwork . OPTIMISM ] : { } , [ SupportedNetwork . POLYGON ] : { } , } , } ; export default createReducer ( initialState , ( builder ) => builder . addCase ( updateTokenData , ( state , { payload : { tokens , networkId } } ) => { tokens . map ( ( tokenData ) => ( state . byAddress [ networkId ] [ tokenData . address ] = { ... state . byAddress [ networkId ] [ tokenData . address ] , data : tokenData , lastUpdated : currentTimestamp ( ) , } ) ) ; } ) . addCase ( addTokenKeys , ( state , { payload : { tokenAddresses , networkId } } ) => { tokenAddresses . map ( ( address ) => { if ( ! state . byAddress [ networkId ] [ address ] ) { state . byAddress [ networkId ] [ address ] = { poolAddresses : undefined , data : undefined , chartData : undefined , priceData : { } , transactions : undefined , lastUpdated : undefined , } ; } } ) ; } ) . addCase ( addPoolAddresses , ( state , { payload : { tokenAddress , poolAddresses , networkId } } ) => { state . byAddress [ networkId ] [ tokenAddress ] = { ... state . byAddress [ networkId ] [ tokenAddress ] , poolAddresses } ; } ) . addCase ( updateChartData , ( state , { payload : { tokenAddress , chartData , networkId } } ) => { state . byAddress [ networkId ] [ tokenAddress ] = { ... state . byAddress [ networkId ] [ tokenAddress ] , chartData } ; } ) . addCase ( updateTransactions , ( state , { payload : { tokenAddress , transactions , networkId } } ) => { state . byAddress [ networkId ] [ tokenAddress ] = { ... state . byAddress [ networkId ] [ tokenAddress ] , transactions } ; } ) . addCase ( updatePriceData , ( state , { payload : { tokenAddress , secondsInterval , priceData , oldestFetchedTimestamp , networkId } } ) => { state . byAddress [ networkId ] [ tokenAddress ] = { ... state . byAddress [ networkId ] [ tokenAddress ] , priceData : { ... state . byAddress [ networkId ] [ tokenAddress ] . priceData , [ secondsInterval ] : priceData , oldestFetchedTimestamp , } , } ; } ) ) ;