import allPass from 'ramda/src/allPass' ; import curry from 'ramda/src/curry' ; import find from 'ramda/src/find' ; import map from 'ramda/src/map' ; import prop from 'ramda/src/prop' ; import propEq from 'ramda/src/propEq' ; import uniq from 'ramda/src/uniq' ; import values from 'ramda/src/values' ; import { Asset , AssetBalanceObject , IAccount , LocalStorage , NetworkId , TTicker , TUuid } from '@types' ; export function migrate ( prev , curr ) { const getAssetByTickerAndNetworkID = ( assets , networkId , ticker ) => find ( allPass ( [ propEq ( 'ticker' , ticker ) , propEq ( 'networkId' , networkId ) ] ) , values ( assets ) ) ; const updateAccountAssetsUUID = ( { networkId , assets = [ ] , ... rest } ) => { const getTicker = ( uuid ) => { const asset = prev . assets [ uuid ] || { } ; return asset && asset . ticker ? asset . ticker : undefined ; } ; const getUUID = ( ticker ) => { const asset = curry ( getAssetByTickerAndNetworkID ) ( curr . assets ) ( networkId ) ( ticker ) ; return prop ( 'uuid' , asset ) ; } ; const updateUUID = ( assetBalance ) => ( { ... assetBalance , uuid : getUUID ( getTicker ( assetBalance . uuid ) ) } ) ; return { ... rest , networkId , assets : map ( updateUUID , assets ) } ; } ; const accounts = Object . assign ( { } , curr . accounts , map ( updateAccountAssetsUUID , ( prev . accounts as R . Functor < IAccount > ) || { } ) ) ; const { dashboardAccounts = [ ] } = prev . settings ; const accountUUIDs = map ( prop ( 'uuid' ) , values ( accounts ) ) ; const settings = { ... curr . settings , dashboardAccounts : uniq ( [ ... accountUUIDs , ... dashboardAccounts ] ) . filter ( Boolean ) } ; return Object . assign ( { } , curr , { accounts , settings } ) ; }