Functions and addresses declared payable can receive ether into the contract.
payable으로 선언된 함수와 주소는 계약을 통해 이더를 받을 수 있다.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;
contract Payable {
// Payable address can receive Ether
// Payable로 선언된 주소는 이더를 받을 수 있다.
address payable public owner;
// Payable constructor can receive Ether
// Payable로 선언된 constructor는 이더를 받을 수 있다.
constructor() payable {
owner = payable(msg.sender);
}
// Function to deposit Ether into this contract.
// Call this function along with some Ether.
// The balance of this contract will be automatically updated.
// 이 계약에 Ether를 입금하는 기능.
// 이 함수를 Ether와 함께 호출합니다.
// 이 계약의 잔액은 자동으로 업데이트됩니다.
function deposit() public payable {}
// Call this function along with some Ether.
// The function will throw an error since this function is not payable.
// 이 함수를 Ether와 함께 호출합니다.
// 이 함수는 지불할 수 없기 때문에 함수가 오류를 발생시킵니다.
function notPayable() public {}
// Function to withdraw all Ether from this contract.
// 이 계약에서 모든 이더를 인출하는 기능.
function withdraw() public {
// get the amount of Ether stored in this contract
// 이 계약에 저장된 이더의 양을 가지고 오다
uint amount = address(this).balance;
// send all Ether to owner
// Owner can receive Ether since the address of owner is payable
// 이더를 모두 소유자에게 보내다
// 소유자의 주소를 지불할 수 있기 때문에 소유자는 Ether를 받을 수 있습니다
(bool success, ) = owner.call{value: amount}("");
require(success, "Failed to send Ether");
}
// Function to transfer Ether from this contract to address from input
// 이 계약의 Ether를 입력의 주소로 전송하는 기능
function transfer(address payable _to, uint _amount) public {
// Note that "to" is declared as payable
(bool success, ) = _to.call{value: _amount}("");
require(success, "Failed to send Ether");
}
}