0

I am developing a decentralised notary app, using truffle/web3/react. It need to deploy multiple instances of a same contract.

I managed to deploy multiple instances of a contract, MyContract via a registry, MyContractRegistry.

  const contractRegistry = contract(MyContractRegistry);
  contractRegistry.setProvider(web3.currentProvider);

  contractRegistry.deployed().then(function(instance) {
    registryInstance = instance;

    myArray.map(function(){
        return registryInstance
          .createCertificate(
            params
            { from: issuer }
          )        
    });

   });

The above code works, but if the myArray have 10 elements, then Metamask will popup 10 confirmation dialogs. Yes it should and as expected.

So, my question is what is the better approach to the problem above.

2 Answers 2

1

The best approach will be to have a contract that deploys the contracts. In this way you only need a single transaction for N contracts to deploy, is gas efficient and you will have a registry on-chain of all the deployed contracts.

2
  • To add to this, it gives the benefit of not having to include the contract's bytecode as tx data for each separate creation.
    – natewelch_
    Commented Nov 3, 2018 at 15:16
  • if this answered your question, please accept it by clicking on the check mark so that people know.
    – Jaime
    Commented Nov 6, 2018 at 6:25
1

I followed Jaime suggested approach. It works and thanks again Jaime!

MyContractRegistry.sol

function createCertificates(address[] _owners, bytes32[] _ownerNames, bytes32[] _grades) public {

    // Of course the isuer is allowed to create certificate!
    require(msg.sender == owner);

    for( uint i = 0; i<_owners.length; i++ ){

        address newCertificate = new MyContract(_owners[i], _ownerNames[i], _grades[i]);

        // store each certificate
        registeredCertificates.push(newCertificate);

        emit ContractCreated(newCertificate, _owners[i]);
    }
}

client web3:

contractRegistry.deployed().then(function(instance) {
return instance.createCertificates(
    owners,     // public addresses, "0xfE6D20...Fc4DdeaA"
    ownerNames, // "John", "Jimmy", etc
    grades,     // For example "A", "B", etc
    { from: issuer }
 )});

However, it only works for array up to 15 elements. I wish it can do for at least 100 elements. Any ideas?

1
  • Sorry,... if I added the 16th element, Metamask will complain "Transaction Error: Exception thrown in contract code". Sometimes the Metamask will stuck at "Estimating transaction cost". I also tried by changing the gas limit and gas price. But still did not work. Commented Nov 10, 2018 at 15:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.