PatrickAlphaC / ethers-simple-storage-fcc

86 stars 124 forks source link

ethers.JsonRpcProvider is not a constructor #93

Open JohnAnderson5 opened 1 year ago

JohnAnderson5 commented 1 year ago

When I run deploy.js in lesson 5, I get this error.

today988 commented 1 year ago

When I run deploy.js in lesson 5, I get this error.

TypeError: Cannot read properties of undefined (reading 'JsonRpcProvider') at main (/root/folder/ethers-simple-storage/deploy.js:7:41) at Object. (/root/folder/ethers-simple-storage/deploy.js:25:1) at Module._compile (node:internal/modules/cjs/loader:1233:14) at Module._extensions..js (node:internal/modules/cjs/loader:1287:10) at Module.load (node:internal/modules/cjs/loader:1091:32) at Module._load (node:internal/modules/cjs/loader:938:12) at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) at node:internal/main/run_main_module:23:47

kangaroona commented 12 months ago

You can debug ethers,I think you have a wrong ethers object. It's my code ,depend on "ethers": "^6.2.3"

const ethers = require("ethers")
// const solc = require("solc")
const fs = require("fs-extra")
require("dotenv").config()

async function main() {
    // First, compile this!
    // And make sure to have your ganache network up!
    // The old way can be seen below:
    // let provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL)
    // On ether 6 and above, you should use like this
    console.log(process.env.PRIVATE_KEY)
    let provider = new ethers.JsonRpcProvider(process.env.RPC_URL)
    // let provider = new ethers.providers.JsonRpcProvider(process.env.RPC_URL)
    let wallet = new ethers.Wallet(process.env.PRIVATE_KEY, provider)
    // const encryptedJson = fs.readFileSync("./encryptedKey.json", "utf8")
    // let wallet = new ethers.Wallet.fromEncryptedJsonSync(
    //     encryptedJson,
    //     process.env.PRIVATE_KEY_PASSWORD
    // )
    // wallet = wallet.connect(provider)
    const abi = fs.readFileSync("./SimpleStorage_sol_SimpleStorage.abi", "utf8")
    const binary = fs.readFileSync(
        "./SimpleStorage_sol_SimpleStorage.bin",
        "utf8"
    )
    const contractFactory = new ethers.ContractFactory(abi, binary, wallet)
    console.log("Deploying, please wait...")
    const nonce = await wallet.getNonce()
    // const contract = await contractFactory.deploy({ nonce: nonce })
    const contract = await contractFactory.deploy()
    console.log("🚀 ~ file: deploy.js:31 ~ main ~ contract:", contract)
    // console.log(`Contract deployed to ${JSON.stringify(contract)}`)
    // const contract = await contractFactory.deploy({ gasPrice: 20000000000 })
    // const deploymentReceipt = await contract.deployTransaction.wait(1)
    const deploymentReceipt = await contract.deploymentTransaction().wait(1)
    console.log(`Contract deployed to ${await contract.getAddress()}`)
    console.log(
        `Contract deployed at address: ${deploymentReceipt.contractAddress}`
    )
    // console.log("Here is the transaction:")
    // console.log(contract.deployTransaction)
    // console.log("Here is the receipt:")
    // console.log(deploymentReceipt)
    // const nonce = await wallet.getTransactionCount()

    let currentFavoriteNumber = await contract.retrieve()
    console.log(`Current Favorite Number: ${currentFavoriteNumber}`)
    console.log("Updating favorite number...")
    let transactionResponse = await contract.store(7)
    let transactionReceipt = await transactionResponse.wait()
    currentFavoriteNumber = await contract.retrieve()
    console.log(`New Favorite Number: ${currentFavoriteNumber}`)
}

main()
    .then(() => process.exit(0))
    .catch((error) => {
        console.error(error)
        process.exit(1)
    })

// synchronous [solidity]
// asynchronous [javascript]

// cooking
// Synchronous
// 1. Put popcorn in microwave -> Promise
// 2. Wait for popcorn to finish
// 3. Pour drinks for everyone

// Asynchronous
// 1. Put popcorn in the mircrowave
// 2. Pour drinks for everyone
// 3. Wait for popcorn to finish

// Promise
// Pending
// Fulfilled
// Rejected
TARishabh commented 7 months ago

This is because you have to downgrade your ethers version 5.6.2, and then change your line to: const provider = new ethers.providers.JsonRpcProvider("HTTP://127.0.0.1:7545");

Hope it helps.