Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/blackberry/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ethereum 从web3js调用工厂合同子级中的函数_Ethereum_Solidity_Web3js_Truffle - Fatal编程技术网

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

Ethereum 从web3js调用工厂合同子级中的函数,ethereum,solidity,web3js,truffle,Ethereum,Solidity,Web3js,Truffle,给定一份合同示例和一份工厂合同示例工厂: //Example.sol contract ExampleFactory { Example [] public examples; function newExample(bytes32 _name) { Example example = new Example(_name); examples.push(example); } } contract Example { bytes32 public name;

给定一份合同
示例
和一份工厂合同
示例工厂

//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

//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!!
    })

  })
})

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

artifacts.require("Example" )

Const example = await Example.at(await this.exampleFactory.examples(0))

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

artifacts.require("Example" )

Const example = await Example.at(await this.exampleFactory.examples(0))