# hardhat部署合约时,获取合约工厂存在的问题 **Published by:** [石小龙](https://paragraph.com/@0x994cd38723375a0eb0d55fcbc1e2a862fd49d007/) **Published on:** 2025-03-12 **URL:** https://paragraph.com/@0x994cd38723375a0eb0d55fcbc1e2a862fd49d007/hardhat-2 ## Content 获取合约工厂的代码如下 await ethers.getContractFactor 该方法中支持两种传参方式: 1、直接传入合约名称 const contract = await ethers.getContractFactory("MockV3Aggregator") 2、传入合约的具体地址+合约名称 const contract = await ethers.getContractFactory("contracts/MockV3Aggregator.sol:MockV3Aggregator2") 两种传参方式分别存在两种潜在的问题 1、直接传入合约名称 如果contracts目录下存在多个同名的合约,那么此时会报错,就必须通过具体路径来定位到合约 2、传入合约的具体地址+合约名称 如果该合约不是自定义的,而是直接导入的,那么该方式不支持,会直接报错,比如: const contract = await ethers.getContractFactory("contracts/MockV3Aggregator.sol:MockV3Aggregator") 由于合约 MockV3Aggregator 是通过代码导入进来的,这种就不被支持 import "@chainlink/contracts/src/v0.8/tests/MockV3Aggregator.sol"; 目标合约:MockV3Aggregator ## Publication Information - [石小龙](https://paragraph.com/@0x994cd38723375a0eb0d55fcbc1e2a862fd49d007/): Publication homepage - [All Posts](https://paragraph.com/@0x994cd38723375a0eb0d55fcbc1e2a862fd49d007/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@0x994cd38723375a0eb0d55fcbc1e2a862fd49d007): Subscribe to updates