import { createContract as createAContract , destroyContract , selectContracts , useDispatch , useSelector } from '@store' ; import { Contract , ExtendedContract , NetworkId , TAddress , TUuid } from '@types' ; import { generateDeterministicAddressUUID , isSameAddress } from '@utils' ; function useContracts ( ) { const contracts = useSelector ( selectContracts ) ; const dispatch = useDispatch ( ) ; param contract const createContract = ( contract ) : ExtendedContract => { const uuid = generateDeterministicAddressUUID ( contract . networkId , contract . address ) ; dispatch ( createAContract ( { ... contract , uuid } ) ) ; return { ... contract , uuid } ; } ; const deleteContract = ( uuid ) => { dispatch ( destroyContract ( uuid ) ) ; } ; const getContractsByIds = ( uuids ) => uuids . map ( ( contractId ) => contracts . find ( ( c ) => c . uuid === contractId ) ! ) . filter ( Boolean ) ; const getContractByAddress = ( address ) => contracts . find ( ( x ) => isSameAddress ( x . address , address ) ) ; const getContractsByNetwork = ( networkId ) => contracts . filter ( ( c ) => c . networkId === networkId ) ; return { contracts , createContract , deleteContract , getContractsByIds , getContractByAddress , getContractsByNetwork } ; } export default useContracts ;