Skip to content

Commit

Permalink
add paymaster constants to BasePaymaster
Browse files Browse the repository at this point in the history
  • Loading branch information
drortirosh committed Jan 4, 2024
1 parent 277c379 commit effa64f
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 14 deletions.
6 changes: 5 additions & 1 deletion contracts/core/BasePaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import "../interfaces/IPaymaster.sol";
import "../interfaces/IEntryPoint.sol";
import "./Helpers.sol";

import "./UserOperationLib.sol";
/**
* Helper class for creating a paymaster.
* provides helper methods for staking.
Expand All @@ -17,6 +17,10 @@ import "./Helpers.sol";
abstract contract BasePaymaster is IPaymaster, Ownable {
IEntryPoint public immutable entryPoint;

uint256 internal constant PAYMASTER_VALIDATION_GAS_OFFSET = UserOperationLib.PAYMASTER_VALIDATION_GAS_OFFSET;
uint256 internal constant PAYMASTER_POSTOP_GAS_OFFSET = UserOperationLib.PAYMASTER_POSTOP_GAS_OFFSET;
uint256 internal constant PAYMASTER_DATA_OFFSET = UserOperationLib.PAYMASTER_DATA_OFFSET;

constructor(IEntryPoint _entryPoint) Ownable(msg.sender) {
_validateEntryPointInterface(_entryPoint);
entryPoint = _entryPoint;
Expand Down
2 changes: 1 addition & 1 deletion contracts/samples/LegacyTokenPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ contract LegacyTokenPaymaster is BasePaymaster, ERC20 {

// verificationGasLimit is dual-purposed, as gas limit for postOp. make sure it is high enough
// make sure that verificationGasLimit is high enough to handle postOp
require(uint128(bytes16(userOp.paymasterAndData[UserOperationLib.PAYMASTER_POSTOP_GAS_OFFSET:UserOperationLib.PAYMASTER_DATA_OFFSET])) > COST_OF_POST, "TokenPaymaster: gas too low for postOp");
require(uint128(bytes16(userOp.paymasterAndData[PAYMASTER_POSTOP_GAS_OFFSET : PAYMASTER_DATA_OFFSET])) > COST_OF_POST, "TokenPaymaster: gas too low for postOp");

if (userOp.initCode.length != 0) {
_validateConstructor(userOp);
Expand Down
4 changes: 2 additions & 2 deletions contracts/samples/TokenPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ contract TokenPaymaster is BasePaymaster, UniswapHelper, OracleHelper {
override
returns (bytes memory context, uint256 validationResult) {unchecked {
uint256 priceMarkup = tokenPaymasterConfig.priceMarkup;
uint256 paymasterAndDataLength = userOp.paymasterAndData.length - UserOperationLib.PAYMASTER_DATA_OFFSET;
uint256 paymasterAndDataLength = userOp.paymasterAndData.length - PAYMASTER_DATA_OFFSET;
require(paymasterAndDataLength == 0 || paymasterAndDataLength == 32,
"TPM: invalid data length"
);
uint256 preChargeNative = requiredPreFund + (tokenPaymasterConfig.refundPostopCost * userOp.maxFeePerGas);
// note: as price is in ether-per-token and we want more tokens increasing it means dividing it by markup
uint256 cachedPriceWithMarkup = cachedPrice * PRICE_DENOMINATOR / priceMarkup;
if (paymasterAndDataLength == 32) {
uint256 clientSuppliedPrice = uint256(bytes32(userOp.paymasterAndData[UserOperationLib.PAYMASTER_DATA_OFFSET : UserOperationLib.PAYMASTER_DATA_OFFSET + 32]));
uint256 clientSuppliedPrice = uint256(bytes32(userOp.paymasterAndData[PAYMASTER_DATA_OFFSET : PAYMASTER_DATA_OFFSET + 32]));
if (clientSuppliedPrice < cachedPriceWithMarkup) {
// note: smaller number means 'more ether per token'
cachedPriceWithMarkup = clientSuppliedPrice;
Expand Down
4 changes: 2 additions & 2 deletions contracts/samples/VerifyingPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ contract VerifyingPaymaster is BasePaymaster {

address public immutable verifyingSigner;

uint256 private constant VALID_TIMESTAMP_OFFSET = UserOperationLib.PAYMASTER_DATA_OFFSET;
uint256 private constant VALID_TIMESTAMP_OFFSET = PAYMASTER_DATA_OFFSET;

uint256 private constant SIGNATURE_OFFSET = VALID_TIMESTAMP_OFFSET + 64;

Expand All @@ -50,7 +50,7 @@ contract VerifyingPaymaster is BasePaymaster {
keccak256(userOp.initCode),
keccak256(userOp.callData),
userOp.accountGasLimits,
uint256(bytes32(userOp.paymasterAndData[20 : 52])),
uint256(bytes32(userOp.paymasterAndData[PAYMASTER_VALIDATION_GAS_OFFSET : PAYMASTER_DATA_OFFSET])),
userOp.preVerificationGas,
userOp.maxFeePerGas,
userOp.maxPriorityFeePerGas,
Expand Down
2 changes: 1 addition & 1 deletion contracts/test/TestExpirePaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ contract TestExpirePaymaster is BasePaymaster {
internal virtual override view
returns (bytes memory context, uint256 validationData) {
(userOp, userOpHash, maxCost);
(uint48 validAfter, uint48 validUntil) = abi.decode(userOp.paymasterAndData[UserOperationLib.PAYMASTER_DATA_OFFSET :], (uint48, uint48));
(uint48 validAfter, uint48 validUntil) = abi.decode(userOp.paymasterAndData[PAYMASTER_DATA_OFFSET :], (uint48, uint48));
validationData = _packValidationData(false, validUntil, validAfter);
context = "";
}
Expand Down
14 changes: 7 additions & 7 deletions reports/gas-checker.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
║ simple paymaster │ 1 │ 89079 │ │ ║
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
║ simple paymaster with diff │ 2 │ │ 4396314984
║ simple paymaster with diff │ 2 │ │ 4398715008
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
║ simple paymaster │ 10 │ 485071 │ │ ║
║ simple paymaster │ 10 │ 485083 │ │ ║
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
║ simple paymaster with diff │ 11 │ │ 4401715038
║ simple paymaster with diff │ 11 │ │ 4400515026
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
║ big tx 5k │ 1 │ 183735 │ │ ║
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
Expand All @@ -36,11 +36,11 @@
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
║ big tx - diff from previous │ 11 │ │ 145440 │ 20216 ║
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
║ paymaster+postOp │ 1 │ 90907 │ │ ║
║ paymaster+postOp │ 1 │ 90895 │ │ ║
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
║ paymaster+postOp with diff │ 2 │ │ 4581416835
║ paymaster+postOp with diff │ 2 │ │ 4585016871
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
║ paymaster+postOp │ 10 │ 503383 │ │ ║
║ paymaster+postOp │ 10 │ 503503 │ │ ║
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
║ paymaster+postOp with diff │ 11 │ │ 45851 │ 16872 ║
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
Expand All @@ -50,6 +50,6 @@
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
║ token paymaster │ 10 │ 816150 │ │ ║
╟────────────────────────────────┼───────┼───────────────┼────────────────┼─────────────────────╢
║ token paymaster with diff │ 11 │ │ 7409445115
║ token paymaster with diff │ 11 │ │ 7410645127
╚════════════════════════════════╧═══════╧═══════════════╧════════════════╧═════════════════════╝

0 comments on commit effa64f

Please sign in to comment.