1

I am trying to debug a smart contract using hardhat. I am using the below code.


pragma solidity ^0.8.9;

import "Hardhat/node_modules/hardhat/console.sol";

contract SmartWallet
{
    address payable public owner;
    mapping(address => uint) private Allowed_address;
    mapping(address => uint) private Amount_deposit;
    bool private lock = false;


// We assign the owner during the start of the smart contract.
    constructor()
    {
       owner = payable(msg.sender);
    }

//This modifier allows only the owner to access the function.
    modifier OnlyOwner()
    {
        require(msg.sender == owner,"SmartWallet: You are not the owner");
        _;
    }
//This modifier allows only the owner or the allowed addresses if the amount they have is greater than
//that they are trying to send.
    modifier OwnerorAllowed(uint _amount)
    {
        require(msg.sender == owner || Allowed_address[msg.sender] >= _amount,"SmartWallet: You are not allowed to transfer");
        _;
    }

//The receive function will store all the addresses who have send money to the contract and how much money they
//have sent.
    receive() external payable
    {
        (Amount_deposit[msg.sender] += msg.value);
    }

//In this function the owner allows other address permission to withdraw certain maount from the contract.
    function GrantAllowance(address _address, uint _amount) public OnlyOwner()
    {
        Allowed_address[_address] = _amount;
    }

//Using this function the Allowance can checked.
    function CheckAllowance(address _account) public view returns(uint)
    {
        return(Allowed_address[_account]);
    }

// This function is used to check the amount deposited by different owners
    function CheckDepositAmount(address _account) public view returns(uint)
    {
        return(Amount_deposit[_account]);
    }

//This function is used to check the balance of the smart contract.
    function CheckBalance() public view returns(uint)
    {
        return address(this).balance;
    }

//This function is used to reduce the amount alloted allowance to an address by the ownwer
    function ReduceAllowance(address _address, uint _amount) internal 
    {
        Allowed_address[_address] -= _amount;
    }
//This function is used to Transfer amount. But onlt the owner and allowed addresses can use it.
    function TransferMoney(uint _amount, address payable _to) public payable OwnerorAllowed(_amount)
    {
        if(msg.sender != owner)
        {
            require(Allowed_address[msg.sender] >= _amount,"SmartWallet: You are not allowed to send");
            ReduceAllowance(msg.sender,_amount);
        }
        require(lock == false,"Aborting!!! transfer is locked");
        lock = true;
        _to.transfer(_amount);
        lock = false;
    }
}```
 
 
**As you see in the 2nd line, I have imported the console.sol file available in my hardhat node modules folder, to perform the Debugging.**

**But it throws the following Error:
Error HH404: File Hardhat/node_modules/hardhat/console.sol, imported from Contracts/SmartWallet.sol, not found.
But the file is present under the specified location.**
3
  • Please post the command you are using and also the error you are getting. Commented Apr 30, 2023 at 15:07
  • Hi Afser, if you see in line 2 of the code, I have imported the console. sol, It should allow me to use the JavaScript console. log functionality with my solidity code. But when I try to use the Console.log I get the Error mentioned at the end of the question. Commented May 2, 2023 at 2:13
  • Sorry, I didn't read the last part of your code. The problem is you are importing the console package as a normal folder, but it is supposed to be a node_modules folder that doesn't need a specified path. Please see the answer below by @Gleichmut. The way he is importing the package is the correct way Commented May 2, 2023 at 14:53

1 Answer 1

2

I wrote a small test to check your issue:

import { ethers } from "hardhat";
import { expect } from "chai";
import { loadFixture } from "@nomicfoundation/hardhat-network-helpers";

describe.only("SmartWallet test suite", async function() {

    async function deploy() {
        const factory = await ethers.getContractFactory("SmartWallet");
        const contract = await factory.deploy();

        return { contract };
    }

    it("On emit new tokens balance of user changes", async function() {
        const accounts = await ethers.getSigners();
        const owner = accounts[0].address;
        const totalAmount = 42

        const { contract } = await loadFixture(deploy);
        await contract.GrantAllowance(owner, totalAmount);

        await expect(await contract.CheckAllowance(owner)).to.be.equal(totalAmount);
    });

})

It successfully passes when there is no console import. When console has been imported and used within the code, it successfully passes too.

    import "hardhat/console.sol";

    function GrantAllowance(address _address, uint _amount) public OnlyOwner()
    {
        console.log("[start] GrantAllowance");
        Allowed_address[_address] = _amount;
    }

I run this test from my existing project. It means it is not a clean install and I can not say for sure what kind of modules and version it uses, but simple npm install hardhat should be enough to cover issue with the console.

1
  • For a future, either here or on github please consider to add working test suite to spot your issue. You are free to open an issue 'as is', but providing test suite will help a lot to assist you with your issue. Thanks!
    – Gleichmut
    Commented May 2, 2023 at 6:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.