Update buildAgentConfig for backwards compat (#2525)

- Change buildAgentConfig to create a combined new + old config schema
- Add unit tests for buildAgentConfig utils
pull/2532/head
J M Rossy 1 year ago committed by GitHub
parent f5e98a745c
commit 637172bb96
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      typescript/sdk/src/index.ts
  2. 40
      typescript/sdk/src/metadata/agentConfig.test.ts
  3. 75
      typescript/sdk/src/metadata/agentConfig.ts

@ -101,8 +101,10 @@ export {
AgentSigner,
ChainMetadataForAgent,
ChainMetadataForAgentSchema,
CombinedAgentConfig,
buildAgentConfig,
buildAgentConfigDeprecated,
buildAgentConfigNew,
} from './metadata/agentConfig';
export {
ChainMetadata,

@ -0,0 +1,40 @@
import { expect } from 'chai';
import { Chains } from '../consts/chains';
import { MultiProvider } from '../providers/MultiProvider';
import {
buildAgentConfig,
buildAgentConfigDeprecated,
buildAgentConfigNew,
} from './agentConfig';
describe('Agent config', () => {
const args: Parameters<typeof buildAgentConfig> = [
[Chains.ethereum],
new MultiProvider(),
{
ethereum: {
mailbox: '0xmailbox',
interchainGasPaymaster: '0xgas',
validatorAnnounce: '0xannounce',
},
},
{ ethereum: 0 },
];
it('Should generate a deprecated agent config', () => {
const result = buildAgentConfigDeprecated(...args);
expect(Object.keys(result)).to.deep.equal(['chains']);
});
it('Should generate a new agent config', () => {
const result = buildAgentConfigNew(...args);
expect(Object.keys(result)).to.deep.equal([Chains.ethereum]);
});
it('Should generate a combined agent config', () => {
const result = buildAgentConfig(...args);
expect(Object.keys(result)).to.deep.equal([Chains.ethereum, 'chains']);
});
});

@ -54,30 +54,6 @@ export const ChainMetadataForAgentSchema =
export type ChainMetadataForAgent = z.infer<typeof ChainMetadataForAgentSchema>;
export function buildAgentConfig(
chains: ChainName[],
multiProvider: MultiProvider,
addresses: ChainMap<HyperlaneDeploymentArtifacts>,
startBlocks: ChainMap<number>,
): ChainMap<ChainMetadataForAgent> {
const configs: ChainMap<ChainMetadataForAgent> = {};
for (const chain of [...chains].sort()) {
const metadata = multiProvider.getChainMetadata(chain);
const config: ChainMetadataForAgent = {
...metadata,
rpcConsensusType: AgentConnectionType.HttpFallback,
mailbox: addresses[chain].mailbox,
interchainGasPaymaster: addresses[chain].interchainGasPaymaster,
validatorAnnounce: addresses[chain].validatorAnnounce,
index: {
from: startBlocks[chain],
},
};
configs[chain] = config;
}
return configs;
}
/**
* Deprecated agent config shapes.
* See https://github.com/hyperlane-xyz/hyperlane-monorepo/issues/2215
@ -118,6 +94,36 @@ export interface AgentConfig {
};
}
/**
* Utilities for generating agent configs from metadata / artifacts.
*/
// Returns the new agent config shape that extends ChainMetadata
export function buildAgentConfigNew(
chains: ChainName[],
multiProvider: MultiProvider,
addresses: ChainMap<HyperlaneDeploymentArtifacts>,
startBlocks: ChainMap<number>,
): ChainMap<ChainMetadataForAgent> {
const configs: ChainMap<ChainMetadataForAgent> = {};
for (const chain of [...chains].sort()) {
const metadata = multiProvider.getChainMetadata(chain);
const config: ChainMetadataForAgent = {
...metadata,
rpcConsensusType: AgentConnectionType.HttpFallback,
mailbox: addresses[chain].mailbox,
interchainGasPaymaster: addresses[chain].interchainGasPaymaster,
validatorAnnounce: addresses[chain].validatorAnnounce,
index: {
from: startBlocks[chain],
},
};
configs[chain] = config;
}
return configs;
}
// Returns the current (but deprecated) agent config shape.
export function buildAgentConfigDeprecated(
chains: ChainName[],
multiProvider: MultiProvider,
@ -150,3 +156,24 @@ export function buildAgentConfigDeprecated(
}
return agentConfig;
}
// For compat with the older agent config shape, we return a combination
// of the two schemas (ChainMap<ChainMetadataForAgent> & AgentConfig).
export type CombinedAgentConfig = ChainMap<ChainMetadataForAgent> | AgentConfig;
export function buildAgentConfig(
chains: ChainName[],
multiProvider: MultiProvider,
addresses: ChainMap<HyperlaneDeploymentArtifacts>,
startBlocks: ChainMap<number>,
): CombinedAgentConfig {
return {
...buildAgentConfigNew(chains, multiProvider, addresses, startBlocks),
...buildAgentConfigDeprecated(
chains,
multiProvider,
addresses,
startBlocks,
),
};
}

Loading…
Cancel
Save