Dynamic Fees
Objective
Solution
abstract contract DynamicGasCharging {
uint256 private gasScalingFactor;
/**
* @dev Consume the given amount of gas.
* In contract code this function can be flexibly used at any place to add extra gas consumption
*/
function _consumeGas(uint256 gas) view internal {
uint256 initialGas = gasleft();
uint256 wantedGas = initialGas - gas;
while (gasleft() > wantedGas) {
// do nothing
}
}
// Modifier to increase the gas costs of a function by a configurable multiplier
modifier scaleGasCosts() {
uint256 initialGas = gasleft();
_;
uint256 gasUsed = initialGas - gasleft();
_consumeGas(gasUsed * (gasScalingFactor-1));
}
// Set the scaling factor (to be called by implementations)
function _setGasScalingFactor(uint256 newGasScalingFactor) internal {
gasScalingFactor = newGasScalingFactor;
}
}Last updated

