-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPayMe.sol
More file actions
61 lines (54 loc) · 2.61 KB
/
PayMe.sol
File metadata and controls
61 lines (54 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
import {RollupOrders} from "zenith/src/orders/RollupOrders.sol";
import {SignetL2} from "../Signet.sol";
/// @notice This contract provides a modifier that allows functions to be gated
/// by requiring a payment of a specified amount of native asset.
abstract contract PayMe is SignetL2 {
/// @notice This modifier crates an order with no input, that pays the
/// specified amount of native asset to the contract. It can be used
/// to gate access to payment-gate functions.
modifier payMe(uint256 amount) {
_;
demandPayment(NATIVE_ASSET, amount);
}
/// @notice A version of payMe that subsidizes the gas cost of the
/// transaction by deducting it from the payment amount.
modifier payMeSubsidizedGas(uint256 amount) {
uint256 pre = gasleft();
_;
_payMeSubsidizedGasAfter(pre, amount);
}
/// @notice This silences spurious foundry warnings.
function _payMeSubsidizedGasAfter(uint256 pre, uint256 amount) internal {
uint256 post = gasleft();
uint256 gp = tx.gasprice;
uint256 loot = amount - (gp * (pre - post));
demandPayment(NATIVE_ASSET, loot);
}
/// @notice Creates an order that demands payment of the specified amount
/// of the specified asset to the contract.
/// @dev This is useful for cases where the payment should go to the
/// contract itself, such as for fees or service charges.
/// @param asset The address of the asset to be paid.
/// @param amount The amount of the asset to be paid.
function demandPayment(address asset, uint256 amount) internal {
demandPaymentTo(asset, amount, address(this));
}
/// @notice Creates an order that demands payment of the specified amount
/// of the specified asset to a specific recipient.
/// @dev This is useful for cases where the payment should go to a different
/// address, such as a treasury or a specific user.
/// @param asset The address of the asset to be paid.
/// @param amount The amount of the asset to be paid.
/// @param recipient The address that will receive the payment.
function demandPaymentTo(address asset, uint256 amount, address recipient) internal {
RollupOrders.Output[] memory outputs = new RollupOrders.Output[](1);
outputs[0] = makeRollupOutput(asset, amount, recipient);
ORDERS.initiate(
block.timestamp, // this is equivalent to no deadline
new RollupOrders.Input[](0), // no inputs
outputs
);
}
}