4

I wrote a contract A that creates a new contract B and returns the address of the new contract B that's been created. When I tested this on solidity browser it seems to work well, but when I tried to run the create method using web3, I keep getting this error:

        throw errors.InvalidResponse(result);
    ^
Error: VM Exception while processing transaction: out of gas

Here's my contract code:

pragma solidity ^0.4.2; 
contract B { 
   int public id;
   function B(int _id) { 
     id = _id;
   }
} 

contract A { 
   address Baddress;
   function createB(int _id) returns (address) { 
     Baddress = new B(_id); 
     return Baddress;
   } 
}

Here's my Javascript code that attempts to run A's createB method:

web3.eth.defaultAccount = web3.eth.coinbase;
var Acontract = web3.eth.contract([{"constant":false,"inputs":[],"name":"getdum","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"num","type":"int256"}],"name":"create","outputs":[],"payable":false,"type":"function"}]);

var A = Acontract.new({from: web3.eth.accounts[0], 
data: '{{compiled byte code}}', 
gas: '47000000'}, function (e, contract){
   console.log(e, contract);
   if (typeof contract.address !== 'undefined') {
     console.log('Contract mined! address: ' + contract.address);
     contract.createB(5000).then(console.log); // This line throws the error
   }
})

I've read many other related posts and could only find answers that suggest to use more gas, but no matter how much gas I sent with the transaction, it would throw the same error. You can see I'm using a really large value for gas in the code. Can anyone help me identify what I'm doing wrong? I'm using solc Version: 0.4.9+commit.364da425.Darwin.appleclang, EthereumJS TestRPC v3.0.3, and node v6.4.0.

  • Are you using truffle? Also, I think you may have a typo in your code .create(5000) vs. .createB(5000) – Matthew Schmidt Mar 12 '17 at 23:42
  • Thank you for noticing the typo. I accidentally deleted the B there... I did try to use truffle, but when I used this command: A.deployed().then(function(instance){return instance.createB(50)}).then(console.log), it returned a null contract address. How do I get newly created B's contract address..? – thalsky Mar 12 '17 at 23:49
  • I actually don't use truffle, but the promisified methods aren't native to web3 (yet!). .createB().then() won't work. That said, you can use a library like bluebird to promisify web3 anyway. – Matthew Schmidt Mar 12 '17 at 23:51
  • Oh I see. Although, do you think I can use createB() without a promise? – thalsky Mar 12 '17 at 23:52
  • You can, it'll just return a transaction hash rather than a thenable. The standard way is to do that, then filter for an event the transaction creates. – Matthew Schmidt Mar 12 '17 at 23:54
2

After replicating this, I discovered the issue.

You'll need to specify the gas again when you send the .createB() transaction. That is, if you use .createB(5000), it only uses the default gas limit of 90000 and fails. Setting the gas limit to higher, with .createB(5000, {gas: 200000}) was successful for me. browser-solidity gives a more accurate value of 113598 for the transaction's cost.

  • Thank you! This indeed was the gas supply I missed. However, I'm still unable to get correct ID value of B due to an error of "invalid address" after calling B from its contract address returned in A. I've followed the example on Web3's documentation page using web3.eth.contract(abi).at(baddress).getB() where it threw that error. Although the previous error was resolved, this is where I had problems before on truffle and now web3. – thalsky Mar 13 '17 at 0:46
  • Nvm! I fixed it after looking up eth.filter in your last comment. Thank you again for your help!! – thalsky Mar 13 '17 at 1:46
  • cheers for this! spend ours debugging. – Katafalkas Jan 6 '18 at 23:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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