Description:
Smart contract deployed on Ethereum.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
contract ImmediateAutoRefunder {
address public owner;
uint256 public gasEstimate = 21000; // Standard ETH transfer gas
uint256 public gasBufferWei = 300000000; // 0.3 gwei in wei
event Deposit(address indexed from, uint256 amount, uint256 refunded, uint256 gasCost);
event GasSettingsUpdated(uint256 gasEstimate, uint256 gasBuffer);
constructor() {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner, "Not the owner");
_;
}
// Immediate auto refund function
receive() external payable {
// Calculate gas cost for refund transaction
uint256 adjustedGasPrice = tx.gasprice + gasBufferWei; // Add 0.3 gwei buffer
uint256 estimatedGasCost = gasEstimate * adjustedGasPrice;
require(msg.value > estimatedGasCost, "Amount too small to cover gas");
// Calculate refund amount
uint256 refundAmount = msg.value - estimatedGasCost;
// Send immediate refund
payable(msg.sender).transfer(refundAmount);
emit Deposit(msg.sender, msg.value, refundAmount, estimatedGasCost);
}
// Update gas settings (only owner)
function updateGasSettings(uint256 _gasEstimate, uint256 _gasBufferWei) external onlyOwner {
gasEstimate = _gasEstimate;
gasBufferWei = _gasBufferWei;
emit GasSettingsUpdated(_gasEstimate, _gasBufferWei);
}
// Get current gas calculation
function getGasCalculation() external view returns (
uint256 currentGasPrice,
uint256 adjustedGasPrice,
uint256 estimatedCost
) {
currentGasPrice = tx.gasprice;
adjustedGasPrice = tx.gasprice + gasBufferWei;
estimatedCost = gasEstimate * adjustedGasPrice;
}
// Preview what refund would be for a given amount
function previewRefund(uint256 amount) external view returns (uint256 refundAmount, uint256 gasCost) {
uint256 adjustedGasPrice = tx.gasprice + gasBufferWei;
gasCost = gasEstimate * adjustedGasPrice;
if (amount > gasCost) {
refundAmount = amount - gasCost;
} else {
refundAmount = 0;
}
}
// Manual execute for owner
function execute(address payable recipient, uint256 amount) external onlyOwner {
require(address(this).balance >= amount, "Insufficient balance");
recipient.transfer(amount);
}
// Get contract balance
function getBalance() external view returns (uint256) {
return address(this).balance;
}
// Emergency withdraw
function withdrawAll() external onlyOwner {
payable(owner).transfer(address(this).balance);
}
}
Submitted on: 2025-10-26 20:18:36
Comments
Log in to comment.
No comments yet.