Send Tax amout to Wallet Address: Gas issue

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);
}

}

What msg.value are you passing?

It must be large enough to ensure that msg.value - taxAmount - gasCost >= 0.

1 Like

Why are you considering the gas cost too?

All error message to gas issues, I cant even get the simplest code to work which is becoming super frustrating.

The following both fail

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

contract SimpleFee
{
address payable feeCollector = payable(0xDFCAb22C163EC9f1412B91f2f18a2575733F6967);

function deposit() external payable
{
require(msg.value > 0, "Amount must be greater than 0");
feeCollector.transfer(msg.value);
}
}

Modified code to consider Gas

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

contract SimpleFee
{
address payable feeCollector = payable(0xDFCAb22C163EC9f1412B91f2f18a2575733F6967);

function deposit() external payable
{
require(msg.value > 0, "Amount must be greater than 0");
feeCollector.transfer(msg.value - (tx.gasprice * gasleft()));
}
}

Transfer and send are limited to 2300 gas.
Use call instead

(bool sent, bytes memory data) = feeCollector.call{gas :10000, value: msg.value}(“”)

Also the gas is taken from the sending tx.gas, why would you remove the gas for the transfer from the msg.value?

Hi implemented the suggested change an it stills falls over, any ideas?

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

contract SimpleFee {
address payable feeCollector = payable(0xDFCAb22C163EC9f1412B91f2f18a2575733F6967);

function deposit() external payable {
require(msg.value > 0, "Amount must be greater than 0");
(bool sent, bytes memory data) = feeCollector.call{gas: 10000, value: msg.value}("");
}
}

feeCollector = 0xDFCAb22C163EC9f1412B91f2f18a2575733F6967 is an externally-owned account, not a smart-contract account.

So feeCollector.transfer should work, and you don't need to use feeCollector.call (let alone specify a gas limit of 10000).

For further assistance, please verify your contract on Etherscan.

Thanks for you help, what would the complete code be then? Because I've used that previous with no joy.

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

contract SimpleFee
{
address payable feeCollector = payable(0xDFCAb22C163EC9f1412B91f2f18a2575733F6967);

function deposit() external payable
{
require(msg.value > 0, "Amount must be greater than 0");
feeCollector.transfer(msg.value);
}
}

pragma solidity ^0.8.0;

contract SimpleFee
{
address payable feeCollector = payable(0xDFCAb22C163EC9f1412B91f2f18a2575733F6967);

function deposit() external payable
{
require(msg.value > 0, "Amount must be greater than 0");
feeCollector.transfer(msg.value);
}
}

In order for others to be able to investigate why your transaction reverts.

You can start here.