Created
December 16, 2023 03:06
-
-
Save duanescarlett/24fbbe7140532b9b4db4e23ebcaf02ea to your computer and use it in GitHub Desktop.
This is for the tutorial that teaches about the MSG object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract TransactionInfo { | |
address public owner; | |
uint256 public contractBalance; | |
constructor() { | |
// Set the owner to the account that deploys the contract | |
owner = msg.sender; | |
} | |
modifier onlyOwner() { | |
require(msg.sender == owner, "Only the owner can call this function"); | |
_; | |
} | |
function sendEther() external payable { | |
// Receive Ether and update contract balance | |
contractBalance += msg.value; | |
} | |
function getSender() external view returns (address) { | |
// Get the sender's address | |
return msg.sender; | |
} | |
function getValueSent() external view returns (uint256) { | |
// Get the amount of Ether sent with the transaction | |
return msg.value; | |
} | |
function getData() external view returns (bytes memory) { | |
// Get the data payload of the transaction | |
return msg.data; | |
} | |
function getGasRemaining() external view returns (uint256) { | |
// Get the remaining gas for the current execution | |
return gasleft(); | |
} | |
function getFunctionSignature() external pure returns (bytes4) { | |
// Get the function signature of the function being called | |
return msg.sig; | |
} | |
function withdraw() external onlyOwner { | |
// Only the owner can withdraw the contract balance | |
uint256 amount = contractBalance; | |
contractBalance = 0; | |
payable(owner).transfer(amount); | |
} | |
} | |
// Here's a brief explanation of the functions: | |
// - `sendEther`: Allows anyone to send Ether to the contract, updating the contract balance. | |
// - `getSender`: Returns the address of the sender of the transaction. | |
// - `getValueSent`: Returns the amount of Ether sent with the transaction. | |
// - `getData`: Returns the data payload of the transaction. | |
// - `getGasRemaining`: Returns the remaining gas for the current execution. | |
// - `getFunctionSignature`: Returns the function signature of the function being called. | |
// - `withdraw`: Only the owner can withdraw the contract balance. | |
// Please note that this is a simple example for educational purposes, and in a production scenario, additional security measures and considerations should be taken into account. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment