Вызов функции в дочернем фабричном контракте из web3js - PullRequest
0 голосов
/ 14 февраля 2019

С учетом контракта 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!!
    })

  })
})

1 Ответ

0 голосов
/ 14 февраля 2019

Вызов this.exampleFactory.examples(0) возвращает адрес дочернего контракта, который web3.js не знает ABI.Вам необходимо импортировать ABI ребенка и создать объект с адресом

artifacts.require("Example" )

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