0

Let's assume I do a transaction to a contract function with my external account, then I have to pay gas for this transaction.

If the function within the contracts makes now a transaction on his own to write something to an other contracts, who is paying then for this?

I was testing this in my truffle develop environment and it looks like my account just paid for the transaction to the first contract, not for the second transaction.

I also didn't send any Ether to the first contracts before. But the second transaction worked as well.

  • Are transaction from contracts free?
  • Or is this just a special behaviour in my test environment to make thing easier?

edit:

The contract makes a simple transaction, something like this:

contract First {
    Second secondContract;
    uint test;

    function setSomething(uint _testValue) external {
        secondContract.set(_testValue); //also set a uint within contract 2
        test = _testValue;
    }
}

If I add the following line in my test, the result is 0 Ether because I never send Ether to this contract.

console.log(`Contract balance: ${web3.fromWei(web3.eth.getBalance(firstContact.address))}`);

The test still works and if i read the value from the second contract I also get the right value back.

  • 1
    do you maybe have some code as an example? When you fire a transaction from within a contract it is paid for by the contract itself. Transactions are never free. – jasper Aug 9 '18 at 12:24
  • 1
    I added an example above – user23858 Aug 9 '18 at 12:33
  • 1
    The gas used by the contract is calculated in on the initial to First call in this case! – jasper Aug 9 '18 at 12:36
  • 1
    Yes that is exactly what happens – jasper Aug 9 '18 at 12:41
  • 1
    Yes, there are fixed gas costs for certain op-codes! – jasper Aug 9 '18 at 12:52
1

The gas consumed by your transaction is the total gas used during the execution of your call to Contract A. If A makes a call (or delegatecall) to another contract, the gas consumed by that is also added to the gas consumed by your transaction.

If you look at the Parity trace for one such transaction, you can see the various other contracts invoked, and how much gas the invocation used. The final cost of the transaction includes all of this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy