0

I try to transfer ether from one contract B to contract A. But I get "Transaction mined but execution failed"

Code:

pragma solidity ^0.5.0;

contract A {
  address payable public owner;
  uint public block_number;

  constructor () public payable {
    owner = msg.sender;
  }

  function getBalance () public view returns (uint) {
      return address(this).balance;
  }

  function () external payable {
      block_number = block.number; 
  }
}

contract B {
  address payable public owner;
  address payable public contract_a = 0x600F51a64e8A703b759EB3d735132d6C802A0a7b;  //contract's A address after deploy

  constructor () public payable {
    owner = msg.sender;
  }

  function () external payable {
      contract_a.transfer(msg.value);
  }
}

At first I deploy A and copy and paste address to B. Then I send ether from wallet to B and get error. What am I doing wrong? If I send ether from wallet directly to A it's successful.

3

Fallback function in Solidity is provided only 2300 gas to get executed successfully. As a result, inside a Fallback function, the operations like sending ether, deploying contracts which consume more gas than 2300 gas cannot be performed.

In contract A, fallback function accesses block number and saves it only. So it works well. But in case of contract B, the fallback function sends ether to contract A which exceeds the gas amount available for fallback. As a result, it gets reverted.

For further explanation, take a look here: FallbackFunction in Solidity

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.