I am making a smart contract so that I can send messages and other addresses can also send messages in that contract. So the message, block.timestamp and the sender's address will be stored in an "array of structs" in blockchain which will be done via transaction. Now in scripts/deploy.js file, we need getSigners() method so that we will be able to transact by "metamask" wallet. So, first check this line ---> const [_, randomPerson,addr1, addr2] = await hre.ethers.getSigners()
.
We know that the 1st value in this array is the owner by default. So, "" _(underscore) "" is the owner in this case. So, firstly please check my "scripts/deploy.js" and "WavePortal.sol" file--->
WavePortal.sol
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.0 <0.9.0;
import "hardhat/console.sol";
contract WavePortal {
uint256 totalWaves;
event NewWave(address indexed from, uint timestamp, string message);
struct Wave{
address waver;
string message;
uint timestamp;
}
Wave[] waves;
constructor() {
console.log("Yo yo, I am a contract and I am smart");
}
function wave(string memory _message) public {
totalWaves += 1;
console.log("%s waved with message %s", msg.sender, _message);
waves.push(Wave(msg.sender, _message, block.timestamp));
emit NewWave(msg.sender, block.timestamp, _message);
}
function getAllWaves() public view returns (Wave[] memory){ //Array of waves
return waves;
}
function getTotalWaves() public view returns (uint256) {
console.log("We have %d total waves!", totalWaves);
return totalWaves;
}
}
scripts/deploy.js
const hre = require("hardhat");
const main = async () => {
const waveContractFactory = await hre.ethers.getContractFactory("WavePortal");
const waveContract = await waveContractFactory.deploy();
await waveContract.deployed();
console.log("Contract address:", waveContract.address);
let waveCount;
waveCount = await waveContract.getTotalWaves();
console.log(waveCount.toNumber());
let waveTxn = await waveContract.wave("A message!");
await waveTxn.wait();
const [_, randomPerson, addr1, addr2] = await hre.ethers.getSigners();
console.log("Address of _ is: ",_.address); //Working absolutely fine
console.log("Random Person: ", randomPerson);
console.log("Address of Random Person is: ", randomPerson.address);
waveTxn = await waveContract.connect(randomPerson).wave("Another message!");
await waveTxn.wait();
let allWaves = await waveContract.getAllWaves();
console.log(allWaves);
};
const runMain = async () => {
try {
await main();
process.exit(0);
} catch (error) {
console.log(error);
process.exit(1);
}
};
runMain();
Now, when I am deploying it to localhost by running the command npx hardhat node
after that I run this command in another terminal npx hardhat run scripts/deploy.js --network localhost
, Everything is working absolutely fine. I was getting the address
of "_" as well as "randomPerson".
But when I was trying to deploy it by the command npx hardhat run scripts/deploy.js --network rinkeby
via Alchemy or Infura, (owner) is working totally fine, but other addresses like randomPerson
or addr1
or addr2
is showing undefined when testing with console.log like console.log("randomPerson is: ", randomPerson)
gives the output --> undefined
. So I am unable to use/call the functions of WavePortal.sol by randomPerson or addr1 or addr2 which we do via connect()
method for other addresses. I am only able to use/call its functions with "(underscore)" which is the owner. And so I am also unable to Deploy it on Rinkeby network of Alchemy or Infura via npx hardhat run scripts/deploy.js --network rinkeby
.
I have tried each and everything but I am not getting the solution from anywhere. My hardhat.config.js file is also properly updated as per Rinkeby or Ropsten whichever network I'm using.
So, Please help me regarding this.
I got the solution to this question from Hardhat Official Discord channel.
As we know that private keys are different for different Accounts, like I wanna use 2 or more Rinkeby Accounts in this case, so I need to include their private keys also in hardhat.config.js
under accounts.
accounts: [privateKey1, privateKey2, ...]
Link --> https://hardhat.org/config