Solidity 充值,提现及转账操作(2023版)

该文章展示了一个用Solidity编写的智能合约,合约包括充值和提现功能。用户可以通过deposit函数充值,资金存储在合约地址中,而withdraw函数允许用户将资金提取到他们的地址。此外,合约还包含一个查看余额的getBalance函数。需要注意的是,在Solidity0.8.7版本之后,address类型不再默认为payable,需显式指定。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

充值

  • 函数+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(
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值