|
| 1 | +import { zeroPad } from "@ethersproject/bytes"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import { task } from "hardhat/config"; |
| 4 | +import { HardhatRuntimeEnvironment } from "hardhat/types"; |
| 5 | + |
| 6 | +import { NETWORK_NAMES, getLzChainId } from "../constants"; |
| 7 | + |
| 8 | +interface ContractsJson { |
| 9 | + oft?: string; |
| 10 | + oftAdapter?: string; |
| 11 | + token?: string; |
| 12 | + endpoint?: string; |
| 13 | + [key: string]: string | undefined; |
| 14 | +} |
| 15 | + |
| 16 | +task("set-oftadapter-peer", "Sets peer for OFTAdapter contract").setAction( |
| 17 | + async (_, hre: HardhatRuntimeEnvironment) => { |
| 18 | + try { |
| 19 | + const network = hre.network.name; |
| 20 | + |
| 21 | + if (network !== NETWORK_NAMES.SEPOLIA) { |
| 22 | + throw new Error("This task must be run on Sepolia network"); |
| 23 | + } |
| 24 | + |
| 25 | + // Read contract addresses from both networks |
| 26 | + const sepoliaContracts = JSON.parse( |
| 27 | + fs.readFileSync(`contracts.${NETWORK_NAMES.SEPOLIA}.json`, "utf8"), |
| 28 | + ) as ContractsJson; |
| 29 | + |
| 30 | + const etherlinkContracts = JSON.parse( |
| 31 | + fs.readFileSync(`contracts.${NETWORK_NAMES.ETHERLINK}.json`, "utf8"), |
| 32 | + ) as ContractsJson; |
| 33 | + |
| 34 | + if (!sepoliaContracts.oftAdapter) { |
| 35 | + throw new Error("OFTAdapter contract not found on Sepolia"); |
| 36 | + } |
| 37 | + if (!etherlinkContracts.oft) { |
| 38 | + throw new Error("OFT contract not found on Etherlink"); |
| 39 | + } |
| 40 | + |
| 41 | + // Get the deployer account |
| 42 | + const [deployer] = await hre.ethers.getSigners(); |
| 43 | + console.log("Setting OFTAdapter peer with account:", deployer.address); |
| 44 | + |
| 45 | + // Get contract instance |
| 46 | + const MyOFTAdapter = await hre.ethers.getContractFactory("MyOFTAdapter"); |
| 47 | + const oftAdapter = MyOFTAdapter.attach(sepoliaContracts.oftAdapter); |
| 48 | + |
| 49 | + // Get Etherlink's EID |
| 50 | + const etherlinkEid = getLzChainId(NETWORK_NAMES.ETHERLINK); |
| 51 | + |
| 52 | + console.log("\nSetting peer for OFTAdapter contract:"); |
| 53 | + console.log("OFTAdapter address:", sepoliaContracts.oftAdapter); |
| 54 | + console.log("Etherlink EID:", etherlinkEid); |
| 55 | + console.log("OFT address:", etherlinkContracts.oft); |
| 56 | + |
| 57 | + // Set the peer |
| 58 | + const tx = await oftAdapter.setPeer(etherlinkEid, zeroPad(etherlinkContracts.oft, 32)); |
| 59 | + const receipt = await tx.wait(); |
| 60 | + |
| 61 | + console.log("\nPeer setup completed successfully"); |
| 62 | + console.log("Transaction hash:", receipt.hash); |
| 63 | + |
| 64 | + console.log("\nIMPORTANT: Make sure to also run set-oft-peer on Etherlink network"); |
| 65 | + console.log("npx hardhat set-oft-peer --network etherlink"); |
| 66 | + } catch (error) { |
| 67 | + console.error("Error setting OFTAdapter peer:", error); |
| 68 | + throw error; |
| 69 | + } |
| 70 | + }, |
| 71 | +); |
0 commit comments