UNPKG

@hyperlane-xyz/sdk

Version:

The official SDK for the Hyperlane Network

75 lines 3.51 kB
import { HyperlaneDeployer, } from '../deploy/HyperlaneDeployer.js'; import { EvmTokenFeeReader } from './EvmTokenFeeReader.js'; import { evmTokenFeeFactories } from './contracts.js'; import { TokenFeeConfigSchema, TokenFeeType, onChainTypeToTokenFeeTypeMap, } from './types.js'; export class EvmTokenFeeDeployer extends HyperlaneDeployer { multiProvider; chain; tokenFeeReader; constructor(multiProvider, chain, options = {}) { super(multiProvider, evmTokenFeeFactories, options); this.multiProvider = multiProvider; this.chain = chain; this.tokenFeeReader = new EvmTokenFeeReader(multiProvider, chain); } async deployContracts(chain, config) { const deployedContract = {}; // This is a partial HyperlaneContracts<EvmTokenFeeFactories> const parsedConfig = TokenFeeConfigSchema.parse(config); switch (parsedConfig.type) { case TokenFeeType.LinearFee: case TokenFeeType.ProgressiveFee: case TokenFeeType.RegressiveFee: deployedContract[parsedConfig.type] = await this.deployFee(chain, parsedConfig); break; case TokenFeeType.RoutingFee: { // Return the routing fee and all the child fee contracts const routingFeeResult = await this.deployRoutingFee(chain, parsedConfig); deployedContract[TokenFeeType.RoutingFee] = routingFeeResult.routingFee; for (const [_, contract] of Object.entries(routingFeeResult.subFeeContracts)) { const onchainFeeType = await contract.feeType(); const feeType = onChainTypeToTokenFeeTypeMap[onchainFeeType]; deployedContract[feeType] = contract; } break; } } return deployedContract; } async deployFee(chain, config) { let { maxFee, halfAmount } = config; if (config.type === TokenFeeType.LinearFee && config.bps && (!maxFee || !halfAmount)) { const { maxFee: calculatedMaxFee, halfAmount: calculatedHalfAmount } = await this.tokenFeeReader.convertFromBps(config.bps, config.token); maxFee = calculatedMaxFee; halfAmount = calculatedHalfAmount; } return this.deployContract(chain, config.type, [ config.token, maxFee, halfAmount, config.owner, ]); } async deployRoutingFee(chain, config) { if (config.type !== TokenFeeType.RoutingFee) { throw new Error('Invalid config type for routing fee deployment'); } // Deploy the routing fee contract const routingFee = await this.deployContract(chain, TokenFeeType.RoutingFee, [config.token, config.owner]); const subFeeContracts = {}; if (config.feeContracts) { // Deploy each fee contract & set each fee for the routing fee for (const [destinationChain, feeConfig] of Object.entries(config.feeContracts)) { const deployedFeeContract = await this.deployFee(chain, feeConfig); await routingFee.setFeeContract(this.multiProvider.getChainId(destinationChain), deployedFeeContract.address); subFeeContracts[destinationChain] = deployedFeeContract; } } return { routingFee, subFeeContracts, }; } } //# sourceMappingURL=EvmTokenFeeDeployer.js.map