我开始使用Hardhat在我的本地系统上部署重力合约,它第一次成功部署,之后我运行一个脚本来创建Gravatars,但当我运行yarn Hardhat node—hostname 0.0.0.0时失败. 它显示了以下错误
*错误:事务恢复没有一个原因字符串在。(0 x5fbdb2315678afecb367f032d93f642f64180aa3)在HardhatNode。_mineBlockWithPendingTxs (/home/ahsan nftiple/市场/node_modules/建筑工人/src/内部/hardhat-network/供应商/node.ts: 1582:23)在HardhatNode。mineBlock (/home/ahsan nftiple/市场/node_modules/建筑工人/src/内部/hardhat-network/供应商/node.ts: 435:16)在EthModule。_sendTransactionAndReturnHash (/home/ahsan/nftiple/市场/node_modules/建筑工人/src/内部/hardhat-network/供应商/模块/eth.ts: 1494:18)在HardhatNetworkProvider。_sendWithLogging (/home/ahsan nftiple/市场/node_modules/建筑工人/src/内部/hardhat-network/供应商/provider.ts: 129:22)在HardhatNetworkProvider。请求(/home/ahsan nftiple/市场/node_modules/建筑工人/src/内部/hardhat-network/供应商/provider.ts: 106:18)*
这是我的部署脚本和重力的代码。Sol合同文件
import {HardhatRuntimeEnvironment} from "hardhat/types";
import {DeployFunction} from "hardhat-deploy/types";
const func: DeployFunction = async (hre: HardhatRuntimeEnvironment) => {
const {deployments, getNamedAccounts} = hre;
const {deployer} = await getNamedAccounts();
const deployResult = await deployments.deploy("GravatarRegistry", {
from: deployer,
log: true,
});
const Registry = await hre.ethers.getContractFactory('GravatarRegistry');
const registry = await Registry.attach(deployResult.address);
registry.createGravatar('Virat','https://img1.hscicdn.com/image/upload/f_auto,t_gn_f_345/lsci/db/PICTURES/CMS/316600/316605.png');
registry.createGravatar('Saurav','https://img1.hscicdn.com/image/upload/f_auto,t_gn_f_345/lsci/db/PICTURES/CMS/319900/319951.png');
registry.createGravatar('Sachin','https://img1.hscicdn.com/image/upload/f_auto,t_gn_f_345/lsci/db/PICTURES/CMS/316400/316486.png');// FAILS HERE
};
export default func;
pragma solidity ^0.8.0;
contract GravatarRegistry {
event NewGravatar(uint id, address owner, string displayName, string imageUrl);
event UpdatedGravatar(uint id, address owner, string displayName, string imageUrl);
struct Gravatar {
address owner;
string displayName;
string imageUrl;
}
Gravatar[] public gravatars;
mapping(uint => address) public gravatarToOwner;
mapping(address => uint) public ownerToGravatar;
function createGravatar(string memory _displayName, string memory _imageUrl) public {
require(ownerToGravatar[msg.sender] == 0);
gravatars.push(Gravatar(msg.sender, _displayName, _imageUrl));
uint id = gravatars.length - 1;
gravatarToOwner[id] = msg.sender;
ownerToGravatar[msg.sender] = id;
emit NewGravatar(id, msg.sender, _displayName, _imageUrl);
}
function getGravatar(address owner) public view returns (string memory, string memory) {
uint id = ownerToGravatar[owner];
return (gravatars[id].displayName, gravatars[id].imageUrl);
}
function updateGravatarName(string memory _displayName) public {
require(ownerToGravatar[msg.sender] != 0);
require(msg.sender == gravatars[ownerToGravatar[msg.sender]].owner);
uint id = ownerToGravatar[msg.sender];
gravatars[id].displayName = _displayName;
emit UpdatedGravatar(id, msg.sender, _displayName, gravatars[id].imageUrl);
}
function updateGravatarImage(string memory _imageUrl) public {
require(ownerToGravatar[msg.sender] != 0);
require(msg.sender == gravatars[ownerToGravatar[msg.sender]].owner);
uint id = ownerToGravatar[msg.sender];
gravatars[id].imageUrl = _imageUrl;
emit UpdatedGravatar(id, msg.sender, gravatars[id].displayName, _imageUrl);
}
// the gravatar at position 0 of gravatars[]
// is fake
// it's a mythical gravatar
// that doesn't really exist
// dani will invoke this function once when this contract is deployed
// but then no more
function setMythicalGravatar() public {
require(msg.sender == 0x8d3e809Fbd258083a5Ba004a527159Da535c8abA);
gravatars.push(Gravatar(address(0), " ", " "));
}
}
我正在使用hardhat-deploy,并且我知道hardhat-deploy试图避免重新部署相同的契约。是否有任何方法可以将我的硬帽重置为空白/干净状态,以便我摆脱此错误。我已经尝试删除node_modules目录多次,但无济于事
如果这是一个硬帽部署错误,并且与重新部署有关,您可以尝试在控制台中运行以下命令:
yarn hardhat deploy --reset
查看https://github.com/wighawag/hardhat-deploy#deploy-scripts获取更多信息。
但是,关于你的错误日志,有一个没有原因字符串的tx返回,所以你可以检查是否有一些require语句正在触发或检查如果你触发:
会发生什么registry.createGravatar('Sachin','https://img1.hscicdn.com/image/upload/f_auto,t_gn_f_345/lsci/db/PICTURES/CMS/316400/316486.png');// FAILS HERE
所以,我建议检查你是否做了两次事情,一个require语句触发了回归,或者你检查你的合同-你可以使用硬帽控制台https://hardhat.org/guides/hardhat-console在控制台记录,而你执行函数(只是把console.log()在某些点,你想确保它们被执行或导致问题)。