|
| 1 | +import { |
| 2 | + ChainMetadataForAltVM, |
| 3 | + getProtocolProvider, |
| 4 | +} from '@hyperlane-xyz/provider-sdk'; |
| 5 | +import { ISigner } from '@hyperlane-xyz/provider-sdk/altvm'; |
| 6 | +import { |
| 7 | + ArtifactNew, |
| 8 | + ArtifactWriter, |
| 9 | +} from '@hyperlane-xyz/provider-sdk/artifact'; |
| 10 | +import { |
| 11 | + DeployedFeeAddress, |
| 12 | + DeployedFeeArtifact, |
| 13 | + FeeArtifactConfig, |
| 14 | + FeeReadContext, |
| 15 | + IRawFeeArtifactManager, |
| 16 | +} from '@hyperlane-xyz/provider-sdk/fee'; |
| 17 | +import { AnnotatedTx, TxReceipt } from '@hyperlane-xyz/provider-sdk/module'; |
| 18 | + |
| 19 | +import { FeeReader } from './fee-reader.js'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Factory function to create a FeeWriter instance. |
| 23 | + * Returns null if the protocol does not support fee programs. |
| 24 | + * |
| 25 | + * @param chainMetadata Chain metadata for the target chain |
| 26 | + * @param signer Signer interface for signing transactions |
| 27 | + * @param context Required fee read context with domains and routers to check |
| 28 | + * @returns A FeeWriter instance, or null if the protocol does not support fees |
| 29 | + */ |
| 30 | +export function createFeeWriter( |
| 31 | + chainMetadata: ChainMetadataForAltVM, |
| 32 | + signer: ISigner<AnnotatedTx, TxReceipt>, |
| 33 | + context: FeeReadContext, |
| 34 | +): FeeWriter | null { |
| 35 | + const protocolProvider = getProtocolProvider(chainMetadata.protocol); |
| 36 | + const artifactManager: IRawFeeArtifactManager | null = |
| 37 | + protocolProvider.createFeeArtifactManager(chainMetadata); |
| 38 | + |
| 39 | + if (!artifactManager) { |
| 40 | + return null; |
| 41 | + } |
| 42 | + |
| 43 | + return new FeeWriter(artifactManager, context, signer); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * FeeWriter handles creation and updates of fee configurations using the Artifact API. |
| 48 | + * It delegates to protocol-specific artifact writers for individual fee types. |
| 49 | + * |
| 50 | + * Extends FeeReader to inherit read() functionality. |
| 51 | + * The FeeReadContext is required at construction time to ensure the reader always |
| 52 | + * knows which domains and routers to check (some fee contracts are not enumerable). |
| 53 | + */ |
| 54 | +export class FeeWriter |
| 55 | + extends FeeReader |
| 56 | + implements ArtifactWriter<FeeArtifactConfig, DeployedFeeAddress> |
| 57 | +{ |
| 58 | + constructor( |
| 59 | + artifactManager: IRawFeeArtifactManager, |
| 60 | + context: FeeReadContext, |
| 61 | + private readonly signer: ISigner<AnnotatedTx, TxReceipt>, |
| 62 | + ) { |
| 63 | + super(artifactManager, context); |
| 64 | + } |
| 65 | + |
| 66 | + async create( |
| 67 | + artifact: ArtifactNew<FeeArtifactConfig>, |
| 68 | + ): Promise<[DeployedFeeArtifact, TxReceipt[]]> { |
| 69 | + const { config } = artifact; |
| 70 | + const writer = this.artifactManager.createWriter(config.type, this.signer); |
| 71 | + return writer.create(artifact); |
| 72 | + } |
| 73 | + |
| 74 | + async update(artifact: DeployedFeeArtifact): Promise<AnnotatedTx[]> { |
| 75 | + const { config } = artifact; |
| 76 | + const writer = this.artifactManager.createWriter(config.type, this.signer); |
| 77 | + return writer.update(artifact); |
| 78 | + } |
| 79 | +} |
0 commit comments