Paymaster
Welcome to the final part of our ZKsync 101 series on mastering ZKsync development! In this guide, we move beyond the basics of smart contract deployment and the creation of contract factories to explore the innovative concept of paymasters in the ZKsync ecosystem. This guide will illuminate the power of paymasters to revolutionize transaction fee management and enhance user experiences within your dApps.
Delve deeper into ZKsync development with the introduction of paymasters.
Learn how paymasters can cover transaction fees for your dApp users, enhancing accessibility and user experience.
Discover the flexibility of fee payment with paymasters, including the ability to pay fees in ERC20 tokens on ZKsync Era.
What is a Paymaster?
Paymasters in the ZKsync ecosystem represent a groundbreaking approach to handling transaction fees. They are special accounts designed to subsidize transaction costs for other accounts, potentially making certain transactions free for end-users. This feature is particularly useful for dApp developers looking to improve their platform's accessibility and user experience by covering transaction fees on behalf of their users.
Built-in Paymaster Flows
Paymasters can operate under various flows, some of which may require user interaction, such as setting allowances for token swaps. These flows enable paymasters to support a wide range of use cases, from simple fee subsidies to more complex scenarios involving ERC20 token exchanges for transaction fees.
- General Paymaster Flow: This default flow requires no preliminary actions from users, allowing paymasters to interpret transaction data as needed to cover fees.
- Approval-Based Paymaster Flow: For operations requiring user permissions, such as token allowances, this flow provides a structured approach. It ensures that user tokens can be seamlessly exchanged for transaction fees, subject to user-approved limits.
As we explore paymasters, remember that while they offer enhanced flexibility for fee management, their implementation should always prioritize security and user trust.
- Users are solely responsible for ensuring that their design, implementation, and use of the paymaster smart contract software complies with all applicable laws, including but not limited to money transmission, anti-money laundering (AML), and payment processing regulations.
- The developers and publishers of this software disclaim any liability for any legal issues that may arise from its use.
Paymaster flow
Select the paymaster type you want to use.
Understanding the GaslessPaymaster
contract
Let's start by reviewing the
contracts/4-paymaster/gasless/GaslessPaymaster.sol
contract:
Key components:
- The
GaslessPaymaster
contract ensures that transaction fees are handled automatically without user intervention. validateAndPayForPaymasterTransaction
Method: This mandatory method assesses whether the paymaster agrees to cover the transaction fees. If affirmative, it transfers the necessary funds (calculated as tx.gasprice * tx.gasLimit) to the operator. It returns a context for thepostTransaction
method.postTransaction
Method: An optional method invoked post-transaction execution, provided the transaction doesn't fail due to out-of-gas errors. It receives several parameters, including the transaction's context and result, aiding in finalizing paymaster duties.onlyBootloader
Modifier: Ensures that certain methods are exclusively callable by the system's bootloader, adding an extra layer of security and control.
Compile and deploy the GaslessPaymaster
contract
Run the npm script compile
to compile the contracts:
npm run compile
The script to deploy the GaslessPaymaster
is located at `/deploy/4-paymaster/gasless/deploy.ts.
Key Components:
deployContract
Method: Utilized for deploying theGaslessPaymaster
contract. This method takes the name of the contract and any constructor arguments needed for initialization, mirroring the deployment process used for theCrowdfundingCampaign
contract.- Funding the Paymaster: An important step where the deployed
GaslessPaymaster
contract is funded with ETH to cover transaction fees for users. The script sends a transaction from the deployer's wallet to the paymaster contract, ensuring it has sufficient balance to operate.
Execute the deploy npm script command to upgrade:
npm run deploy:gasless-paymaster
Upon successful deployment, you'll receive output detailing the deployment process, including the contract address, source, and encoded constructor arguments:
GaslessPaymaster contract deployed at <0xYOUR_PAYMASTER_ADDRESS>
Paymaster ETH balance is now 0.005
Interact with the GaslessPaymaster contract
We will interact with the GaslessPaymaster
by creating a CrowdfundCampaign
and have the paymaster pay for the transaction fees for your contribution.
The interaction script is situated in the /deploy/4-paymaster/gasless
directory,
named interact.ts
.
Ensure the YOUR_PAYMASTER_ADDRESS
variable is set to your deployed paymaster address.
Key Components:
- Paymaster Parameters: Before executing transactions that involve the contract, the script prepares paymaster parameters using
getPaymasterParams
. This specifies the paymaster contract to be used and the type of paymaster flow, which in this case isGeneral
. - Transaction with Paymaster: Demonstrated by the
contribute
function call, the script shows how to include paymaster parameters in transactions. This allows the paymaster to cover transaction fees, providing a seamless experience for users.
Run the npm script that interacts with the gasless paymaster:
npm run interact:gasless-paymaster
Upon successful usage, you'll receive output detailing the transaction:
Deploying a CrowdfundingCampaign contract...
Contributing 0.01 to the crowdfund contract...
Transaction hash: 0x1281592537c81f4d4ca259022b649dc582b186911af8f6b3612568383ea99b1b
Contribution successful!
🎉 Great job! You've successfully interacted with the CrowdfundingCampaign
using a paymaster to cover the transaction fees.
Takeaways
- Comprehensive Understanding of Paymaster Contracts: This guide has provided a detailed look at both the
ApprovalFlowPaymaster
and theGaslessPaymaster
contracts, illustrating how they manage transaction fees in ZKsync. These paymasters are pivotal in handling gas payments, offering a more accessible transaction experience for users. - Flexibility and User Empowerment: By covering the transaction fees through ERC20 tokens or general subsidies, these paymaster contracts offer significant flexibility and reduce the friction typically associated with on-chain interactions. This feature enhances user engagement and adoption of dApps.
Next Steps
- Learn more about Paymasters: Read further on our section on Paymasters to deepen your understanding.
- Experiment with Different Paymaster Contracts: Now that you are familiar with both approval-based and general paymaster flows, you can experiment with these contracts by deploying them under various conditions and with different types validations, restrictions and enhancements.
- Develop a Front-End Interface: Consider building a user interface with ZKsync CLI that interacts with the paymaster contracts you have deployed. This will not only improve the usability of your contracts but also provide practical insights into how end-users interact with your dApps in real-world scenarios.
- Community Engagement and Contribution: Join the vibrant ZKsync developer community. Participate in forums, Discord, or GitHub discussions. Sharing insights, asking queries, and contributing can enrich the ecosystem and your understanding of ZKsync.