# hardhat部署合约时，获取合约工厂存在的问题

By [石小龙](https://paragraph.com/@0x994cd38723375a0eb0d55fcbc1e2a862fd49d007) · 2025-03-12

---

*   获取合约工厂的代码如下
    
        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](https://storage.googleapis.com/papyrus_images/081d7f1b7ed4da7e35dd748a63764a3b6b531958e57d6ee1ac58d15d58a05c5e.png)

目标合约：MockV3Aggregator

---

*Originally published on [石小龙](https://paragraph.com/@0x994cd38723375a0eb0d55fcbc1e2a862fd49d007/hardhat-2)*
