Skip to content

Commit

Permalink
use descriminated union to improve branching and remove error handlin…
Browse files Browse the repository at this point in the history
…g (zod-support next)
  • Loading branch information
nbayindirli committed May 10, 2024
1 parent 3d204af commit c424ca3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 36 deletions.
41 changes: 14 additions & 27 deletions typescript/cli/src/submit/submit.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
EV5GnosisSafeTxSubmitter,
EV5GnosisSafeTxSubmitterProps,
EV5ImpersonatedAccountTxSubmitter,
EV5ImpersonatedAccountTxSubmitterProps,
EV5InterchainAccountTxTransformer,
EV5JsonRpcTxSubmitter,
MultiProvider,
Expand All @@ -10,7 +12,7 @@ import {
TxTransformerInterface,
TxTransformerType,
} from '@hyperlane-xyz/sdk';
import { ProtocolType, assert } from '@hyperlane-xyz/utils';
import { ProtocolType } from '@hyperlane-xyz/utils';

import {
SubmitterBuilderSettings,
Expand All @@ -37,33 +39,23 @@ export async function getSubmitterBuilder<TProtocol extends ProtocolType>({

async function getSubmitter<TProtocol extends ProtocolType>(
multiProvider: MultiProvider,
{ type, settings }: SubmitterMetadata,
submitterMetadata: SubmitterMetadata,
): Promise<TxSubmitterInterface<TProtocol>> {
switch (type) {
switch (submitterMetadata.type) {
case TxSubmitterType.JSON_RPC:
return new EV5JsonRpcTxSubmitter(multiProvider);
case TxSubmitterType.IMPERSONATED_ACCOUNT:
assert(
settings,
'Must provide EV5ImpersonatedAccountTxSubmitterProps for impersonated account submitter.',
);

return new EV5ImpersonatedAccountTxSubmitter(
multiProvider,
settings.eV5ImpersonatedAccountProps,
submitterMetadata.props as EV5ImpersonatedAccountTxSubmitterProps,
);
case TxSubmitterType.GNOSIS_SAFE:
assert(
settings,
'Must provide EV5GnosisSafeTxSubmitterProps for Gnosis safe submitter.',
);

return new EV5GnosisSafeTxSubmitter(
multiProvider,
settings.eV5GnosisSafeProps,
submitterMetadata.props as EV5GnosisSafeTxSubmitterProps,
);
default:
throw new Error(`Invalid TxSubmitterType: ${type}`);
throw new Error(`Invalid TxSubmitterType: ${submitterMetadata.type}`);
}
}

Expand All @@ -72,28 +64,23 @@ async function getTransformers<TProtocol extends ProtocolType>(
metadata: TransformerMetadata[],
): Promise<TxTransformerInterface<TProtocol>[]> {
return Promise.all(
metadata.map(({ type, settings }) =>
getTransformer<TProtocol>(multiProvider, { type, settings }),
metadata.map(({ type, props: settings }) =>
getTransformer<TProtocol>(multiProvider, { type, props: settings }),
),
);
}

async function getTransformer<TProtocol extends ProtocolType>(
multiProvider: MultiProvider,
{ type, settings }: TransformerMetadata,
transformerMetadata: TransformerMetadata,
): Promise<TxTransformerInterface<TProtocol>> {
switch (type) {
switch (transformerMetadata.type) {
case TxTransformerType.ICA:
assert(
settings,
'Must provide EV5InterchainAccountTxTransformerProps for ICA transformer.',
);

return new EV5InterchainAccountTxTransformer(
multiProvider,
settings.eV5InterchainAccountProps,
transformerMetadata.props,
);
default:
throw new Error(`Invalid TxTransformerType: ${type}`);
throw new Error(`Invalid TxTransformerType: ${transformerMetadata.type}`);
}
}
15 changes: 6 additions & 9 deletions typescript/cli/src/submit/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,14 @@ export interface SubmitterBuilderSettings {
}
export interface SubmitterMetadata {
type: TxSubmitterType;
settings?: SubmitterSettings;
props: SubmitterProps;
}
export interface TransformerMetadata {
type: TxTransformerType;
settings?: TransformerSettings;
props: TransformerProps;
}

interface SubmitterSettings {
eV5GnosisSafeProps: EV5GnosisSafeTxSubmitterProps;
eV5ImpersonatedAccountProps: EV5ImpersonatedAccountTxSubmitterProps;
}
interface TransformerSettings {
eV5InterchainAccountProps: EV5InterchainAccountTxTransformerProps;
}
type SubmitterProps =
| EV5ImpersonatedAccountTxSubmitterProps
| EV5GnosisSafeTxSubmitterProps;
type TransformerProps = EV5InterchainAccountTxTransformerProps;

0 comments on commit c424ca3

Please sign in to comment.