微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

错误:使用 Contract.connect(Signer) 调用 getter 时,使用 Mocha、Chai、Waffle 测试在 Hardhat 上调用还原异常

如何解决错误:使用 Contract.connect(Signer) 调用 getter 时,使用 Mocha、Chai、Waffle 测试在 Hardhat 上调用还原异常

在安全帽上测试一些代码时,我发现抛出了一个无法解释的异常。我在 discord 频道和 etherjs 文档中都找不到任何解决方案。

这是我的测试套件:

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

这是我的合同:

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
}

这是我在运行“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)

解决方法

在测试套件中:

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

我终于找到了解释。事实证明,当我部署我的合同时,我在摩卡华夫饼上使用了这个装置:

在这里,当合同被部署时,它已经绑定到签名者(所有者)。

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

这里我们改为使用合约工厂部署方法,默认情况下不需要签名者。

  const _Decentagram2Fac = await ethers.getContractFactory("Decentragram");
  const _Decentagram2 = await _Decentagram2Fac.deploy();
  const Decentragram2 = await _Decentagram2.deployed();
  return {Decentragram,Decentragram2};
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?