I have a function that I am calling and based on the logic of that function and how much gas it spends to execute the function, I want to increment a user's balance to reimburse the user for the gas expended on the function call.
Here is an attempt that I am working through using a combination of gasleft() and tx.gasprice that is failing at the moment.
How can I come to a rough approximation to the amount of gas in Wei/Eth that a function expends during the life of its execution in a solidity contract?
function aFxn2RefundGas(address gamer) external {
uint256 begin = gasleft();
...
uint256 end = gasleft();
if (users.length > 100) {
// pay startMultiplier * gas fees to the person who clicked
balances[address(this)] = balances[address(this)] - (((begin - end) + 21000) * tx.gasprice) * startMultiplier;
balances[gamer] = balances[gamer] + (((begin - end) + 21000) * tx.gasprice) * startMultiplier;
} else {
//
// pay startMultiplier * gas fees to the person who clicked
balances[address(this)] = balances[address(this)] - (((begin - end) + 21000) * tx.gasprice);
balances[gamer] = balances[gamer] + (((begin - end) + 21000) * tx.gasprice);
}
}