diff --git a/typescript/sdk/src/contracts.ts b/typescript/sdk/src/contracts.ts index 7fb096c7d..8115cac08 100644 --- a/typescript/sdk/src/contracts.ts +++ b/typescript/sdk/src/contracts.ts @@ -4,7 +4,7 @@ import type { types } from '@hyperlane-xyz/utils'; import { MultiProvider } from './providers/MultiProvider'; import { ChainMap, Connection } from './types'; -import { objMap, pick } from './utils/objects'; +import { objFilter, objMap, pick } from './utils/objects'; export type HyperlaneFactories = { [key: string]: ethers.ContractFactory; @@ -53,20 +53,30 @@ function getFactory( return factories[key]; } -export function filterAddresses( - addresses: HyperlaneAddresses, +export function filterAddressesMap( + addressesMap: HyperlaneAddressesMap, factories: HyperlaneFactories, -): HyperlaneAddresses { - return pick(addresses, Object.keys(factories)); +): HyperlaneAddressesMap { + const factoryKeys = Object.keys(factories); + // Filter out addresses that we do not have factories for + const pickedAddressesMap = objMap(addressesMap, (_, addresses) => + pick(addresses, factoryKeys), + ); + // Filter out chains for which we do not have a complete set of addresses + return objFilter( + pickedAddressesMap, + (_, addresses): addresses is HyperlaneAddresses => { + return Object.keys(addresses).every((a) => factoryKeys.includes(a)); + }, + ); } export function attachContracts( addresses: HyperlaneAddresses, factories: F, ): HyperlaneContracts { - return objMap( - filterAddresses(addresses, factories), - (key, address: types.Address) => getFactory(key, factories).attach(address), + return objMap(addresses, (key, address: types.Address) => + getFactory(key, factories).attach(address), ) as HyperlaneContracts; } @@ -74,9 +84,10 @@ export function attachContractsMap( addressesMap: HyperlaneAddressesMap, factories: F, ): HyperlaneContractsMap { - return objMap(addressesMap, (_, addresses) => + const filteredAddressesMap = filterAddressesMap(addressesMap, factories); + return objMap(filteredAddressesMap, (_, addresses) => attachContracts(addresses, factories), - ); + ) as HyperlaneContractsMap; } export function connectContracts( diff --git a/typescript/sdk/src/index.ts b/typescript/sdk/src/index.ts index ee430fdb7..cf1c87e8b 100644 --- a/typescript/sdk/src/index.ts +++ b/typescript/sdk/src/index.ts @@ -36,7 +36,7 @@ export { attachContractsMap, connectContracts, connectContractsMap, - filterAddresses, + filterAddressesMap, HyperlaneAddresses, HyperlaneAddressesMap, HyperlaneContracts,