4

I am pretty new to solidity. I am trying to create a smart contract which deploys another smart contract and transfers ether to the newly created contract when a payment greater than 0.01 ether is made to the parent contract. The deployment goes through, but when i try to send ether to the parent contract, i keep getting the "out of gas" error. Can someone please let me know what might be the issue?

Thanks!

pragma solidity ^0.4.8;

contract Bakery {
    address[] public contracts;
    address public ContractAddress;

    event LogForwarded(address indexed sender, uint amount);
    event LogFlushed(address indexed sender, uint amount);

    function() payable external {
        emit LogForwarded(msg.sender, msg.value);
        if(msg.value > 0.001 ether) {
            Cookie c = new Cookie();
            contracts.push(c);
            address(c).transfer(msg.value);
        }
    }
}

contract Cookie {
    address public destinationAddress;

    event LogForwarded(address indexed sender, uint amount);
    event LogFlushed(address indexed sender, uint amount);

    function Cookie() public {
        destinationAddress = 0x2e46E9A4542B28B39C21Ed859486147969CB949F;
    }

    function() payable external {
        emit LogForwarded(msg.sender, msg.value);
        destinationAddress.transfer(msg.value);
    }

    function flush() public {
        emit LogFlushed(msg.sender, address(this).balance);
        destinationAddress.transfer(address(this).balance);
    }
}
3

This particular code can't do that because <address>.transfer() only makes 2300 gas available to the recipient, which is not enough to make another ether transfer.

This could be made to work by providing more gas with the ether transfers, e.g. address(c).call.gas(50000)(""). This forwards 50,000 gas, which should be more than enough to make the subsequent transfer.

2

You should know that https://ethereum-magicians.org/t/remediations-for-eip-1283-reentrancy-bug/2434/29:

  1. EVM adds 2300 gas stipend in case you transferring > 0 weis
  2. Solidity compiler adds 2300 gas to call in case you transferring exactly 0 weis

Following code works 2 different ways depending on v:

a.transfer(v)
  1. If v > 0 EVM stipends 2300 of gas, Solidity compiler stipends 0 of gas

  2. If v == 0 EVM stipends 0 of gas, Solidity compiler stipends 2300 of gas

So transfer internally equals to the following code:

a.call.gas(v == 0 ? 2300 : 0).value(v)();

So you CAN'T forward funds by providing less gas than 2300, because EVM will try to provide additional 2300 stipend:

contract A {
    address to = 0x***;
    function() payable external {
        // Will try to give 1150+2300 of gas
        require(to.call.gas(1150).value(msg.value)());
    }
}
0

Gas is the generic measurement of the "price" of your code. All actions in the blockchain cost gas and the more complex your contract (and the transaction calling the contract) the more gas it requires. Gas is bought directly with Ethers.

You should start by reading more about gas and how it works, for example here: What is meant by the term "gas"? .

If your transactions runs out of gas it means you did not give enough gas for the transaction. Each transaction has a set gas limit which the transaction is not allowed to exceed. The limit is set by you, although some clients may hide the functionality. When the gas limit is reached, the whole transaction is reverted and it's considered as failed. So your transaction failed because it was trying to do too many things with too little gas.

Basically you can just increase your gas limit and that should do it (unless there are other errors in the execution). It's probably easiest to test it in Remix (http://remix.ethereum.org) with a JavaScript VM (so you don't interact with a real blockchain yet).

  • Great! thanks. I tried increasing the gas limit to literally the current maximum gas limit as displayed on ethstats. The transaction still failed due to out of gas. I am actually executing the transactions on the testnet. You can find the tx here: ropsten.etherscan.io/tx/… – Arvind Jan 22 at 19:09

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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