Hardhat, function and variable are not showing the result #1611
-
I have created a simple contract using hardhat, the contract is deploying successfully but when I want to access a variable or function then it's not showing any result, and below is my code Output
Storage.sol //SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
contract Storage {
uint[] public money;
function storeMoney(uint256 _money) public returns (uint256) {
money.push(_money);
return _money;
}
} deploy.js async function main() {
const simpleStorage = await ethers.getContractFactory("Storage");
console.log("Contract is deploying....");
const simple = await simpleStorage.deploy();
await simple.deployed();
console.log(`Deployed at ${simple.address}`);
await simple.deployTransaction.wait(2);
const store = await simple.storeMoney(35);
console.log("You have sent: ", +store);
const dollors = await simple.money;
console.log(dollors[0]);
}
main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
}); |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 5 replies
-
Create a getter function instead of using simple.money. Also, is |
Beta Was this translation helpful? Give feedback.
-
There are several errors in your code: |
Beta Was this translation helpful? Give feedback.
-
@shakilkhan12 This is how you can achieve what you are trying to do: You do not need to wait after deploying, so remove it: await simple.deployTransaction.wait(2); Here the So instead of this: const store = await simple.storeMoney(35);
console.log("You have sent: ", +store); Do this: const transactionResponse = await simple.storeMoney(35);
console.log("Waiting for 1 block to mine...");
await transactionResponse.wait(1); After that, you are calling a public money variable that is a type of array, and for each public variable solidity creates a getter function by default, so you are calling it like a variable, instead, call it like a function. Like this: const dollors = await simple.money(); Now, as I mentioned above solidity creates a getter function for all of the public variables, but here is a twist, for arrays it creates a getter function with an argument of the index (by default it is set to 0) and returns only the first value of array if you do not specify index while calling the function. So you do not need to pass the index while printing the response of - console.log(dollors[0]);
+ console.log(dollors); Hope it helps you. |
Beta Was this translation helpful? Give feedback.
There are several errors in your code:
Remove this line from your code:
await simple.deployTransaction.wait(2);
It causes an unnecessary wait.Also, you cannot print out the variable
store
because it is not a number so just try hardcoding the value in yourconsole.log("You have sent <value>")
statementAlso you are trying to access a solidity array directly (which is a tricky affair) in an incorrect manner. checkout https://www.geeksforgeeks.org/solidity-arrays/ for more information on how to work with arrays.
I'd recommend using a different methodology/approach altogether to get the value stored.