I have this function:
function setAccount(BaseUBI _ubi, address _user, uint256 _startTime, uint _esiaID, bool _setToZero) external {
uint256 _refund = (gasleft() + 0/*FIXME*/) * tx.gasprice;
require(msg.sender == server, "System function"); // don't refund otherwise
require(_refund <= balances[_user], "Not enough balance");
balances[_user] -= _refund; // must be called before transfer() against reentrancy attack
server.transfer(_refund); // refund gas to the server
_ubi.setAccount{gas: balances[_user]}(_user, _startTime, _esiaID, _setToZero);
// We may be in a wrong state now, don't change any variables here.
}
Which number should be in place of TODO to ensure that the server will receive back at least as much gas as it spent?
Is there a library that automates this task?
If necessary, rewrite the appropriate part of the above code in assembler.
balances[_user] -= _refund;
should come beforeserver.transfer(_refund);
.