Reputation: 8570
I understand msg.value
represents how much wei the sender sent. But how does a contract use it?
In the example of a vending machine, the contract checks to ensure enough msg.value
is present to cover the cupcakes ordered, but there's no code that actually deducts the wei.
Example: https://ethereum.org/en/developers/docs/smart-contracts/#a-digital-vending-machine
I do see some info about buyer and seller.transfer()
here:
https://docs.soliditylang.org/en/v0.8.15/solidity-by-example.html#safe-remote-purchase
Upvotes: 1
Views: 3085
Reputation: 238
A contract has no need for deducting to receive ETH or msg.value
. The sender will determine ETH payment amount.
ETH receiving contract only need payable
modifier to indicate that it can receive ETH. We can say it is like a vending machine in the way that the contract cannot determine how much it will receive, but you can refund by transferring the exceeding ETH back to the sender.
Unlike ERC20 which works differently, it does needs to be approved and deducted for the general contract purchase function.
Read more:
Upvotes: 1
Reputation: 126
Typically, this code is paired with a dapp front-end for the user to operate, so if you are a developer who wants to send ETH when calling a contract, I recommend doing this
Upvotes: 1