smartcontractkit / full-blockchain-solidity-course-js

Learn Blockchain, Solidity, and Full Stack Web3 Development with Javascript
12.01k stars 2.91k forks source link

Error:TypeError: simpleStorage.deployed is not a function #5975

Closed highHumann closed 11 months ago

highHumann commented 11 months ago

This is my code

// imports
const { ethers, run, network } = require("hardhat")

// async main
async function main() {
    const SimpleStorageFactory = await ethers.getContractFactory(
        "SimpleStorage"
    )
    console.log("Deploying contract...")
    const simpleStorage = await SimpleStorageFactory.deploy()
    await simpleStorage.deployed()
    console.log(`Deployed contract to: ${simpleStorage.address}`)
    // what happens when we deploy to our hardhat network?
    if (network.config.chainId === 11155111 && process.env.ETHERSCAN_API_KEY) {
        console.log("Waiting for block confirmations...")
        await simpleStorage.deployTransaction.wait(6)
        await verify(simpleStorage.address, [])
    }

    const currentValue = await simpleStorage.retrieve()
    console.log(`Current Value is: ${currentValue}`)

    // Update the current value
    const transactionResponse = await simpleStorage.store(7)
    await transactionResponse.wait(1)
    const updatedValue = await simpleStorage.retrieve()
    console.log(`Updated Value is: ${updatedValue}`)
}

// async function verify(contractAddress, args) {
const verify = async (contractAddress, args) => {
    console.log("Verifying contract...")
    try {
        await run("verify:verify", {
            address: contractAddress,
            constructorArguments: args,
        })
    } catch (e) {
        if (e.message.toLowerCase().includes("already verified")) {
            console.log("Already Verified!")
        } else {
            console.log(e)
        }
    }
}

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

and I am still getting the same error even I am trying to do it with github's deploy.js but it is still giving the same error


yarn hardhat run scripts/deploy.js 
yarn run v1.22.15
warning ../../../package.json: No license field
$ /home/faizan/folder/rvn/h-s-s/node_modules/.bin/hardhat run scripts/deploy.js
Deploying contract...
TypeError: simpleStorage.deployed is not a function
    at main (/home/faizan/folder/rvn/h-s-s/scripts/deploy.js:11:25)
    at processTicksAndRejections (node:internal/process/task_queues:96:5)
error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.```

_Originally posted by @highHumann in https://github.com/smartcontractkit/full-blockchain-solidity-course-js/discussions/793#discussioncomment-6656546_
hamingsi commented 11 months ago

maybe you could share your whole repo or just package.json file it seems that ethers 6 Basecontract don't have method deployed while the method is still available in ethers 5 you can look up the offcial document

highHumann commented 11 months ago

Thanks for replying ya the issue was i was using the latest version of ethers now it is working

wjcty commented 11 months ago

I encountered the same problem,May I ask how to solve it

highHumann commented 11 months ago

by installing the dependencies recommended in package.json other than the latest

{
  "name": "hardhat-simple-storage-fcc",
  "devDependencies": {
    "@nomiclabs/hardhat-ethers": "^2.0.4",
    "@nomiclabs/hardhat-etherscan": "^3.0.0",
    "@nomiclabs/hardhat-waffle": "^2.0.2",
    "chai": "^4.3.4",
    "ethereum-waffle": "^3.4.0",
    "ethers": "^5.5.3",
    "hardhat": "^2.8.3",
    "hardhat-gas-reporter": "^1.0.7",
    "solidity-coverage": "^0.7.18"
  },
  "dependencies": {
    "dotenv": "^14.2.0",
    "prettier-plugin-solidity": "^1.0.0-beta.19"
  },
  "scripts": {
    "lint": "yarn prettier --check .",
    "lint:fix": "yarn prettier --write ."
  }
}

paste this file in your package.json then run yarn or npx i in the terminal for installing the dependencies

wjcty commented 11 months ago

Thanks for replying,but I encountered another problem, when I executed yarn hardhat run srcipts/deploy.js, I was prompted to install @nomicfoundation/hardhat-toolbox and other related modules,when I finish installing, reminder again simpleStorage.deployed is not a function. my version of node is 16.14.2, version of npm is 9.8.1