I have created a simpleStorage contract and deploying it using hardhat, everything is working fine but in there is a store function to change the value of a variable. After executing it, when I am retrieveing value, it is not giving me updated value, but the default value.
This is my contract
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract SimpleStorage {
//this line defines the start of a contract
uint256 favouriteNumber = 7;
People[] public people;
mapping(string => uint256) public nametonum;
struct People {
uint256 favouriteNumber;
string name;
}
function store(uint256 _favouriteNumber) public virtual {
favouriteNumber = _favouriteNumber;
}
function retrieve() public view returns (uint256) {
return favouriteNumber;
}
function addperson(string memory _name, uint256 _favouriteNumber) public {
people.push(People(_favouriteNumber, _name)); //push function of array, creating struct without{}
nametonum[_name] = _favouriteNumber;
}
}
and this my deploy script
const { ethers, run, network } = require("hardhat")
async function main() {
const simpleStorageFactory = await ethers.getContractFactory(
"SimpleStorage"
)
console.log("Deploying... Please wait")
const simpleStorageContract = await simpleStorageFactory.deploy()
await simpleStorageContract.waitForDeployment()
console.log(
`Deployed contract to: ${(
await simpleStorageContract.getAddress()
).toString()}`
)
if (network.config.chainId === 11155111 && process.env.ETHERSCAN_API_KEY)
await simpleStorageContract.deploymentTransaction().wait(6)
await verify((await simpleStorageContract.getAddress()).toString(), [])
const currentValue = await simpleStorageContract.retrieve()
console.log(currentValue.toString())
const changeNumberResponse = await simpleStorageContract.store("10")
changeNumberResponse.wait()
const updatedValue = await simpleStorageContract.retrieve()
console.log(updatedValue.toString())
}
async function verify(contractAddress, args) {
console.log("Verifying Contract...")
try {
await run("verify:verify", {
address: contractAddress,
constructorArguments: args,
})
} catch (error) {
if (error.message.toLowerCase().includes("already been verified")) {
console.log("Already verified!")
} else {
console.error(error)
}
}
}
main()
.then(() => process.exit(0))
.catch((eroor) => {
console.error(eroor)
process.exit(1)
})
updated value is not getting changed value after store function call
response–
Deploying... Please wait
Deployed contract to: 0xDA63Fc7a82d1eD19b8cC301206AaFC4e56Fd9C75
Verifying Contract...
Successfully submitted source code for contract
contracts/SimpleStorage.sol:SimpleStorage at 0xDA63Fc7a82d1eD19b8cC301206AaFC4e56Fd9C75
for verification on the block explorer. Waiting for verification result...
Successfully verified contract SimpleStorage on the block explorer.
https://sepolia.etherscan.io/address/0xDA63Fc7a82d1eD19b8cC301206AaFC4e56Fd9C75#code
7
7
Please help!