Hi Guys. I'm trying to create a smart contract that receives and amount of ETH, calculates the tax and sends it to a hard coded wallet and returns the balance to user. Im constantly running into issues in respect to gas. I try to calculate this upfront. Any help or assistance would be really appreciated,
/ SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract TaxedWallet {
address payable public taxCollector = payable(0x2795377b509E5CAb8F768851F7448ecf2A758c74);
event PaymentReceived(address indexed sender, uint256 value, uint256 taxAmount, uint256 gasCost);
function sendPayment() external payable {
require(msg.value > 0, "Amount must be greater than 0");
uint256 taxPercentage = 2; // Set your desired tax percentage
uint256 taxAmount = (msg.value * taxPercentage) / 100;
uint256 gasCost = tx.gasprice * gasleft(); // Calculate the gas cost
uint256 amountToTransfer = msg.value - taxAmount - gasCost;
taxCollector.transfer(taxAmount);
payable(msg.sender).transfer(amountToTransfer);
emit PaymentReceived(msg.sender, msg.value, taxAmount, gasCost);
}