1

While testing some code on hardhat, I found an unexplained exception that was being thrown. I couldn't find any solution both on the discord channels, as well as in the etherjs docs.

This is my test suite:

import {ethers} from "hardhat";
import {loadFixture} from "ethereum-waffle";        

     describe.only("Images", async function () {
          let result;
        
          it("Creates images", async function () {
            const {Decentragram} = await loadFixture(fixture);
            const [owner, address2] = await ethers.getSigners();
            result = await Decentragram.connect(owner).uploadImage();
            let test = await Decentragram.connect(owner).test("test");
            console.log(test);
          });
        });

This is my contract:

pragma solidity 0.8.6;

contract Decentragram {
    string public name = "Decentragram";

    // Store Images
    mapping(uint256 => Image) public images;
    mapping(string => uint256) public test;

    struct Image {
        uint256 id;
        string hashImg;
        string description;
        uint256 tipAmount;
        address payable author;
    }

    // Create Images
    function uploadImage() public {
        images[1] = Image({
            id: 1,
            hashImg: "abcd",
            description: "description",
            tipAmount: 0,
            author: payable(address(0x0))
        });
    }

    function getImage(uint256 id) public view returns (Image memory) {
        Image memory img = images[id];
        return img;
    }
    // Tip Images
}

This is the error I'm getting when running "npx hardhat test":

Error: call revert exception (method="test(string)", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.4.0)
      at Logger.makeError (node_modules\@ethersproject\logger\src.ts\index.ts:213:28)
      at Logger.throwError (node_modules\@ethersproject\logger\src.ts\index.ts:225:20)
      at Interface.decodeFunctionResult (node_modules\@ethersproject\abi\src.ts\interface.ts:425:23)
      at Contract.<anonymous> (node_modules\@ethersproject\contracts\src.ts\index.ts:332:44)
      at step (node_modules\@ethersproject\contracts\lib\index.js:48:23)
      at Object.next (node_modules\@ethersproject\contracts\lib\index.js:29:53)
      at fulfilled (node_modules\@ethersproject\contracts\lib\index.js:20:58)
      at processTicksAndRejections (internal/process/task_queues.js:95:5)
      at runNextTicks (internal/process/task_queues.js:64:3)
      at listOnTimeout (internal/timers.js:526:9)
      at processTimers (internal/timers.js:500:7)
Edward Casanova
  • 726
  • 7
  • 19

1 Answers1

0

In the test suite:

import {ethers} from "hardhat";
import {loadFixture} from "ethereum-waffle";        

     describe.only("Images", async function () {
          let result;

          it("Creates images", async function () {
            const {Decentragram} = await loadFixture(fixture);
            const [owner, address2] = await ethers.getSigners();
            result = await Decentragram.connect(owner).uploadImage();
            let test = await Decentragram.test("test"); <<<<< don't use .connect() in order to call any default getter generated by solidity. Can't tell for sure but perhaps is because it is not necessary. There's not msg.sender on the call? Who knows? If you do, i'd appreciate your explanation below.
            console.log(test);
          });
        });

I finally found the explanation. Turns out when I deployed my contract I used this fixture on mocha-waffle:

Here, when the contract gets deployed it's already tied to a signer (owner).

async function fixture([owner]: Wallet[], provider: Provider) {
  const _Decentragram: Contract = await deployContract(owner, DecentragramJSON);
  const Decentragram: Contract = await _Decentragram.deployed();

<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Here we instead use a contract factory deploy method, which doesn't need a Signer by default.

  const _Decentagram2Fac = await ethers.getContractFactory("Decentragram");
  const _Decentagram2 = await _Decentagram2Fac.deploy();
  const Decentragram2 = await _Decentagram2.deployed();
  return {Decentragram, Decentragram2};
}
Edward Casanova
  • 726
  • 7
  • 19