|
| 1 | +import assert from 'assert' |
| 2 | + |
| 3 | +import { bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes' |
| 4 | +import { BigNumber } from 'ethers' |
| 5 | +import { BytesLike, parseUnits } from 'ethers/lib/utils' |
| 6 | +import { HardhatRuntimeEnvironment } from 'hardhat/types' |
| 7 | + |
| 8 | +import { makeBytes32 } from '@layerzerolabs/devtools' |
| 9 | +import { createGetHreByEid } from '@layerzerolabs/devtools-evm-hardhat' |
| 10 | +import { ChainType, EndpointId, endpointIdToChainType, endpointIdToNetwork } from '@layerzerolabs/lz-definitions' |
| 11 | +import { Options } from '@layerzerolabs/lz-v2-utilities' |
| 12 | + |
| 13 | +import layerzeroConfig from '../../../layerzero.config' |
| 14 | +import { SendResult } from '../../common/types' |
| 15 | +import { DebugLogger, KnownErrors } from '../../common/utils' |
| 16 | +import { getLayerZeroScanLink } from '../../solana' |
| 17 | + |
| 18 | +export interface EvmArgs { |
| 19 | + srcEid: number |
| 20 | + dstEid: number |
| 21 | + amount: string |
| 22 | + to: string |
| 23 | + oftAddress?: string |
| 24 | +} |
| 25 | + |
| 26 | +const PT_SEND = 0 |
| 27 | +const GAS_LIMIT_SOLANA = 200_000 // Gas limit for the executor when sending to Solana |
| 28 | +const MSG_VALUE_SOLANA = 2_500_000 // For why this is necessary, see: https://docs.layerzero.network/v2/developers/solana/oft/account#setting-enforced-options-inbound-to-solana |
| 29 | + |
| 30 | +const GAS_LIMIT_DEFAULT = 80_000 // Gas limit for the executor when sending to EVM / Aptos |
| 31 | +const MSG_VALUE_DEFAULT = 0 // No msg.value needed for EVM / Aptos |
| 32 | + |
| 33 | +export async function sendEvm( |
| 34 | + { srcEid, dstEid, amount, to, oftAddress }: EvmArgs, |
| 35 | + hre: HardhatRuntimeEnvironment |
| 36 | +): Promise<SendResult> { |
| 37 | + if (endpointIdToChainType(srcEid) !== ChainType.EVM) { |
| 38 | + throw new Error(`non-EVM srcEid (${srcEid}) not supported here`) |
| 39 | + } |
| 40 | + |
| 41 | + const getHreByEid = createGetHreByEid(hre) |
| 42 | + let srcEidHre: HardhatRuntimeEnvironment |
| 43 | + try { |
| 44 | + srcEidHre = await getHreByEid(srcEid) |
| 45 | + } catch (error) { |
| 46 | + DebugLogger.printErrorAndFixSuggestion( |
| 47 | + KnownErrors.ERROR_GETTING_HRE, |
| 48 | + `For network: ${endpointIdToNetwork(srcEid)}, OFT: ${oftAddress}` |
| 49 | + ) |
| 50 | + throw error |
| 51 | + } |
| 52 | + |
| 53 | + const { deployer } = await srcEidHre.getNamedAccounts() |
| 54 | + const signer = await srcEidHre.ethers.getSigner(deployer) // getNamedSigner is not available in v1, so we use getSigner with deployer account |
| 55 | + |
| 56 | + // 1️⃣ resolve the OFT wrapper address |
| 57 | + let wrapperAddress: string |
| 58 | + if (oftAddress) { |
| 59 | + wrapperAddress = oftAddress |
| 60 | + } else { |
| 61 | + const { contracts } = layerzeroConfig // not using simple config generator, so no need to check if type is function |
| 62 | + const wrapper = contracts.find((c) => c.contract.eid === srcEid) |
| 63 | + if (!wrapper) throw new Error(`No config for EID ${srcEid}`) |
| 64 | + wrapperAddress = wrapper.contract.contractName |
| 65 | + ? (await srcEidHre.deployments.get(wrapper.contract.contractName)).address |
| 66 | + : wrapper.contract.address! |
| 67 | + } |
| 68 | + |
| 69 | + // 2️⃣ load MyEndpointV1OFTV2Mock ABI, extend it with token() |
| 70 | + const ioftArtifact = await srcEidHre.artifacts.readArtifact('MyEndpointV1OFTV2Mock') |
| 71 | + |
| 72 | + // now attach |
| 73 | + const oft = await srcEidHre.ethers.getContractAt(ioftArtifact.abi, wrapperAddress, signer) |
| 74 | + |
| 75 | + // 3️⃣ fetch the underlying ERC-20 |
| 76 | + const underlying = await oft.token() |
| 77 | + |
| 78 | + // 4️⃣ fetch decimals from the underlying token |
| 79 | + const erc20 = await srcEidHre.ethers.getContractAt('ERC20', underlying, signer) |
| 80 | + const decimals: number = await erc20.decimals() |
| 81 | + |
| 82 | + // 5️⃣ normalize the user-supplied amount |
| 83 | + const amountUnits: BigNumber = parseUnits(amount, decimals) |
| 84 | + |
| 85 | + const minDstGas: BigNumber = await oft.minDstGasLookup(dstEid, PT_SEND) // 0 = send, 1 = send_and_call |
| 86 | + |
| 87 | + assert( |
| 88 | + minDstGas.gt(0), |
| 89 | + "minDstGas must be a non-0 value to bypass gas assertion part of EndpointV1. Ensure you have called 'npx hardhat lz:epv1:set-min-dst-gas' for the destination eid" |
| 90 | + ) |
| 91 | + |
| 92 | + // Decide how to configure chain-specific values and encode `to` |
| 93 | + const dstChain = endpointIdToChainType(dstEid) |
| 94 | + let toBytes: string |
| 95 | + let MSG_VALUE: number |
| 96 | + let GAS_LIMIT: number |
| 97 | + |
| 98 | + if (dstChain === ChainType.SOLANA) { |
| 99 | + // 1️⃣ Validate & encode Base58 → 32-byte buffer |
| 100 | + try { |
| 101 | + toBytes = makeBytes32(bs58.decode(to)) |
| 102 | + } catch { |
| 103 | + throw new Error(`Invalid Solana address: not valid Base58`) |
| 104 | + } |
| 105 | + // 2️⃣ Solana-specific fee settings |
| 106 | + MSG_VALUE = MSG_VALUE_SOLANA |
| 107 | + GAS_LIMIT = GAS_LIMIT_SOLANA |
| 108 | + } else { |
| 109 | + // 1️⃣ Validate & encode hex (EVM, Move, Hyperliquid, etc.) → 32-byte buffer |
| 110 | + if (!/^0x[0-9a-fA-F]{40}$/.test(to)) { |
| 111 | + throw new Error(`Invalid address: expected 0x-prefixed 40 hex chars`) |
| 112 | + } |
| 113 | + toBytes = makeBytes32(to) |
| 114 | + // 2️⃣ Non-Solana fee settings |
| 115 | + MSG_VALUE = MSG_VALUE_DEFAULT |
| 116 | + GAS_LIMIT = GAS_LIMIT_DEFAULT |
| 117 | + } |
| 118 | + |
| 119 | + // 6️⃣ send |
| 120 | + const _options = Options.newOptions().addExecutorLzReceiveOption(GAS_LIMIT, MSG_VALUE) |
| 121 | + const adapterParams: BytesLike = _options.toBytes() |
| 122 | + |
| 123 | + const fees = await oft.estimateSendFee(dstEid, toBytes, amount, false, adapterParams) |
| 124 | + console.log(`fees[0] (wei): ${fees[0]} / (eth): ${hre.ethers.utils.formatEther(fees[0])}`) |
| 125 | + const tx = await oft.sendFrom( |
| 126 | + signer.address, // 'from' address to send tokens |
| 127 | + dstEid, // remote LayerZero chainId |
| 128 | + toBytes, // 'to' address to send tokens |
| 129 | + amountUnits, // amount of tokens to send (in wei) |
| 130 | + { |
| 131 | + refundAddress: signer.address, |
| 132 | + zroPaymentAddress: hre.ethers.constants.AddressZero, |
| 133 | + adapterParams: _options.toBytes(), // as workaround for EndpointV1 OFT -> OFT202, we specify options type 3 instead of adapter params |
| 134 | + }, |
| 135 | + { value: fees[0] } |
| 136 | + ) |
| 137 | + const receipt = await tx.wait() |
| 138 | + const scanLink = getLayerZeroScanLink(receipt.transactionHash, srcEid === EndpointId.SOLANA_V2_TESTNET) |
| 139 | + |
| 140 | + return { |
| 141 | + txHash: receipt.transactionHash, |
| 142 | + scanLink, |
| 143 | + } |
| 144 | +} |
0 commit comments