import { Address , Network , Networkish } from '@aragon/connect-types' ; import { ErrorInvalidNetwork } from '../errors' ; import { NETWORKS } from '../params' ; export function networkFromChainId ( chainId ) { return NETWORKS . find ( ( network ) => network . chainId === chainId ) || null ; } export function networkFromName ( name ) { return NETWORKS . find ( ( network ) => network . name === name ) || null ; } function networkFromObject ( { chainId , ensAddress , name , } ) { if ( name === undefined && chainId === undefined ) { throw new ErrorInvalidNetwork ( `Network: no name or chainId passed. ` + `Please provide at least one of these.` ) ; } if ( name !== undefined && chainId === undefined ) { chainId = networkFromName ( name ) ?. chainId ; if ( chainId === undefined ) { throw new ErrorInvalidNetwork ( `Network: invalid name provided: ${ name }. ` + `Please use provide a chainId or use one of the following names: ` + NETWORKS . map ( ( network ) => network . chainId ) . join ( ', ' ) + `.` ) ; } } chainId = chainId as number ; const chainIdNetwork = networkFromChainId ( chainId ) ; if ( ! chainIdNetwork ) { throw new ErrorInvalidNetwork ( `Network: invalid chainId provided: ${ chainId }. ` + `Please use one of the following: ` + NETWORKS . map ( ( network ) => network . chainId ) . join ( ', ' ) + `.` ) ; } if ( name === undefined ) { name = chainIdNetwork . name ; } if ( ! ensAddress ) { ensAddress = chainIdNetwork . ensAddress ; } return { chainId , ensAddress , name } ; } export function toNetwork ( network ) { if ( ! network ) { throw new ErrorInvalidNetwork ( `Network: incorrect value provided.` ) ; } if ( typeof network === 'string' ) { return networkFromObject ( { name : network } ) ; } if ( typeof network === 'number' ) { return networkFromObject ( { chainId : network } ) ; } return networkFromObject ( network ) ; }