This should be really easy but I can't seem to find the way to do it, thanks to the great documentation. I'm thinking I'm missing something big.
I have a function that buys ownership of something. I want to remove amount
ether to msg.sender
(user a) and give them to owner
(user b) (minus fees).
Can I do it directly? Do I have to first let a send ether to the contract and then send b the same amount, or is there a function to transfer directly?
I assume owner.transfer(amount)
(or .send(amount)
) sends ether from the contract to the owner, but then how do I get ether from a?
I'm using Solidity by Example as a model, but in the Blind Auction example I can't seem to find what function actually sends ether from the bidder to the contract.
Best Answer
You can't just take somebody's ether, so yes, they need to send it to the contract first, but this can all be done in a single contract call. E.g.
When calling this function, the caller must attach
price
ether. This means setting the "value" field of the transaction.I think this blog post will help: https://programtheblockchain.com/posts/2017/12/15/writing-a-contract-that-handles-ether/.
EDIT
You linked to a very old version of the Solidity documentation, before
payable
was required to allow ether to be sent. The current version shows apayable
bid
function. That's where ether is transferred to the contract.