当合同调用另一个注入的合同时,我得到以下错误:
我不确定合作合同是否被正确地注入。从测试输出来看,这似乎是问题所在(测试输出中地址为零的原因)。总之,这是主要合同:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
import "hardhat/console.sol";
import "./TestDependency.sol";
contract TestContract
{
TestDependency private testDep;
function setDependency(address dependencyAddress) external
{
testDep = TestDependency(dependencyAddress);
}
function getDependency() external view returns (address)
{
return address(testDep);
}
function internalHello() public view returns (string memory) {
console.log("internal hello function called");
return "internal hello";
}
function helloWithDependency() public view returns (string memory) {
console.log("helloWithDependency function called");
return testDep.getConstant();
}
}扶养合同:
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
import "hardhat/console.sol";
contract TestDependency
{
function getConstant() public view returns (string memory) {
console.log("getConstant function called");
return "test123";
}
}还有一项测试:
const { expect } = require("chai");
const { ethers } = require("hardhat");
describe("Test", function () {
it("Test dependency", async function () {
const TestContract = await ethers.getContractFactory("TestContract");
const testContract = await TestContract.deploy();
await testContract.deployed();
const TestDependency = await ethers.getContractFactory("TestDependency");
const testDependency = await TestDependency.deploy();
await testDependency.deployed();
testContract.setDependency(testDependency);
console.log("testContract.address:" + testContract.address);
console.log("testDep.address:" + testDependency.address);
console.log("testDep injected dependency address:" + testContract.getDependency());
let internalHello = await testContract.internalHello();
console.log("internalHello response:" + internalHello);
let helloResp = await testContract.helloWithDependency();
console.log("helloWithDependency response:" + helloResp);
});
});我得到了以下输出:
npx hardhat test test/test-dependency-test.js
Test
testContract.address:0x5FbDB2315678afecb367f032d93F642f64180aa3
testDep.address:0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512
testDep injected dependency address:0x0000000000000000000000000000000000000000
internal hello function called
internalHello response:internal hello
helloWithDependency function called
1) Test dependency
0 passing (18s)
1 failing
1) Test
Test dependency:
**Error: call revert exception [ See: https://links.ethers.org/v5-errors-CALL_EXCEPTION ] (method="helloWithDependency()", data="0x", errorArgs=null, errorName=null, errorSignature=null, reason=null, code=CALL_EXCEPTION, version=abi/5.6.2)**
at Logger.makeError (node_modules/@ethersproject/logger/src.ts/index.ts:261:28)
at Logger.throwError (node_modules/@ethersproject/logger/src.ts/index.ts:273:20)
at Interface.decodeFunctionResult (node_modules/@ethersproject/abi/src.ts/interface.ts:427:23)
at Contract.<anonymous> (node_modules/@ethersproject/contracts/src.ts/index.ts:400: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)这可能是个新手的问题,但我在短期内对此很感兴趣。
提前谢谢你,马尔科
发布于 2022-05-20 06:43:12
你离答案不远,检查一下你的
testContract.setDependency(testDependency);
to be replaced with
await testContract.setDependency(testDependency.address);https://ethereum.stackexchange.com/questions/128574
复制相似问题