Created
April 24, 2023 17:02
-
-
Save uF4No/096d774d469144685236379da5f01b78 to your computer and use it in GitHub Desktop.
zkSync basic Paymaster implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.8; | |
import {IPaymaster, ExecutionResult, PAYMASTER_VALIDATION_SUCCESS_MAGIC} | |
from "@matterlabs/zksync-contracts/l2/system-contracts/interfaces/IPaymaster.sol"; | |
import {IPaymasterFlow} from "@matterlabs/zksync-contracts/l2/system-contracts/interfaces/IPaymasterFlow.sol"; | |
import {TransactionHelper, Transaction} from "@matterlabs/zksync-contracts/l2/system-contracts/libraries/TransactionHelper.sol"; | |
import "@matterlabs/zksync-contracts/l2/system-contracts/Constants.sol"; | |
contract MyPaymaster is IPaymaster { | |
function validateAndPayForPaymasterTransaction ( | |
bytes32, | |
bytes32, | |
Transaction calldata _transaction | |
) onlyBootloader external payable returns (bytes4 magic, bytes memory context) { | |
// By default we consider the transaction as accepted. | |
magic = PAYMASTER_VALIDATION_SUCCESS_MAGIC; | |
bytes4 paymasterInputSelector = bytes4( | |
_transaction.paymasterInput[0:4] | |
); | |
if (paymasterInputSelector == IPaymasterFlow.general.selector) { | |
// Note, that while the minimal amount of ETH needed is tx.gasPrice * tx.gasLimit, | |
// neither paymaster nor account are allowed to access this context variable. | |
uint256 requiredETH = _transaction.gasLimit * | |
_transaction.maxFeePerGas; | |
// The bootloader never returns any data, so it can safely be ignored here. | |
(bool success, ) = payable(BOOTLOADER_FORMAL_ADDRESS).call{value: requiredETH}(""); | |
require(success, "Failed to transfer funds to the bootloader"); | |
} else { | |
revert("Unsupported paymaster flow"); | |
} | |
} | |
function postTransaction ( | |
bytes calldata _context, | |
Transaction calldata _transaction, | |
bytes32, | |
bytes32, | |
ExecutionResult _txResult, | |
uint256 _maxRefundedGas | |
) onlyBootloader external payable override { | |
// Refunds are not supported yet. | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment