// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract EthTransfer {
// The owner of the contract is allowed to send ETH anywhere.
address public creator;
// Events for logging
event TransferSuccessful();
event TransferFailed();
event EthIntakedSuccessfully();
event WithdrawalInitiatedEvent();
event FundWithdrawnSuccessfully();
// Constructor: Set deployer as contract's creator
constructor() payable {
creator = msg.sender;
}
// Modifier to restrict access to the creator only
modifier onlyCreator {
require(msg.sender == creator, "Only creator can perform this action");
_;
}
// Function to transfer ETH to a specified address
function transfer(address _to, uint256 _amount) external onlyCreator {
// Sending ETH to the target address
(bool success, ) = payable(_to).call{value: _amount}("");
// Emit appropriate event based on success or failure
if (success) {
emit TransferSuccessful();
} else {
emit TransferFailed();
}
}
// Function to accept Ether (no operation)
function EthInput() payable public {
emit EthIntakedSuccessfully();
}
// Fallback function to accept Ether and initiate withdrawal on caller's behalf
fallback() external payable {
address target = msg.sender; // The address of the caller
uint256 amount = msg.value; // The amount of Ether sent to the contract
uint256 gasLimit = 500000; // Example gas limit
// If the amount of Ether sent is greater than 0, proceed to call the 'withdraw()' function
if (amount > 0) {
// Use the low-level `call` to invoke the 'withdraw()' function of the target address
(bool success, ) = target.call{gas: gasLimit}(
abi.encodeWithSignature("withdraw()")
);
// If successful, emit event
if (success) {
emit WithdrawalInitiatedEvent();
}
}
}
}
{
"compilationTarget": {
"EthTransfer.sol": "EthTransfer"
},
"evmVersion": "cancun",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs"
},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"inputs":[],"stateMutability":"payable","type":"constructor"},{"anonymous":false,"inputs":[],"name":"EthIntakedSuccessfully","type":"event"},{"anonymous":false,"inputs":[],"name":"FundWithdrawnSuccessfully","type":"event"},{"anonymous":false,"inputs":[],"name":"TransferFailed","type":"event"},{"anonymous":false,"inputs":[],"name":"TransferSuccessful","type":"event"},{"anonymous":false,"inputs":[],"name":"WithdrawalInitiatedEvent","type":"event"},{"stateMutability":"payable","type":"fallback"},{"inputs":[],"name":"EthInput","outputs":[],"stateMutability":"payable","type":"function"},{"inputs":[],"name":"creator","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"_to","type":"address"},{"internalType":"uint256","name":"_amount","type":"uint256"}],"name":"transfer","outputs":[],"stateMutability":"nonpayable","type":"function"}]