solidity ->payable
1.一个函数(或地址)如果想接收ether,需要将其修饰为payable 2.address常用方法 2.1 balance() ->查询当前地址的ether余额 2.2 transfer(uint)->合约向当前地址转指定数量的ether,如果失败会回滚 2.3 send(uint) ->合约向当前地址转指定数量的ether,如果失败会返回false,不回滚(不建议使用send)
// SPDX-License-Identifier: SEE LICENSE IN LICENSE
pragma solidity ^0.8.9;
contract Payable {
address payable public owner;
constructor() payable {
owner = payable(msg.sender);
}
function deposit() public payable {}
function notPayable() public {}
function withdraw() public {
uint amount = address(this).balance;
owner.transfer(amount);
}
function transfer(address payable _to, uint _amount) public {
_to.transfer(_amount);
}
}
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!