I am able to reproduce issue that you were facing. I did following to solve this issue.
Created two separate contract files inside contracts folder for SimpleStore and ContrctFactory as following :-
SimpleStore.sol file -
pragma solidity ^0.4.16;
contract SimpleStore {
uint public value;
function SimpleStore (uint num) public {
value = num;
}
function setValue (uint num) public {
value = num;
}
}
ContractFactory.sol file -
pragma solidity ^0.4.16;
contract SimpleStore {
uint public value;
function SimpleStore (uint num) public {
value = num;
}
function setValue (uint num) public {
value = num;
}
}
contract ContractFactory {
address public store;
function ContractFactory () public {
}
function createStore () public {
store = new SimpleStore(12);
}
}
2_deploy_contracts.js file -
var SimpleStore = artifacts.require("./SimpleStore.sol");
var ContractFactory = artifacts.require("./ContractFactory.sol");
module.exports = function(deployer) {
deployer.deploy(SimpleStore,10, {gas: 6700000});
deployer.deploy(ContractFactory,{gas: 6700000});
};
app.js file -
import { default as Web3} from 'web3';
import { default as contract } from 'truffle-contract'
import SimpleStore_artifacts from '../../build/contracts/SimpleStore.json'
import ContractFactory_artifacts from '../../build/contracts/ContractFactory.json'
var SimpleStore = contract(SimpleStore_artifacts);
var ContractFactory = contract(ContractFactory_artifacts);
$( document ).ready(function() {
if (typeof web3 !== 'undefined') {
console.warn("Using web3 detected from external source like Metamask")
// Use Mist/MetaMask's provider
window.web3 = new Web3(web3.currentProvider);
} else {
console.warn("No web3 detected. Falling back to http://localhost:8545. 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://localhost:8545"));
}
SimpleStore.setProvider(web3.currentProvider);
ContractFactory.setProvider(web3.currentProvider);
ContractFactory.deployed().then(function(instance) {
return instance.createStore({from: web3.eth.accounts[0],gas: 6700000});
console.log("******");
});
});
Adding gas cost as an argument has resolved problem of VM exception.
instance.createStore({from: web3.eth.accounts[0],gas: 6700000});
I am also able to execute transaction from truffle console -
truffle(development)> ContractFactory.deployed().then(function (instance) { return instance.createStore({from: web3.eth.accounts[0],gas: 6700000});})
{ tx: '0x4d34e45cddbfa6f087f5dcb02bb5e7dc0fa18a0f7d24e87f8fff8702dd3e53bc',
receipt:
{ transactionHash: '0x4d34e45cddbfa6f087f5dcb02bb5e7dc0fa18a0f7d24e87f8fff8702dd3e53bc',
transactionIndex: 0,
blockHash: '0xf77b2baa3cacc941a8376376425c642c6db580e4a26918a0905ed589a618bfe7',
blockNumber: 26,
gasUsed: 123078,
cumulativeGasUsed: 123078,
contractAddress: null,
Best Answer
If you're running in a browser with MetaMask, then
Web3
is injected as a side effect of this (although try not to rely on this behaviour). In server-side code, you must import web3 yourself, by putting something like:near the start of your code. You may also have to
npm install web3
oryarn install web3
, and if you're using BigNumber (which they no longer bundle with Web3 1.0) then you probably want to specifynpm install web3@^0.20.6
oryarn install web3@^0.20.6
.