充值
- 函数+payable;
- msg.value大于0;
提现
- 地址+payable;
- address.transfer(uint256 amount);
示例代码如下:
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.7;
contract money_demo{
address public admin;
address payable public user;
uint256 totalAmount;
constructor(address _owner){
admin = _owner; //决定一个值成为管理员
}
function deposit(uint256 _amount)public payable{ //充值函数
if(_amount!=msg.value)return;
user=payable(msg.sender); //记录是谁充值的
totalAmount=_amount; //记录数值,表面记账
//address(this).balance+= _amount;
}
function getBalance()public view returns(uint256,uint256){ //查看合约账户余额
//this代表合约本身
return (address(this).balance,totalAmount);
}
function withdraw(uint256 _amount)public payable{ //提现函数
user.transfer(