1

I have a function:

function sendEther(address payable _to, uint amount) public  payable returns (bool) {
        bool success = _to.send(amount);
        return success;
    }

Now, how transferring funds work, is it first tries to grab the value from the caller's transaction and if it's not enough, then grabs the contract's funds and moves it to _to address.

Now, let's say that my contract address has 1 ether. contract has a function which moves 1 ether from it to another address. User calls this function. But user puts 0 ether while calling a function. If that's so, the function would still send 1 ether even though user put 0 ether.

Is this how it's supposed to be ? what if i don't want users to be able to lose my contract's funds ? they can call this function, but it should be their money that gets sent from my contract to another, not the actual contract's funds. I guess i'd need a require statement to check msg.value field right ?

Do you agree with me ?

1 Answer 1

1

Is this how it's supposed to be?

No, you can (and should) omit the uint amount parameter.

Instead, the user should pass the desired amount within the msg.value.

This way, the user (and not your contract) will be the one providing the funds.

3
  • Yes, it makes sense, but what i explained there how it works is correct, right ? other than that, about the solution, i agree. Oct 5, 2020 at 21:20
  • @NikaKurashvili: BTW, you can (and should) get rid of the boolean return-value, and replace the send with transfer (which reverts upon failure instead of returning status). Oct 5, 2020 at 21:23
  • Yes ! it's just an example code. Oct 5, 2020 at 21:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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