Skip to content

Instantly share code, notes, and snippets.

@z0r0z
Last active July 9, 2022 17:26
Show Gist options
  • Save z0r0z/bd21de838999f09ab15641f69c88620d to your computer and use it in GitHub Desktop.
Save z0r0z/bd21de838999f09ab15641f69c88620d to your computer and use it in GitHub Desktop.
Subsidized transactions for smart contracts.
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.4;
import {SafeTransferLib} from "https://github.com/Rari-Capital/solmate/blob/main/src/utils/SafeTransferLib.sol";
/// @notice Subsidized transactions for smart contracts.
/// @author z0r0z.eth
abstract contract Subsidized {
using SafeTransferLib for address;
modifier subsidized() virtual {
uint256 startGas = gasleft();
_;
unchecked {
tx.origin.safeTransferETH(
(
startGas - gasleft()
)
* tx.gasprice
);
}
}
}
contract SubsidizedTester is Subsidized {
uint256 public counter;
receive() external payable virtual {}
function testCounter() public subsidized virtual {
unchecked {
++counter;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment