1

I have been stuck on this for days trying to figure out why I am unable to create new contract instances from inside my parent. I am working on a larger project and couldn't understand so I created an extremely simple case to work on. Both my project and my simple project work correctly when using Remix Solidity Web but fail when I try to deploy using Truffle/testrpc. I downgraded from truffle 4.0.2 to 3.4.6. I am also using webpack to create the page.

Child.sol:

pragma solidity ^0.4.13;


contract child {
    int b;
    function child(int a) {
        b = a;
    }
    function getChild() returns (int abc) {
        return b;
    }
}

Parent.sol:

pragma solidity ^0.4.13;

import "./child.sol";

contract parent {

    uint pls;

    function parent(uint a) {
        pls = a;
    }

    function tester() returns (uint) {
        return pls;
    }

    function make() returns (child abc) {
        return new child(16); 
    }
}

Here is my simple deploy script: 2_deploy_contracts.js

var parent = artifacts.require("./parent.sol");
module.exports = function(deployer, accounts) {
    deployer.deploy(parent,20);

};

Lastly here is my app.js. My html page is blank and I am only debugging using console.

// Import the page's CSS. Webpack will know what to do with it.
import "../stylesheets/app.css";

// Import libraries we need.
import { default as Web3} from 'web3';
import { default as contract } from 'truffle-contract'

// Import our contract artifacts and turn them into usable abstractions.
import metacoin_artifacts from '../../build/contracts/parent.json'

// MetaCoin is our usable abstraction, which we'll use through the code below.
var MetaCoin = contract(metacoin_artifacts);

// The following code is simple to show off interacting with your contracts.
// As your needs grow you will likely need to change its form and structure.
// For application bootstrapping, check out window.addEventListener below.
var accounts;
var account;

window.App = {
  start: function() {
    var self = this;

    // Bootstrap the MetaCoin abstraction for Use.
    MetaCoin.setProvider(web3.currentProvider);

    // Get the initial account balance so it can be displayed.
    web3.eth.getAccounts(function(err, accs) {
      if (err != null) {
        alert("There was an error fetching your accounts.");
        return;
      }

      if (accs.length == 0) {
        alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
        return;
      }

      accounts = accs;
      account = accounts[0];
    });

    var temp;
    MetaCoin.deployed().then(function(instance) {
      temp = instance;
      console.log(instance.address);
      temp.make({from: account});
    }).then(function(abc) {
      console.log(abc);
    }).catch(function(e) {
      console.log(e);
    })


  }
};

window.addEventListener('load', function() {
  // Checking if Web3 has been injected by the browser (Mist/MetaMask)
  if (typeof web3 !== 'undefined') {
    console.warn("Using web3 detected from external source. If you find that your accounts don't appear or you have 0 MetaCoin, ensure you've configured that source properly. If using MetaMask, see the following link. Feel free to delete this warning. :) http://truffleframework.com/tutorials/truffle-and-metamask")
    // Use Mist/MetaMask's provider
    window.web3 = new Web3(web3.currentProvider);
  } else {
    console.warn("No web3 detected. Falling back to http://127.0.0.1:9545. You should remove this fallback when you deploy live, as it's inherently insecure. Consider switching to Metamask for development. More info here: http://truffleframework.com/tutorials/truffle-and-metamask");
    // fallback - use your fallback strategy (local node / hosted node + in-dapp id mgmt / fail)
    window.web3 = new Web3(new Web3.providers.HttpProvider("http://127.0.0.1:8545"));
  }

  App.start();
});

The error I get when trying to create a new contract for both my large project and this example project are the same:

Uncaught (in promise) Error: VM Exception while processing transaction: revert at Object.InvalidResponse (app.js:10361) at app.js:48279 at XMLHttpRequest.request.onreadystatechange (app.js:50906)

I did a bit of debugging with truffle 4.0 before downgrading to see if it was the problem. Here is the error that I reached when it came to creating the new contract object:

Warning: The source code for one or more contracts could not be found. This is the only source i was able to find this error: https://github.com/trufflesuite/truffle/issues/576

When I try this on remix browser solidity, my create function returns the address of the newly created contract with zero problems. Please help me, I have been banging my head against a wall for hours.

3
  • Did you try passing gas along with from in the make function call? Jan 10, 2018 at 12:03
  • you can use web3.js to call the contract functions. It works without promises. Jan 10, 2018 at 12:04
  • Adding gas fixed the problem, I was so used to using browser solidity that it completely slipped my mind Jan 11, 2018 at 0:49

1 Answer 1

2

I was able to fix this by adding in a gas parameter next to accounts.

  temp.make({from: account, gas : 50000000});

Your Answer

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

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