Solidity 056 SimpleFallBack

本文详细介绍了用Solidity语言编写的EtherBox智能合约,包括构造函数、事件日志、接收Ether、转账、余额查询及所有权变更等关键功能。
摘要由CSDN通过智能技术生成

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

contract EtherBox {
    address public owner;
    event LogMe(string message, address sender, uint amount);

    constructor() {
        owner = msg.sender; // Set the contract creator as the owner
    }

    // Fallback function to log incoming transactions with data
    fallback() external payable {
        emit LogMe("Fallback called", msg.sender, msg.value);
    }

    // Receive function to handle plain Ether transactions
    receive() external payable {
        emit LogMe("Receive called", msg.sender, msg.value);
    }

    // Function to withdraw Ether from the contract
    function withdraw(uint _amount) external {
        require(msg.sender == owner, "Only the owner can withdraw");
        payable(owner).transfer(_amount);
    }

    // Function to check the balance of Ether in the contract
    function getBalance() public view returns (uint) {
        return address(this).balance;
    }

    // Function to change the owner of the contract
    function changeOwner(address _newOwner) external {
        require(msg.sender == owner, "Only the owner can change ownership");
        owner = _newOwner;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值