How to reset/clean restart hardhat

Viewed 1502

I started the deployment of Gravity contract on my local system using Hardhat and it got deployed successfully first time, after that I ran a script to create Gravatars but it fails when I run yarn hardhat node --hostname 0.0.0.0. It shows the below errors

*Error: Transaction reverted without a reason string at . (0x5fbdb2315678afecb367f032d93f642f64180aa3) at HardhatNode._mineBlockWithPendingTxs (/home/ahsan/nftiple/marketplace/node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:1582:23) at HardhatNode.mineBlock (/home/ahsan/nftiple/marketplace/node_modules/hardhat/src/internal/hardhat-network/provider/node.ts:435:16) at EthModule._sendTransactionAndReturnHash (/home/ahsan/nftiple/marketplace/node_modules/hardhat/src/internal/hardhat-network/provider/modules/eth.ts:1494:18) at HardhatNetworkProvider._sendWithLogging (/home/ahsan/nftiple/marketplace/node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:129:22) at HardhatNetworkProvider.request (/home/ahsan/nftiple/marketplace/node_modules/hardhat/src/internal/hardhat-network/provider/provider.ts:106:18) *

Here is the code of my deployment script and Gravity.sol contract file

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), " ", " "));
    }
}

I am using hardhat-deploy and I know that hardhat-deploy tries to avoid redeployment of the same contract. Is there any way I can reset my hardhat to a blank/clean state so that I get rid of this error.I have tried deleting node_modules directory multiple times but to no avail

1 Answers

If it's a hardhat-deploy error and has to do with a re-deployment, you could try running the following at your console:

yarn hardhat deploy --reset

Check https://github.com/wighawag/hardhat-deploy#deploy-scripts for more informations.

But, regarding your error log, there was a tx reverted without a reason string, so you could check if some require statements are firing or checking what happens if you fire the:

registry.createGravatar('Sachin','https://img1.hscicdn.com/image/upload/f_auto,t_gn_f_345/lsci/db/PICTURES/CMS/316400/316486.png');// FAILS HERE

So, I would suggest to check if you doing things twice and a require statement triggers the reversion, or you check your contract - you could use the hardhat console https://hardhat.org/guides/hardhat-console for logging at console while you executing functions (just put a console.log() at some points where you want to get sure they are executed or causing the problem).

Related