1

Let's say I am creating a contract from another contract by calling a function in a parent contract, is the gas required to create new contract included in running the function that creates new contract?

pragma solidity ^0.4.15;

contract Test {
    bool public active = true;
    address public owner;
    address public contract_address;

    uint public amountReceived;

    address[] public activeContracts;
    uint256 public gasLeft;

    function () payable {
        amountReceived = msg.value;
        gasLeft = msg.gas;
    }

    function Test() {
        owner = msg.sender;
        contract_address = this;
        gasLeft = msg.gas;
    }

    function updateStatus() {
        active = false;
    }

    function kill() {
        selfdestruct(owner);
        gasLeft = msg.gas;
    }

    function createSubContract() {
        address newContractAddress = new SubTest();
        activeContracts.push(newContractAddress);
        gasLeft = msg.gas;
    }
}

contract SubTest {

    address public owner;
    address public contract_address;

    uint256 public amountReceived;
    uint256 public gasLeft;

    function () payable {
        amountReceived = msg.value;
        gasLeft = msg.gas;
    }

    function SubTest() {
        owner = msg.sender;
        contract_address = this;
        gasLeft = msg.gas;
    }

    function kill() {
        selfdestruct(owner);
        gasLeft = msg.gas;
    }

}

Here is a sample contract I am testing. Inorder to create a subcontract I call the function createSubContract, does gas required for calling this function include the gas required to create the contract?

marked as duplicate by Richard Horrocks, Achala Dissanayake, Ismael, lungj, Community Oct 1 '17 at 10:45

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Yes. You have to take care of assigning enough gas to your createSubContract() function call to be able to cover all the gas usage of all the subsequent calls originated from createSubContract().

Although this gas assignment to sub-calls can be done in a more fine tuned way, i.e. you don't have to forward all the gas of your transaction to the sub-call as it is explained here:

A contract can decide how much of its remaining gas should be sent with the inner message call and how much it wants to retain. If an out-of-gas exception happens in the inner call (or any other exception), this will be signalled by an error value put onto the stack. In this case, only the gas sent together with the call is used up. In Solidity, the calling contract causes a manual exception by default in such situations, so that exceptions “bubble up” the call stack.

In the future, the Metropolis hardfork will allow contracts to pay for gas. Check it out here the EIP 86 (account security abstraction) paragraph.

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