A contract SimpleStorage.sol has been compiled by Truffle using truffle compile. Its artifact is found at build/contracts/SimpleStorage.json.

However, this contract should only be deployed to the Ethereum network when a user clicks a button.

When handling the button click, what should be done to deploy the artifact once I get the TruffleContract object?

Can we also get the transaction hash and contract address after it is deployed?

import SimpleStorageContract from '../../../build/contracts/SimpleStorage.json';

onDeployButtonClick() {
    const contract = require('truffle-contract');
    const simpleStorage = contract(SimpleStorageContract);

    // How to deploy this contract to the Ethereum network?
    // then get the TX hash and contract address

Tried this code on testrpc

onDeployButtonClick() {
    const contract = require('truffle-contract');
    const simpleStorage = contract(SimpleStorageContract);

    this.props.web3.eth.getAccounts((error, accounts) => {
        simpleStorage.new([], {from: accounts[0]})

but in the JS console, when onDeployButtonClick is triggered, I get an error

Uncaught (in promise) Error: VM Exception while processing transaction: out of gas
    at Object.InvalidResponse (errors.js:35)
    at requestmanager.js:86
    at XMLHttpRequest.r
  • simpleStorage.new().then(instance => {...}) See docs on the new method Nov 13, 2017 at 20:14
  • @LibertyLocked Tried it out, but getting an error.. any ideas? I'm running on testrpc. Updated original post
    – Nyxynyx
    Nov 13, 2017 at 20:20
  • If you run out of gas on testrpc just give it more gas Nov 13, 2017 at 20:26
  • @LibertyLocked Finally worked when I adjusted gas to 200000. Is there a way to estimate how much gas is need? Is there an auto option for setting gas?
    – Nyxynyx
    Nov 13, 2017 at 20:29
  • It does auto estimate gas but I'm guessing it might be a bug in testrpc. It usually works fine in geth without specifying gas though. Nov 13, 2017 at 20:40

2 Answers 2


With Truffle contract you can do it easily with the factory you constructed from the ABI.

const Contract = require('truffle-contract');
const SimpleStorage = Contract(SimpleStorageContract);

// deploy from accounts[0]
SimpleStorage.new({from: this.props.web3.eth.accounts[0], gas: 6000000}).then(instance => {

Adjust the gas accordingly.

  • On testrpc, the address of the newly deployed contract returns instantly. Will it take longer on mainnet/testnet?
    – Nyxynyx
    Nov 13, 2017 at 20:30
  • Yes, since you will have to wait for the TX to be mined. Nov 13, 2017 at 20:34
  • Is it possible to get the transaction hash of this contract creation?
    – Nyxynyx
    Nov 13, 2017 at 22:58
  • instance.transactionHash gives you the hash of the contract creation tx Nov 14, 2017 at 1:31

The best way to achieve this would be to have 2 contracts. Your SimpleStorage contract, as it is, and a SimpleStorageFactory contract which uses the Factory pattern.

You would deploy the Factory contract which has a function that gets called when you click the button. This function basically creates a new instance of SimpleStorage contract. Something like this:

function createSStorage() {
    SimpleStorage s = new SimpleStorage(); // You can pass other constructor parameters.

    //Store the SStorages created in either an array or mapping so you can later list them, for example.

    //Array of SStorages

    // Mapping of SStorages with address key
    SimpleStorageMapping[address(s)] = s;

  • You answered my next question! Thanks for introducing the factory pattern in smart contrats to me!
    – Nyxynyx
    Nov 13, 2017 at 20:31

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.