从web3js调用工厂合同子的函数

问题描述 投票:0回答:1

鉴于合同Example和工厂合同ExampleFactory

//Example.sol

contract ExampleFactory {
  Example [] public examples;

 function newExample(bytes32 _name) {
   Example example = new Example(_name);
   examples.push(example);
 }
}

contract Example {

  bytes32 public name;
  bool printed;
  event Print(bytes32);

  constructor(bytes32 _name) {
    name = _name;
  }

  function printName() public {
    printed = true;
    emit Print(name);
  }
}

如何在我的printName中调用truffle test?:

//Example.test.sol

artifacts.require("ExampleFactory");

contract("Example", function () {

  beforeEach(async function() {
    this.exampleFactory = await ExampleFactory.new()
    await ExampleFactory.newExample(web3.utils.utf8ToHex("hello"))
  })

  describe("printName()", function () {

    it("PRINTS!", async function() {
     const example = await this.exampleFactory.examples(0);
     await example.printName() // example.printName is not a function!!
    })

  })
})
ethereum solidity web3js truffle
1个回答
2
投票

调用this.exampleFactory.examples(0)返回子契约的地址,web3.js不知道ABI。您需要导入子ABI并使用该地址实例化一个对象

artifacts.require("Example" )

Const example = await Example.at(await this.exampleFactory.examples(0))
© www.soinside.com 2019 - 2024. All rights reserved.