Fix SDK attachContractsMap bug (#2076)

### Description

attachContractsMap should filter out chains for which a complete set of
addresses is not present

### Drive-by changes

None
pull/2083/head
Asa Oines 2 years ago committed by GitHub
parent 864689a8a5
commit a4785f6592
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 31
      typescript/sdk/src/contracts.ts
  2. 2
      typescript/sdk/src/index.ts

@ -4,7 +4,7 @@ import type { types } from '@hyperlane-xyz/utils';
import { MultiProvider } from './providers/MultiProvider'; import { MultiProvider } from './providers/MultiProvider';
import { ChainMap, Connection } from './types'; import { ChainMap, Connection } from './types';
import { objMap, pick } from './utils/objects'; import { objFilter, objMap, pick } from './utils/objects';
export type HyperlaneFactories = { export type HyperlaneFactories = {
[key: string]: ethers.ContractFactory; [key: string]: ethers.ContractFactory;
@ -53,20 +53,30 @@ function getFactory(
return factories[key]; return factories[key];
} }
export function filterAddresses( export function filterAddressesMap(
addresses: HyperlaneAddresses<any>, addressesMap: HyperlaneAddressesMap<any>,
factories: HyperlaneFactories, factories: HyperlaneFactories,
): HyperlaneAddresses<any> { ): HyperlaneAddressesMap<typeof factories> {
return pick(addresses, Object.keys(factories)); 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<typeof factories> => {
return Object.keys(addresses).every((a) => factoryKeys.includes(a));
},
);
} }
export function attachContracts<F extends HyperlaneFactories>( export function attachContracts<F extends HyperlaneFactories>(
addresses: HyperlaneAddresses<F>, addresses: HyperlaneAddresses<F>,
factories: F, factories: F,
): HyperlaneContracts<F> { ): HyperlaneContracts<F> {
return objMap( return objMap(addresses, (key, address: types.Address) =>
filterAddresses(addresses, factories), getFactory(key, factories).attach(address),
(key, address: types.Address) => getFactory(key, factories).attach(address),
) as HyperlaneContracts<F>; ) as HyperlaneContracts<F>;
} }
@ -74,9 +84,10 @@ export function attachContractsMap<F extends HyperlaneFactories>(
addressesMap: HyperlaneAddressesMap<F>, addressesMap: HyperlaneAddressesMap<F>,
factories: F, factories: F,
): HyperlaneContractsMap<F> { ): HyperlaneContractsMap<F> {
return objMap(addressesMap, (_, addresses) => const filteredAddressesMap = filterAddressesMap(addressesMap, factories);
return objMap(filteredAddressesMap, (_, addresses) =>
attachContracts(addresses, factories), attachContracts(addresses, factories),
); ) as HyperlaneContractsMap<F>;
} }
export function connectContracts<F extends HyperlaneFactories>( export function connectContracts<F extends HyperlaneFactories>(

@ -36,7 +36,7 @@ export {
attachContractsMap, attachContractsMap,
connectContracts, connectContracts,
connectContractsMap, connectContractsMap,
filterAddresses, filterAddressesMap,
HyperlaneAddresses, HyperlaneAddresses,
HyperlaneAddressesMap, HyperlaneAddressesMap,
HyperlaneContracts, HyperlaneContracts,

Loading…
Cancel
Save