1

Say, I am creating a contract from another contract's function. AND I want to transfer certain amount (1 ether) to be added to contract's balance after it is created. Will this 1 ether and gas price be independent? I mean, if I run out of gas, I want that 1 ether to remain untouched, and if a contract is created successfully, I want it to get 1 ether, and not 1 ether - gas fee. Will it work as I described?

  • Gas is always paid by the tx.origin. Gas is not subtracted from mgs.value – libertylocked Nov 13 '17 at 17:30
1

The following code deploys a B instance and send 1 ether when you call newB() on A.

pragma solidity 0.4.18;

contract A {
    function A() public {}
    function() public payable { }
    function newB() public returns (address) {
        address bInstance = new B();
        bInstance.transfer(1 ether);
        return bInstance;
    }
}

contract B {
    function() public payable { }
}

When you call myA.newB(), you are the supplier of the initial gas. That is the total amount of gas you have in the whole transaction, and the transaction cannot refuel halfway. (This is effectively to prevent DOS attacks or re-entrancy)

So when your contract A instance creates an internal transaction within your transaction, it still spends the gas you initially provided (typically with a gas stipend).

So yes, when you send 1 ether you always send 1 ether, not 1 ether - gas. The gas isn't even paid by contract A - it's paid by you.

0

If your transaction runs out of gas during execution, then the entire transaction is rolled back. You're still charged the gas, but all state changes are reversed. This means if you sent 1 ETH to the contract that create a contract, then you get it back. The contract that the contract creates also wont be created, it'll be rolled back, since running out of gas rolls everything back.

So yes, the contract will either be created and get 1 ETH or, if it runs out of gas, it will not be created and wont get 1 ETH. The 1 ETH you send is also completely separate from gas, so if you do run out of gas, it wont use any of the ETH you also sent to the contract.

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.