import { DEFAULT_ASSET_DECIMAL , Fiats } from '@config' ; import { Asset , AssetLegacy , ContractLegacy , ExtendedAsset , ExtendedContract , Fiat , LocalStorage , LSKeys , Network , NetworkId , NetworkLegacy , NodeOptions , NodeType } from '@types' ; import { generateAssetUUID , generateDeterministicAddressUUID } from '@utils/generateUUID' ; import { chain , filter , map , mapObjIndexed , mergeRight , pipe , reduce } from '@vendor' ; import { NetworkConfig , NETWORKS_CONFIG , NODES_CONFIG } from './data' ; import { add , toArray , toObject } from './helpers' ; import { StoreAction } from './types' ; const addNetworks = add ( LSKeys . NETWORKS ) ( ( networks ) => { const formatNetwork = ( n ) : Network => { const baseAssetUuid = generateAssetUUID ( n . chainId ) ; const nodes = [ ... ( NODES_CONFIG [ n . id ] || [ ] ) , ... ( n . nodes || [ ] ) ] ; const { unit , ... rest } = n ; return { ... rest , baseAsset : baseAssetUuid , baseUnit : unit , nodes : nodes . filter ( ( { type } ) => type !== NodeType . WEB3 ) } ; } ; return mapObjIndexed ( formatNetwork , networks ) ; } ) ; const addContracts = add ( LSKeys . CONTRACTS ) ( ( networks , store ) => { const formatContract = ( id ) => ( c ) : ExtendedContract => ( { uuid : c . uuid || generateDeterministicAddressUUID ( id , c . address ) , name : c . name , address : c . address , abi : c . abi , networkId : id , isCustom : c . isCustom } ) ; return pipe ( map ( ( { id , contracts } ) => ( { id , contracts } ) ) , filter ( ( { contracts } ) => contracts ) , chain ( ( { id , contracts } ) : ExtendedAsset [ ] => contracts . map ( formatContract ( id ) ) ) , reduce ( toObject ( 'uuid' ) , { } as any ) , mergeRight ( store . contracts ) ) ( toArray ( networks ) ) ; } ) ; const addContractsToNetworks = add ( LSKeys . NETWORKS ) ( ( _ , store ) => { const getNetworkContracts = ( n ) => { const nContracts = filter ( ( c ) => c . networkId === n . id , store . contracts ) ; return { ... n , contracts : toArray ( nContracts ) . map ( ( c ) => c . uuid ) } ; } ; return mapObjIndexed ( getNetworkContracts , store . networks ) ; } ) ; const addBaseAssetsToAssets = add ( LSKeys . ASSETS ) ( ( _ , store ) => { const formatAsset = ( n ) : Asset => ( { uuid : n . baseAsset , ticker : n . baseUnit , name : n . baseUnitName ? n . baseUnitName : n . name , networkId : n . id , type : 'base' , decimal : DEFAULT_ASSET_DECIMAL , isCustom : n . isCustom } ) ; return pipe ( toArray , map ( formatAsset ) , reduce ( ( acc , curr ) => ( { ... acc , [ curr . uuid ] : curr } ) , { } ) , mergeRight ( store . assets ) ) ( store . networks ) ; } ) ; const addFiatsToAssets = add ( LSKeys . ASSETS ) ( ( fiats , store ) => { const formatFiat = ( { ticker , name } ) : ExtendedAsset => ( { uuid : generateAssetUUID ( ticker , name ) , name , ticker , networkId : 'OldWorld' as NetworkId , type : 'fiat' , decimal : 0 } ) ; return pipe ( map ( formatFiat ) , reduce ( ( acc , curr ) => ( { ... acc , [ curr . uuid ] : curr } ) , { } ) , mergeRight ( store . assets ) ) ( fiats ) ; } ) ; const addTokensToAssets = add ( LSKeys . ASSETS ) ( ( networks , store ) => { const formatToken = ( id ) => ( a ) : ExtendedAsset => ( { uuid : a . uuid || generateAssetUUID ( id ) , name : a . name , decimal : a . decimal , ticker : a . ticker , networkId : id , contractAddress : a . address , type : 'erc20' , isCustom : a . isCustom } ) ; return pipe ( map ( ( { id , tokens } ) => ( { id , tokens } ) ) , filter ( ( { tokens } ) => tokens ) , chain ( ( { id , tokens } ) : ExtendedAsset [ ] => tokens . map ( formatToken ( id ) ) ) , reduce ( toObject ( 'uuid' ) , { } as any ) , mergeRight ( store . assets ) ) ( toArray ( networks ) ) ; } ) ; const getDefaultTransducers = ( networkConfig ) : StoreAction [ ] => [ addNetworks ( networkConfig ) , addContracts ( networkConfig ) , addContractsToNetworks ( ) , addBaseAssetsToAssets ( ) , addFiatsToAssets ( toArray ( Fiats ) ) , addTokensToAssets ( networkConfig ) ] ; type Transduce = ( z : LocalStorage , networkConfig : NetworkConfig ) => LocalStorage ; export const createDefaultValues = ( initialSchema , networkConfig ) => { return pipe ( ... getDefaultTransducers ( networkConfig ) ) ( initialSchema ) ; } ;