0

The following Smart Contract is supposed to simply send any ethers received back to the sender:

pragma solidity ^0.4.0;

contract ExampleSmartContract {


  function() payable {

      msg.sender.transfer(msg.value);

  }

}

I published the contract to the Rinkeby Test Network. When I tried to send 1 Ether from my Account to the address of the Smart Contract, I expected to get 1 Ether back.

However, when trying to send one ether to the smart contract, I get the following error message in MetaMask:

enter image description here

Does anybody know how to write the contract in such a way that it sends any ethers received back to the sender?

  • 1
    Your code is ok tho. – Eli Drion Apr 14 '18 at 22:40
  • 1
    Your code is correct, so you should focus on two assumptions a) it was published successfully and b) you're sending funds to it. Consider publishing the address of your contract and adding more details about the send attempt so we can help you pinpoint the issue. – Rob Hitchens - B9lab Apr 15 '18 at 0:45
  • Given piece of code is working fine as it is. – Ayushya Apr 15 '18 at 5:21
2

You are on the right track, and the code you provided is working perfectly Ok. You can verify this by using this rinkeby instance:

https://rinkeby.etherscan.io/address/0xc0723ee73363d244ed0e400a5c9106e0020a74c4

pragma solidity ^0.4.21;

contract ExampleSmartContract {


  function() public payable {

      msg.sender.transfer(msg.value);

  }

}

Following things work:

  • Sending ether to the contract by calling fallback function in remix
  • Sending ether to the contract address using metamask directly.

If there is anything else that you are doing, please update the question and we will try finding out what is wrong.

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.