Repository navigation
Expand file tree
/
Copy pathPaymasterSystem.sol
More file actions
131 lines (115 loc) · 5.44 KB
/
Copy pathPaymasterSystem.sol
File metadata and controls
131 lines (115 loc) · 5.44 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
// SPDX-License-Identifier: MIT
pragma solidity >=0.8.24;
import { IPaymaster } from "@account-abstraction/contracts/interfaces/IPaymaster.sol";
import { PackedUserOperation } from "@account-abstraction/contracts/interfaces/PackedUserOperation.sol";
import { System } from "@latticexyz/world/src/System.sol";
import { Allowance } from "../codegen/tables/Allowance.sol";
import { Balance } from "../codegen/tables/Balance.sol";
import { Spender } from "../codegen/tables/Spender.sol";
import { SystemConfig } from "../codegen/tables/SystemConfig.sol";
import { recoverCallWithSignature } from "../utils/recoverCallWithSignature.sol";
import { AllowanceLib } from "./AllowanceSystem.sol";
uint256 constant FIXED_POST_OP_GAS = 80_000;
contract PaymasterSystem is System, IPaymaster {
error PaymasterSystem_InsufficientFunds(
address user,
uint256 maxCost,
uint256 availableAllowance,
uint256 availableBalance
);
error PaymasterSystem_OnlyEntryPoint();
/**
* Payment validation: check if paymaster agrees to pay.
* Must verify sender is the entryPoint.
* Revert to reject this request.
* Note that bundlers will reject this method if it changes the state, unless the paymaster is trusted (whitelisted).
* The paymaster pre-pays using its deposit, and receive back a refund after the postOp method returns.
* @param userOp - The user operation.
* @param userOpHash - Hash of the user's request data.
* @param maxCost - The maximum cost of this transaction (based on maximum gas and gas price from userOp).
* @return context - Value to send to a postOp. Zero length to signify postOp is not required.
* @return validationData - Signature and time-range of this operation, encoded the same as the return
* value of validateUserOperation.
* <20-byte> sigAuthorizer - 0 for valid signature, 1 to mark signature failure,
* other values are invalid for paymaster.
* <6-byte> validUntil - last timestamp this operation is valid. 0 for "indefinite"
* <6-byte> validAfter - first timestamp this operation is valid
* Note that the validation code cannot use block.timestamp (or block.number) directly.
*/
function validatePaymasterUserOp(
PackedUserOperation calldata userOp,
bytes32 userOpHash,
uint256 maxCost
) public override returns (bytes memory context, uint256 validationData) {
_requireFromEntryPoint();
address user = _getUser(userOp);
uint256 fromBalance = AllowanceLib.getMissingAllowance(user, maxCost);
uint256 fromAllowance = maxCost - fromBalance;
if (fromBalance > 0) {
uint256 availableBalance = Balance._get(user);
if (fromBalance > availableBalance) {
revert PaymasterSystem_InsufficientFunds(user, maxCost, fromAllowance, availableBalance);
}
}
context = abi.encode(user, fromAllowance);
}
/**
* Post-operation handler.
* Must verify sender is the entryPoint.
* @param mode - Enum with the following options:
* opSucceeded - User operation succeeded.
* opReverted - User op reverted. The paymaster still has to pay for gas.
* postOpReverted - never passed in a call to postOp().
* @param context - The context value returned by validatePaymasterUserOp
* @param actualGasCost - Actual cost of gas used so far (without this postOp call).
* @param actualUserOpFeePerGas - the gas price this UserOp pays. This value is based on the UserOp's maxFeePerGas
* and maxPriorityFee (and basefee)
* It is not the same as tx.gasprice, which is what the bundler pays.
*/
function postOp(
IPaymaster.PostOpMode mode,
bytes calldata context,
uint256 actualGasCost,
uint256 actualUserOpFeePerGas
) public override {
_requireFromEntryPoint();
(address user, uint256 fromAllowance) = abi.decode(context, (address, uint256));
uint256 totalGasCost = actualGasCost + FIXED_POST_OP_GAS * actualUserOpFeePerGas;
if (totalGasCost > fromAllowance) {
AllowanceLib.spendAllowance(user, fromAllowance);
uint256 fromBalance = totalGasCost - fromAllowance;
uint256 currentBalance = Balance._get(user);
if (fromBalance > currentBalance) {
revert PaymasterSystem_InsufficientFunds(user, totalGasCost, fromAllowance, currentBalance);
}
Balance._set(user, currentBalance - fromBalance);
} else {
AllowanceLib.spendAllowance(user, totalGasCost);
}
}
/**
* If this user op is sent from a spender account, translate it to the user account.
* Else return the userOp sender.
*/
function _getUser(PackedUserOperation calldata userOp) internal view returns (address) {
// Check if this is a spender account
address user = Spender.getUser(userOp.sender);
if (user != address(0)) {
return user;
}
// Check if this is a call to register a new spender via `callWithSignature`
user = recoverCallWithSignature(userOp);
if (user != address(0)) {
return user;
}
return userOp.sender;
}
/**
* Validate the call is made from a valid entrypoint
*/
function _requireFromEntryPoint() internal virtual {
if (_msgSender() != SystemConfig.getEntryPoint()) {
revert PaymasterSystem_OnlyEntryPoint();
}
}
}