Payment Processing
Learn about making payments to and from a Solidity contract.
Sending money to a contract
Contracts have an address on the Ethereum network, just like EOAs; therefore, they can receive and send ETH.
Payable functions
A contract is able to receive ETH, for a specific purpose, by making the corresponding function payable using the payable keyword. ETH can then be sent as part of a call to that function.
For example, in the following contract, we define a deposit() function, allowing addresses (the msg.sender) to send ETH to the contract.
All ETH sent to a contract is held indistinctly in its total balance. So, a contract must include mechanisms to keep track of what belongs to which address.
In the below contract, the amount sent (msg.value) is added to the balance of the msg.sender, which we keep track of using the mapping defined in line 3.
pragma solidity >=0.5.0 <0.9.0;contract exampleDeposit {mapping(address => uint256) public balance;function deposit() public payable {balance[msg.sender] += msg.value;}}
The receive function
On Ethereum, contract accounts have addresses that are indistinguishable from those ...