@hyperlane-xyz/sdk
Version:
The official SDK for the Hyperlane Network
87 lines • 3.93 kB
JavaScript
import { assert, deepFind, rootLogger, } from '@hyperlane-xyz/utils';
import { HookType, } from '../../hook/types.js';
import { IsmType } from '../types.js';
import { AggregationMetadataBuilder } from './aggregation.js';
import { ArbL2ToL1MetadataBuilder } from './arbL2ToL1.js';
import { OffchainLookupMetadataBuilder } from './ccipread.js';
import { decodeIsmMetadata } from './decode.js';
import { MultisigMetadataBuilder } from './multisig.js';
import { NullMetadataBuilder } from './null.js';
import { DynamicRoutingMetadataBuilder } from './routing.js';
export class BaseMetadataBuilder {
nullMetadataBuilder;
multisigMetadataBuilder;
aggregationMetadataBuilder;
routingMetadataBuilder;
arbL2ToL1MetadataBuilder;
ccipReadMetadataBuilder;
multiProvider;
logger = rootLogger.child({ module: 'BaseMetadataBuilder' });
constructor(core) {
this.multisigMetadataBuilder = new MultisigMetadataBuilder(core);
this.aggregationMetadataBuilder = new AggregationMetadataBuilder(this);
this.routingMetadataBuilder = new DynamicRoutingMetadataBuilder(this);
this.nullMetadataBuilder = new NullMetadataBuilder(core.multiProvider);
this.arbL2ToL1MetadataBuilder = new ArbL2ToL1MetadataBuilder(core);
this.ccipReadMetadataBuilder = new OffchainLookupMetadataBuilder(core);
this.multiProvider = core.multiProvider;
}
// assumes that all post dispatch hooks are included in dispatchTx logs
async build(context, maxDepth = 10) {
this.logger.debug({ context, maxDepth }, `Building ${context.ism.type} metadata`);
assert(maxDepth > 0, 'Max depth reached');
const { ism, hook } = context;
switch (ism.type) {
case IsmType.TRUSTED_RELAYER:
case IsmType.TEST_ISM:
case IsmType.OP_STACK:
case IsmType.PAUSABLE:
return this.nullMetadataBuilder.build({ ...context, ism });
case IsmType.MERKLE_ROOT_MULTISIG:
case IsmType.MESSAGE_ID_MULTISIG:
case IsmType.STORAGE_MERKLE_ROOT_MULTISIG:
case IsmType.STORAGE_MESSAGE_ID_MULTISIG:
if (typeof hook === 'string') {
throw new Error('Hook context must be an object (for multisig ISM)');
}
// eslint-disable-next-line no-case-declarations
const merkleTreeHook = deepFind(hook, (v) => v.type === HookType.MERKLE_TREE && !!v.address);
assert(merkleTreeHook, 'Merkle tree hook context not found');
return this.multisigMetadataBuilder.build({
...context,
ism,
hook: merkleTreeHook,
});
case IsmType.ROUTING:
case IsmType.FALLBACK_ROUTING:
case IsmType.AMOUNT_ROUTING:
case IsmType.INTERCHAIN_ACCOUNT_ROUTING:
return this.routingMetadataBuilder.build({
...context,
ism,
}, maxDepth);
case IsmType.AGGREGATION:
case IsmType.STORAGE_AGGREGATION:
return this.aggregationMetadataBuilder.build({ ...context, ism }, maxDepth);
case IsmType.ARB_L2_TO_L1: {
const hookConfig = hook;
return this.arbL2ToL1MetadataBuilder.build({
...context,
ism,
hook: hookConfig,
});
}
case IsmType.OFFCHAIN_LOOKUP:
return this.ccipReadMetadataBuilder.build({
...context,
ism,
});
default:
throw new Error(`Unsupported ISM: ${JSON.stringify(ism)}`);
}
}
static decode(metadata, context) {
return decodeIsmMetadata(metadata, context);
}
}
//# sourceMappingURL=builder.js.map