Solidity: 'msg.sender.send' means sending ether to the sender?

Created on 20 Oct 2017  ·  5Comments  ·  Source: ethereum/solidity

Hi guys,

I'm reading the official tutorial here:

https://ethereum.org/token

and in this part:

    function buy() payable returns (uint amount){
        amount = msg.value / buyPrice;                    // calculates the amount
        require(balanceOf[this] >= amount);               // checks if it has enough to sell
        balanceOf[msg.sender] += amount;                  // adds the amount to buyer's balance
        balanceOf[this] -= amount;                        // subtracts amount from seller's balance
        Transfer(this, msg.sender, amount);               // execute an event reflecting the change
        return amount;                                    // ends function and returns
    }

    function sell(uint amount) returns (uint revenue){
        require(balanceOf[msg.sender] >= amount);         // checks if the sender has enough to sell
        balanceOf[this] += amount;                        // adds the amount to owner's balance
        balanceOf[msg.sender] -= amount;                  // subtracts the amount from seller's balance
        revenue = amount * sellPrice;
        require(msg.sender.send(revenue));                // sends ether to the seller: it's important to do this last to prevent recursion attacks
        Transfer(msg.sender, this, amount);               // executes an event reflecting on the change
        return revenue;                                   // ends function and returns
    }

I found the code looks a bit weird:

        require(msg.sender.send(revenue));                // sends ether to the seller: it's important to do this last to prevent recursion attacks

Because 'msg.sender.send' looks like the sender is sending ether to someone, not receiving from someone.

I'm just curious about the design, and maybe renaming it to something else could be better?

Most helpful comment

msg.sender is an address object with send (and transfer) being a
method of address.

On 20 October 2017 at 17:31, 尤 川豪 notifications@github.com wrote:

Hi, thanks for the reply.
Now I know 'msg.sender.send' means the contract sends ether to
'msg.sender'.
I just feel the syntax itself is a bit confusing.
Because 'msg.sender.send' looks like sending ether from the sender.
Anyway ... not big issue ... thanks!


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/ethereum/solidity/issues/3115#issuecomment-338128671,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AMMqOh6t-4RATGYAhMhF86LDA_CvrWR4ks5suExsgaJpZM4QAQpB
.

All 5 comments

I saw the doc:

http://solidity.readthedocs.io/en/develop/types.html#members-of-addresses

So the 'send' is the reverse action of 'transfer'.

I'm thinking maybe something like 'receive' could be more clear?

msg.sender is selling tokens to the contract and receiving ether in
return, so the contract must send ether to msg.sender.

On 20 October 2017 at 17:10, 尤 川豪 notifications@github.com wrote:

I saw the doc:

http://solidity.readthedocs.io/en/develop/types.html#members-of-addresses

So the 'send' is the reverse action of 'transfer'.

I'm thinking maybe something like 'receive' could be more clear?


You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/ethereum/solidity/issues/3115#issuecomment-338124676,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AMMqOkXhQA2QW7jZCfzTrOitk3GLzvIZks5suEdogaJpZM4QAQpB
.

Hi, thanks for the reply.
Now I know 'msg.sender.send' means the contract sends ether to 'msg.sender'.
I just feel the syntax itself is a bit confusing.
Because 'msg.sender.send' looks like sending ether from the sender, not to the sender.
Anyway ... not big issue ... thanks!

msg.sender is an address object with send (and transfer) being a
method of address.

On 20 October 2017 at 17:31, 尤 川豪 notifications@github.com wrote:

Hi, thanks for the reply.
Now I know 'msg.sender.send' means the contract sends ether to
'msg.sender'.
I just feel the syntax itself is a bit confusing.
Because 'msg.sender.send' looks like sending ether from the sender.
Anyway ... not big issue ... thanks!


You are receiving this because you commented.
Reply to this email directly, view it on GitHub
https://github.com/ethereum/solidity/issues/3115#issuecomment-338128671,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AMMqOh6t-4RATGYAhMhF86LDA_CvrWR4ks5suExsgaJpZM4QAQpB
.

Didn't write any Solidity code for few months and now in 2019 I googled the same question just to be sure. I find it also a bit confusing but @o0ragman0o explanation is definitely clear. Not suggesting it to change but smth like this would have been more clear imo. as people are used to such a syntax more:

this.transfer(msg.sender);

Where "this" would refer to SC.

Was this page helpful?
0 / 5 - 0 ratings