How to include another Contract as dependency?

Issue

A function in a contract that is spawning a new contract executes fine from Remix but fails when called from my custom app, without giving any details except "transaction failed". I’m pretty sure the reason for this is that both contracts are written in the same file in Remix so it knows exactly how to define the child contract whereas my app only takes in the abi of the source/parent.
The simplified distributed code is something like this:

pragma solidity ^0.6.0;

contract Parent{
  address[] public children;

  function createChild (uint256[] memory distro)external payable{
    children.push(address(new Child(msg.sender,distro)));
  }
}

contract Child{
  address payable owner;
  uint256[] distribution;

  constructor(address payable admin,uint256[] memory distro)public payable{
    owner=admin;
    distribution=distro;
  }
}

in my Flutter app, I’m defining the Parent contract using a list of strings like so:

  List<String> source = [
    "function createChild(int256[]) payable",
  ]

And then proceed to get the user’s wallet credentials and push the transaction like so:

  String address = "0xbAF01C91b2d53cC9eeb0ED1A300783Cb74563010"; //address of deployed Parent
  var contract = Contract(address, source, web3user); 
  contract.connect(web3user.getSigner()); // uses the connected wallet as signer
  var ponse = await promiseToFuture(callMethod(contract, "createChild", [
    [
    [30, 70], //the array of int256 it takes as parameters
    TxParams(
      value: "1000000000000", 
      gasLimit: "1000000",
    )
  ]));

How do I let my app know about the Child contract? I’m thinking it should be somehow included in the source list of functions, so that the EVM knows what to compile and deploy, but it’s unclear how to specify that.

Solution

Whenever you want to interact with a contract on-chain, you need 4 things:

  1. The ABI
  2. The Address
  3. The Blockchain
  4. A wallet with funds to pay for the gas

And when you create your contract, yes, your application needs to know what the ABI of the child contract is. So when you create your child contract, you’ll need to pass in the ABI.

Answered By – Patrick Collins

Answer Checked By – Gilberto Lyons (FlutterFixes Admin)

Leave a Reply

Your email address will not be published.