|
| 1 | +import { |
| 2 | + type ArtifactReader, |
| 3 | + type ArtifactWriter, |
| 4 | +} from '@hyperlane-xyz/provider-sdk/artifact'; |
| 5 | +import { |
| 6 | + type DeployedMailboxAddress, |
| 7 | + type IRawMailboxArtifactManager, |
| 8 | + type MailboxType, |
| 9 | + type RawMailboxArtifactConfigs, |
| 10 | +} from '@hyperlane-xyz/provider-sdk/mailbox'; |
| 11 | +import { assert } from '@hyperlane-xyz/utils'; |
| 12 | + |
| 13 | +import { type AnyAleoNetworkClient } from '../clients/base.js'; |
| 14 | +import { type AleoSigner } from '../clients/signer.js'; |
| 15 | +import { type AleoArtifactNetworkConfig } from '../utils/types.js'; |
| 16 | + |
| 17 | +import { AleoMailboxReader, AleoMailboxWriter } from './mailbox.js'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Aleo Mailbox Artifact Manager implementing IRawMailboxArtifactManager. |
| 21 | + * |
| 22 | + * This manager: |
| 23 | + * - Provides factory methods for creating mailbox readers and writers |
| 24 | + * - Handles mailbox deployment and configuration |
| 25 | + */ |
| 26 | +export class AleoMailboxArtifactManager implements IRawMailboxArtifactManager { |
| 27 | + constructor( |
| 28 | + private readonly config: AleoArtifactNetworkConfig, |
| 29 | + private readonly aleoClient: AnyAleoNetworkClient, |
| 30 | + ) {} |
| 31 | + |
| 32 | + async readMailbox(address: string) { |
| 33 | + const reader = this.createReader('mailbox'); |
| 34 | + return reader.read(address); |
| 35 | + } |
| 36 | + |
| 37 | + createReader<T extends MailboxType>( |
| 38 | + type: T, |
| 39 | + ): ArtifactReader<RawMailboxArtifactConfigs[T], DeployedMailboxAddress> { |
| 40 | + const readers: { |
| 41 | + [K in MailboxType]: () => ArtifactReader< |
| 42 | + RawMailboxArtifactConfigs[K], |
| 43 | + DeployedMailboxAddress |
| 44 | + >; |
| 45 | + } = { |
| 46 | + mailbox: () => new AleoMailboxReader(this.aleoClient), |
| 47 | + }; |
| 48 | + |
| 49 | + assert(readers[type], `Mailbox reader for ${type} not found`); |
| 50 | + return readers[type](); |
| 51 | + } |
| 52 | + |
| 53 | + createWriter<T extends MailboxType>( |
| 54 | + type: T, |
| 55 | + signer: AleoSigner, |
| 56 | + ): ArtifactWriter<RawMailboxArtifactConfigs[T], DeployedMailboxAddress> { |
| 57 | + const writers: { |
| 58 | + [K in MailboxType]: () => ArtifactWriter< |
| 59 | + RawMailboxArtifactConfigs[K], |
| 60 | + DeployedMailboxAddress |
| 61 | + >; |
| 62 | + } = { |
| 63 | + mailbox: () => |
| 64 | + new AleoMailboxWriter(this.config, this.aleoClient, signer), |
| 65 | + }; |
| 66 | + |
| 67 | + assert(writers[type], `Mailbox writer for ${type} not found`); |
| 68 | + return writers[type](); |
| 69 | + } |
| 70 | +} |
0 commit comments