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 { 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<any>,
export function filterAddressesMap(
addressesMap: HyperlaneAddressesMap<any>,
factories: HyperlaneFactories,
): HyperlaneAddresses<any> {
return pick(addresses, Object.keys(factories));
): HyperlaneAddressesMap<typeof 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>(
addresses: HyperlaneAddresses<F>,
factories: F,
): HyperlaneContracts<F> {
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<F>;
}
@ -74,9 +84,10 @@ export function attachContractsMap<F extends HyperlaneFactories>(
addressesMap: HyperlaneAddressesMap<F>,
factories: F,
): HyperlaneContractsMap<F> {
return objMap(addressesMap, (_, addresses) =>
const filteredAddressesMap = filterAddressesMap(addressesMap, factories);
return objMap(filteredAddressesMap, (_, addresses) =>
attachContracts(addresses, factories),
);
) as HyperlaneContractsMap<F>;
}
export function connectContracts<F extends HyperlaneFactories>(

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

Loading…
Cancel
Save