import { DEFAULT_ASSET_DECIMAL } from '@config' ; import { Asset , ExtendedAsset , Network , StoreAsset , TAddress , TTicker } from '@types' ; import { generateAssetUUID , isSameAddress } from '@utils' ; export const getAssetByTicker = ( assets ) => ( ticker ) : Asset | undefined => { return assets . find ( ( asset ) => asset . ticker . toLowerCase ( ) === ticker . toLowerCase ( ) ) ; } ; export const getNewDefaultAssetTemplateByNetwork = ( assets ) => ( network ) : Asset => { const baseAssetOfNetwork = getAssetByUUID ( assets ) ( network . baseAsset ) ; if ( ! baseAssetOfNetwork ) { return { uuid : generateAssetUUID ( network . chainId ) , name : network . name , networkId : network . id , type : 'base' , ticker : assets . find ( ( a ) => a . uuid === network . baseAsset ) ! . ticker , decimal : DEFAULT_ASSET_DECIMAL } ; } else { return { uuid : baseAssetOfNetwork . uuid , name : baseAssetOfNetwork . name , networkId : baseAssetOfNetwork . networkId , type : 'base' , ticker : baseAssetOfNetwork . ticker , decimal : baseAssetOfNetwork . decimal } ; } } ; export const getAssetByUUID = ( assets ) => ( uuid ) : ExtendedAsset | undefined => { return assets . find ( ( asset ) => asset . uuid === uuid ) ; } ; export const getAssetByContractAndNetwork = ( contractAddress , network ) => ( assets ) : Asset | undefined => { if ( ! network || ! contractAddress ) { return undefined ; } return assets . filter ( ( asset ) => asset . networkId && asset . contractAddress && asset . networkId === network . id ) . find ( ( asset ) => asset . contractAddress ? isSameAddress ( asset . contractAddress as TAddress , contractAddress as TAddress ) : false ) ; } ; export const getTotalByAsset = ( assets ) => assets . reduce ( ( dict , asset ) => { const prev = dict [ asset . uuid ] ; if ( prev ) { dict [ asset . uuid ] = { ... prev , balance : prev . balance . add ( asset . balance ) } ; } else { dict [ asset . uuid ] = asset ; } return dict ; } , { } as { [ key ] : StoreAsset ; } ) ;