98 lines
3.8 KiB
Solidity
98 lines
3.8 KiB
Solidity
// SPDX-License-Identifier: MIT
|
|
pragma solidity ^0.8.0;
|
|
|
|
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";
|
|
|
|
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";
|
|
|
|
import "@openzeppelin/contracts/access/Ownable.sol";
|
|
|
|
/// @author Matter Labs
|
|
/// @notice This smart contract pays the gas fees on behalf of users that are the owner of a specific NFT asset
|
|
contract ERC721gatedPaymaster is IPaymaster, Ownable {
|
|
IERC721 private immutable nft_asset;
|
|
|
|
modifier onlyBootloader() {
|
|
require(
|
|
msg.sender == BOOTLOADER_FORMAL_ADDRESS,
|
|
"Only bootloader can call this method"
|
|
);
|
|
// Continue execution if called from the bootloader.
|
|
_;
|
|
}
|
|
|
|
// The constructor takes the address of the ERC721 contract as an argument.
|
|
// The ERC721 contract is the asset that the user must hold in order to use the paymaster.
|
|
constructor(address _erc721) {
|
|
nft_asset = IERC721(_erc721); // Initialize the ERC721 contract
|
|
}
|
|
|
|
// The gas fees will be paid for by the paymaster if the user is the owner of the required NFT asset.
|
|
function validateAndPayForPaymasterTransaction(
|
|
bytes32,
|
|
bytes32,
|
|
Transaction calldata _transaction
|
|
)
|
|
external
|
|
payable
|
|
onlyBootloader
|
|
returns (bytes4 magic, bytes memory context)
|
|
{
|
|
// By default we consider the transaction as accepted.
|
|
magic = PAYMASTER_VALIDATION_SUCCESS_MAGIC;
|
|
require(
|
|
_transaction.paymasterInput.length >= 4,
|
|
"The standard paymaster input must be at least 4 bytes long"
|
|
);
|
|
|
|
bytes4 paymasterInputSelector = bytes4(
|
|
_transaction.paymasterInput[0:4]
|
|
);
|
|
|
|
// Use the general paymaster flow
|
|
if (paymasterInputSelector == IPaymasterFlow.general.selector) {
|
|
address userAddress = address(uint160(_transaction.from));
|
|
// Verify if user has the required NFT asset in order to use paymaster
|
|
require(
|
|
nft_asset.balanceOf(userAddress) > 0,
|
|
"User does not hold the required NFT asset and therefore must for their own gas!"
|
|
);
|
|
// 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
|
|
}("");
|
|
} else {
|
|
revert("Invalid paymaster flow");
|
|
}
|
|
}
|
|
|
|
function postTransaction(
|
|
bytes calldata _context,
|
|
Transaction calldata _transaction,
|
|
bytes32,
|
|
bytes32,
|
|
ExecutionResult _txResult,
|
|
uint256 _maxRefundedGas
|
|
) external payable override onlyBootloader {
|
|
// Refunds are not supported yet.
|
|
}
|
|
|
|
function withdraw(address payable _to) external onlyOwner {
|
|
// send paymaster funds to the owner
|
|
uint256 balance = address(this).balance;
|
|
(bool success, ) = _to.call{value: balance}("");
|
|
require(success, "Failed to withdraw funds from paymaster.");
|
|
}
|
|
|
|
receive() external payable {}
|
|
}
|