【问题标题】:Calling function in child of Factory contract from web3js从 web3js 调用 Factory 合约的子函数
【发布时间】:2019-07-08 18:41:19
【问题描述】:

给定一份​​合同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);
  }
}

如何在我的truffle test 中调用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!!
    })

  })
})

【问题讨论】:

    标签: ethereum solidity web3js truffle


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-19
      • 2017-08-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-17
      • 2021-07-22
      相关资源
      最近更新 更多