|
| 1 | +import { |
| 2 | + Contract, |
| 3 | + PopulatedTransaction, |
| 4 | + constants as ethersConstants, |
| 5 | +} from 'ethers'; |
| 6 | + |
| 7 | +import { ERC20__factory } from '@hyperlane-xyz/core'; |
| 8 | +import { |
| 9 | + Address, |
| 10 | + Domain, |
| 11 | + addressToBytes32, |
| 12 | + strip0x, |
| 13 | +} from '@hyperlane-xyz/utils'; |
| 14 | + |
| 15 | +import { MultiProtocolProvider } from '../../providers/MultiProtocolProvider.js'; |
| 16 | +import { ChainName } from '../../types.js'; |
| 17 | + |
| 18 | +import { EvmTokenAdapter } from './EvmTokenAdapter.js'; |
| 19 | +import { |
| 20 | + IHypTokenAdapter, |
| 21 | + InterchainGasQuote, |
| 22 | + TransferRemoteParams, |
| 23 | +} from './ITokenAdapter.js'; |
| 24 | + |
| 25 | +/** |
| 26 | + * M0PortalLiteTokenAdapter - Adapter for M0 PortalLite token transfers |
| 27 | + * |
| 28 | + * This adapter extends EvmTokenAdapter for basic ERC20 operations and adds |
| 29 | + * support for cross-chain transfers via the M0 Portal. The Portal handles |
| 30 | + * bridging of M tokens (like mUSD) between chains. |
| 31 | + * |
| 32 | + * Key differences from standard ERC20: |
| 33 | + * - Approvals are made to the Portal contract (not the recipient) |
| 34 | + * - Cross-chain transfers use Portal's transferMLikeToken function |
| 35 | + * - M tokens use index-based accounting which can cause rounding |
| 36 | + */ |
| 37 | + |
| 38 | +// From https://github.com/m0-foundation/m-portal-lite/blob/main/src/Portal.sol |
| 39 | +const PORTAL_LITE_ABI = [ |
| 40 | + 'function transfer(uint256 amount, uint256 destinationChainId, address recipient, address refundAddress) external payable returns (bytes32)', |
| 41 | + 'function transferMLikeToken(uint256 amount, address sourceToken, uint256 destinationChainId, address destinationToken, address recipient, address refundAddress) external payable returns (bytes32)', |
| 42 | + 'function quoteTransfer(uint256 amount, uint256 destinationChainId, address recipient) external view returns (uint256)', |
| 43 | + 'function currentIndex() external view returns (uint128)', |
| 44 | + 'function mToken() external view returns (address)', |
| 45 | +]; |
| 46 | + |
| 47 | +export class M0PortalLiteTokenAdapter |
| 48 | + extends EvmTokenAdapter |
| 49 | + implements IHypTokenAdapter<PopulatedTransaction> |
| 50 | +{ |
| 51 | + public readonly portalContract: Contract; |
| 52 | + |
| 53 | + constructor( |
| 54 | + multiProvider: MultiProtocolProvider, |
| 55 | + chainName: ChainName, |
| 56 | + private readonly portalAddress: Address, |
| 57 | + mTokenAddress: Address, |
| 58 | + ) { |
| 59 | + // Initialize parent EvmTokenAdapter with the M token |
| 60 | + super(chainName, multiProvider, { token: mTokenAddress }, ERC20__factory); |
| 61 | + |
| 62 | + // Initialize the Portal contract for cross-chain transfers |
| 63 | + this.portalContract = new Contract( |
| 64 | + this.portalAddress, |
| 65 | + PORTAL_LITE_ABI, |
| 66 | + this.getProvider(), |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + // ========== ITokenAdapter overrides ========== |
| 71 | + |
| 72 | + override async getMinimumTransferAmount( |
| 73 | + _recipient: Address, |
| 74 | + ): Promise<bigint> { |
| 75 | + // M tokens use index-based accounting which can cause rounding |
| 76 | + // Return a small minimum to avoid rounding to 0 |
| 77 | + return 1n; |
| 78 | + } |
| 79 | + |
| 80 | + // ========== IHypTokenAdapter implementation ========== |
| 81 | + |
| 82 | + async getDomains(): Promise<Domain[]> { |
| 83 | + // This should be configured based on deployment |
| 84 | + // For now return empty - configuration will come from WarpCore config |
| 85 | + return []; |
| 86 | + } |
| 87 | + |
| 88 | + async getRouterAddress(_domain: Domain): Promise<Buffer> { |
| 89 | + // PortalLite doesn't use traditional routers |
| 90 | + // Return the portal address as the "router" |
| 91 | + return Buffer.from(strip0x(addressToBytes32(this.portalAddress)), 'hex'); |
| 92 | + } |
| 93 | + |
| 94 | + async getAllRouters(): Promise<Array<{ domain: Domain; address: Buffer }>> { |
| 95 | + return []; |
| 96 | + } |
| 97 | + |
| 98 | + async getBridgedSupply(): Promise<bigint | undefined> { |
| 99 | + // For simple transfer support, we don't need bridged supply tracking |
| 100 | + // WarpCore can work without this for basic transfers |
| 101 | + return undefined; |
| 102 | + } |
| 103 | + |
| 104 | + async quoteTransferRemoteGas( |
| 105 | + destination: Domain, |
| 106 | + sender?: Address, |
| 107 | + _customHook?: Address, |
| 108 | + ): Promise<InterchainGasQuote> { |
| 109 | + const destinationChainId = this.multiProvider.getChainId( |
| 110 | + this.multiProvider.getChainName(destination), |
| 111 | + ); |
| 112 | + |
| 113 | + // Use PortalLite's built-in gas estimation |
| 114 | + const gasQuote = await this.portalContract.quoteTransfer( |
| 115 | + 1n, // Amount doesn't affect gas quote |
| 116 | + destinationChainId, |
| 117 | + sender || ethersConstants.AddressZero, // Recipient doesn't affect quote |
| 118 | + ); |
| 119 | + |
| 120 | + return { |
| 121 | + amount: BigInt(gasQuote.toString()), |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + async populateTransferRemoteTx( |
| 126 | + params: TransferRemoteParams, |
| 127 | + ): Promise<PopulatedTransaction> { |
| 128 | + const destinationChainId = this.multiProvider.getChainId( |
| 129 | + this.multiProvider.getChainName(params.destination), |
| 130 | + ); |
| 131 | + |
| 132 | + // Get gas quote if not provided |
| 133 | + const gasQuote = |
| 134 | + params.interchainGas?.amount || |
| 135 | + ( |
| 136 | + await this.quoteTransferRemoteGas( |
| 137 | + params.destination, |
| 138 | + params.fromAccountOwner, |
| 139 | + ) |
| 140 | + ).amount; |
| 141 | + |
| 142 | + // Use Portal's transferMLikeToken function to support wrapped tokens like mUSD |
| 143 | + // Both source and destination use the same token address (mUSD on both chains) |
| 144 | + return this.portalContract.populateTransaction.transferMLikeToken( |
| 145 | + BigInt(params.weiAmountOrId.toString()), |
| 146 | + this.addresses.token, // source token |
| 147 | + destinationChainId, |
| 148 | + this.addresses.token, // destination token (same address on both chains for mUSD) |
| 149 | + params.recipient, |
| 150 | + params.fromAccountOwner || ethersConstants.AddressZero, // refundAddress |
| 151 | + { |
| 152 | + value: gasQuote, |
| 153 | + }, |
| 154 | + ); |
| 155 | + } |
| 156 | +} |
0 commit comments