1

This is function I am using to transfer ethers from user account to contract account.

function addEathersToContractAccount(address contractAddress)public payable returns(bool){
                changePaymentStatus(2);
                bool success= contractAddress.send(msg.value)
            if(success){
                changePaymentStatus(3);
                return true; 
            }else{
                return false;
            }
    }

while testing it in REMIX I have found that this function always returns false but the ethers got transferred from user account to contract account.

Again I have tried it using ethereum testrpc but the result was same.

I have also tried

contractAddress.transfer(msg.value)

This is the output provided by remix enter image description here

dose any one has any idea why this happens.

  • Use contractAddress.transfer(msg.value) or require(contractAddress.send(msg.value)) – Prashant Prabhakar Singh Mar 28 '18 at 10:36
1

send returns false on failure, and it's up to you to handle the failure. That's why, except in some cases where you want/need to handle the failures yourself, it's better to use transfer, which will take care of that for you.

In your case, here's what you should do.


function addEathersToContractAccount(address contractAddress)public payable returns(bool){
                changePaymentStatus(2);
                bool success= contractAddress.send(msg.value)
            if(success){
                changePaymentStatus(3);
            }else{
                revert();
            }
            return success;
    }

revert allows you to revert the changes made and to return a value.

You still should use this :

function addEathersToContractAccount(address contractAddress)public payable returns(bool){
                changePaymentStatus(2);
                contractAddress.transfer(msg.value);
                changePaymentStatus(3);
                return success;
    }
  • After replacing my function with your code the result is same, function returns false but ethers got transferred. – SwapnilKumbhar Mar 28 '18 at 10:46
  • @SwapnilKumbhar did you deploy through remix? – Eli Drion Mar 28 '18 at 10:58
  • 1
    @SwapnilKumbhar You must need to revert in case the send fails manually. if you don't the send will return false but the eth send with msg.value will be consumed. Check my comment above. – Prashant Prabhakar Singh Mar 28 '18 at 10:59
  • 1
    @SwapnilKumbhar Remix has sometimes issues where even if you update the code, it will still deploy the old one. Best to go on remix on incognito mode or to empty your cache. – Eli Drion Mar 28 '18 at 11:01
  • @PrashantPrabhakarSingh Yes, thanks for TIP, but the question is why dose it return false even after successfully transferring the ethers. – SwapnilKumbhar Mar 29 '18 at 7:40

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.