无法使用" connect()"用硬汉签署不同帐户交易的方法
我正在签订智能合约,以便我可以发送消息,其他地址也可以在该合同中发送消息。因此,消息, block.timestamp 和发件人的地址将存储在区块链中的“结构阵列”中,这将通过交易完成。现在,在脚本/deploy.js 文件中,我们需要 getsigners()方法,以便我们能够通过“ metamask”钱包进行交易。因此,首先检查此行---> const [_,randomperson,addr1,addr2] =等待hre.ethers.getSigners()
。 我们知道,默认情况下,此数组中的第一个值是所有者。因此,在这种情况下,所有者是所有者。因此,首先请检查我的“脚本/decloy.js”和“ waveportal.sol”文件--->
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;
}
}
脚本/decloy.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();
现在,当我通过运行命令npx hardhat节点
将其部署到localhost时命令在另一个终端npx hardhat运行脚本/decloy.js - 网络localhost
中,一切正常。我得到了“ _”的地址
以及“ randomperson”。
但是,当我试图通过命令npx hardhat run脚本/decloy.js -network rinkeby
通过 alchemy或infura ,(所有者)正在工作时完全可以,但是其他地址,例如randomperson
或addr1
或addr2
在使用Console.log likes进行测试时显示 undefined console.log(“ randomperson是:”,randomperson)
给出输出 - &gt; 未定义的
。因此,我无法使用/调用/调用randomperson或addr1或addr2的 waveportal.sol.sol.sol.sol.sol.sol.sol.sol.sol.sol.sol。我只能使用所有者使用/调用其功能“ (下划线)”。因此,我也无法通过 alchemy>或 infura 通过npx hardhat run scripts/exploy.js- -network rinkeby
。
我已经尝试了每一个,但我没有从任何地方获得解决方案。 我的 hardhat.config.js 文件也可以根据我使用的任何网络正确更新。
因此,请帮助我。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您尝试部署到Rinkeby,则实际上需要私钥和帐户来发送TX。
ethers.getSigners()
适用于Localhost,因为它可以使用1000 Ether创建所谓的虚拟帐户。因此,当您尝试部署到真实网络时,它无法创建一个虚拟帐户,必须从配置中指定的真实帐户发送TX。
因此,如果您创建另一个真实帐户并在配置中指定它应该可以使用。
If you try to deploy to rinkeby you need actually private key and account to send the tx.
ethers.getSigners()
works for the localhost because it creates so called dummy accounts with 1000 ether.So when you try to deploy to a real network it can't create a dummy account the tx has to be sent from a real account that is specified in the config.
So if you create another real account and specify it in your config it should work.
我从Hardhat官方不和谐频道中获得了这个问题的解决方案。
众所周知,在这种情况下,我想使用2个或更多的Rinkeby帐户,私钥有所不同,因此我需要在
hardhat.config.js中包含他们的 private键
帐户下。帐户:[PrivateKey1,PrivateKey2,...]
链接 - &gt;
https://hardhat.org/config
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