首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >获取ethers.js部署脚本中的错误

获取ethers.js部署脚本中的错误
EN

Ethereum用户
提问于 2023-02-05 18:22:04
回答 1查看 73关注 0票数 1

我的代码是

代码语言:javascript
复制
const path = require("path")
const { ethers } = require("@nomiclabs/hardhat-ethers")
const fs = require("fs")
require("dotenv").config()

module.exports = async () => {
    const byteCode =
        "0x6080604052348015600f57600080fd5b506004361060285760003560e01c806367e0badb14602d575b600080fd5b60336047565b604051603e91906067565b60405180910390f35b60008054905090565b6000819050919050565b6061816050565b82525050565b6000602082019050607a6000830184605a565b9291505056fea2646970667358221220bf589d7b8b1882fd287f7f8bf62502781345ff7839575c8bf210dfe6eeb2e48864736f6c63430008110033"

    const getTheAbiOfTest = () => {
        try {
            const dir = path.resolve(
                "/home/abinash/Desktop/DEX2/artifacts/Test.sol/Test.json"
            )
            const file = fs.readFileSync(dir, "utf8")
            const json = JSON.parse(file)
            const abi = json.abi

            return abi
            console.log(`the abi: ${abi}`)
        } catch (e) {
            console.log(`error ${e}`)
        }
    }

    //const [_abi, _byteCode] = getTheAbiOfTest()
    const provider = new ethers.providers.JsonRpcProvider(
        process.env.JSON_RPC_PROVIDER
    )
    const signer = await provider.getSigner()

    const contractFactory = await ethers.ContractFactory(byteCode,getTheAbiOfTest() , signer)
    console.log(`contractFactory is ${contractFactory}`)

    const testContract = await contractFactory.deploy()
    console.log(`deployed`)
    console.log(`address of the testContract is : ${testContract.address}`)

    await testContract.deployTransaction.wait()

    console.log(`connecting to the contract....`)

    const test = new ethers.Contract(
        testContract.address,
        getTheAbiOfTest(),
        signer
    )
    console.log(`address of the test instance ${test.address}`)

    const num = await test.getNum()
    console.log(`num is: ${num}`)
}

当我运行hh deploy --network goerli时,我会得到这个错误-

代码语言:javascript
复制
Error: ERROR processing /home/abinash/Desktop/DEX2/deploy/deploy.js:
TypeError: Cannot read properties of undefined (reading 'providers')
    at Object.module.exports [as func] (/home/abinash/Desktop/DEX2/deploy/deploy.js:111:30)
    at DeploymentsManager.executeDeployScripts (/home/abinash/Desktop/DEX2/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1219:41)
    at DeploymentsManager.runDeploy (/home/abinash/Desktop/DEX2/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1052:16)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at SimpleTaskDefinition.action (/home/abinash/Desktop/DEX2/node_modules/hardhat-deploy/src/index.ts:438:5)
    at Environment._runTaskDefinition (/home/abinash/Desktop/DEX2/node_modules/hardhat/src/internal/core/runtime-environment.ts:311:14)
    at Environment.run (/home/abinash/Desktop/DEX2/node_modules/hardhat/src/internal/core/runtime-environment.ts:159:14)
    at SimpleTaskDefinition.action (/home/abinash/Desktop/DEX2/node_modules/hardhat-deploy/src/index.ts:584:32)
    at Environment._runTaskDefinition (/home/abinash/Desktop/DEX2/node_modules/hardhat/src/internal/core/runtime-environment.ts:311:14)
    at Environment.run (/home/abinash/Desktop/DEX2/node_modules/hardhat/src/internal/core/runtime-environment.ts:159:14)
    at DeploymentsManager.executeDeployScripts (/home/abinash/Desktop/DEX2/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1222:19)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
    at DeploymentsManager.runDeploy (/home/abinash/Desktop/DEX2/node_modules/hardhat-deploy/src/DeploymentsManager.ts:1052:5)
    at SimpleTaskDefinition.action (/home/abinash/Desktop/DEX2/node_modules/hardhat-deploy/src/index.ts:438:5)
    at Environment._runTaskDefinition (/home/abinash/Desktop/DEX2/node_modules/hardhat/src/internal/core/runtime-environment.ts:311:14)
    at Environment.run (/home/abinash/Desktop/DEX2/node_modules/hardhat/src/internal/core/runtime-environment.ts:159:14)
    at SimpleTaskDefinition.action (/home/abinash/Desktop/DEX2/node_modules/hardhat-deploy/src/index.ts:584:32)
    at Environment._runTaskDefinition (/home/abinash/Desktop/DEX2/node_modules/hardhat/src/internal/core/runtime-environment.ts:311:14)
    at Environment.run (/home/abinash/Desktop/DEX2/node_modules/hardhat/src/internal/core/runtime-environment.ts:159:14)
    at SimpleTaskDefinition.action (/home/abinash/Desktop/DEX2/node_modules/hardhat-deploy/src/index.ts:669:5)

我为什么要犯这个错误??

EN

回答 1

Ethereum用户

发布于 2023-02-05 19:51:01

如果您想要与以太一起部署到testnet,则需要传递私钥来实例化部署签名者:

代码语言:javascript
复制
import * as hre from 'hardhat';

const provider = hre.ethers.provider;
const privKey = process.env['PRIVATE_KEY'] as BytesLike;
const deployer_wallet = new Wallet(privKey);
const deployer = await deployer_wallet.connect(provider);

希望它能帮上忙

票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/144324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档