1

Consider we a function as follows :

address public recipient; 

function settlement(uint256 amount) public {

        recipient = msg.sender;
        recipient.transfer(amount);

    }

Then the recipient by invoking this function as follows (using web3.js), tries to receive the amount :

contractInstance.methods.settlement(10).send({ from: '0xca35b7d915458ef540ade6068dfe2f44e8fa733c' }, function(error, result) {
    console.log(error);
    console.log(result)
});

So, the address 0xca35b7d915458ef540ade6068dfe2f44e8fa733c must receive 10 wei, but from which address ? Who is the payer ? and this 10 wei has been reduced from which account address ?

Meanwhile, since we did not use payable for the settlement function, this ether transfer will be done correctly ?

UPDATE : In case we mark payable in the constructor of the contract, then does we need to mark the settlement function ? And the recipient will be sent the amount from the balance of the contract ?

3

This line:

recipient.transfer(amount);

transfers amount wei from the contract's balance to the recipient. This line:

recipient = msg.sender;

means that the recipient is the account that called the function.

So when you call the function from the address 0xca35b7d915458ef540ade6068dfe2f44e8fa733c, with the parameter 10, that address will receive 10 wei, paid from the contract. (This is assuming that the contract call succeeds, which it should as long as the contract has at least 10 wei.)

Meanwhile, since we did not use payable for the settlement function, this ether transfer will be done correctly ?

Yes, a function only needs to be marked payable if it's going to receive ether. You don't need to mark functions that send ether.

This blog post might help understand the basics of receiving and sending ether: https://programtheblockchain.com/posts/2017/12/15/writing-a-contract-that-handles-ether/.

1

There will be no transfer of value, since your code calling the function does not send any. If you would mark the function payable, the value would first be added to the smart contract's balance, and transfered to the recipient from there.

  • Thanks, so do you mean that if I mark function settlement(uint256 amount) public payable {recipient = msg.sender; recipient.transfer(amount);} then the ether will be transferred to the 0xca35b7d915458ef540ade6068dfe2f44e8fa733c ? but we did not determine who is the payer yet ? How can we do it ? Thank you – Questioner Jul 10 '18 at 14:13
  • 1
    as you set the recipient to be the msg.sender, the ether will actually "return" to you. The sender of the ether is your account, if you send some ether along with the transaction. You can also build a contract of course, that can store value and allow you (or whoever) to transfer the ether it holds. – Nikita Fuchs Jul 10 '18 at 14:23
  • Thanks, so if I well understood, in case the contract 1. has a balance and 2. is payable, then I can call the settlement function with the form of contractInstance.methods.settlement(10).send({ from: '0xca35b7d915458ef540ade6068dfe2f44e8fa733c' }, function(error, result) {console.log(error);console.log(result)}); and without need for marking payable for settlement since the contract is payable. I'm right ? Thank you – Questioner Jul 10 '18 at 14:36
  • "There will be no transfer of value, since your function does not send any." Could you explain what you mean? The function is only two lines long, and the second line transfers ether. – user19510 Jul 10 '18 at 15:54
  • My bad smarx, I meant to say "the code he uses to call the function". Sas: Depending on your usecase your contract function can accept ether >and< transfer it somehwere (all in one transaction), or just transfer some ether it already holds >without< accepting any ether in that particular function call/transaction. You're the boss of your piggybank, you can 1. throw something in, 2. throw something in and let some pennies drop out, too, or 3. just let some pennies drop out. – Nikita Fuchs Jul 11 '18 at 0:22

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.