Interacting with a Paymaster using Foundry-ZKsync

Learn how to deploy a smart contract using a Gasless Paymaster in Foundry-ZKsync.

This tutorial guides you through deploying a Counter contract using forge script, leveraging a Gasless Paymaster to cover gas fees.

What you'll learn:

  • How to utilize the Paymaster interfaces with Foundry-ZKsync
  • How to deploy a contract using a Gasless Paymaster
  • How to use paymaster cheatcodes in Foundry-ZKsync
GitHub

Tools:

  • - Foundry-ZKsync
foundry-zksyncaccount abstractionpaymastersmart contractscheatcodes
Last Updated: Feb 21, 2025

Prerequisites

  • This tutorial uses Solidity. You should have some experience with the language.
  • You must have Foundry-ZKsync installed.

What You Will Deploy

In this tutorial, you will deploy a Counter contract and leverage forge script along with ZKsync-specific paymaster cheatcodes to:

  • Deploy contracts using a paymaster to cover gas fees.
  • Use the zkUsePaymaster cheatcode to enable gasless transactions.
  • Understand how paymasters interact with account abstraction on ZKsync.

Why use paymasters?

The Problem: Gas Fees as a Barrier

Transactions requires gas fees, which can be a hurdle for users. Many dApps struggle with onboarding new users due to the complexity and cost of transaction fees. The solution, paymasters! Paymasters allow developers to subsidize transaction fees, making it possible to:

  • Enhance user experience by abstracting gas fees away.
  • Enable new business models where dApps cover transaction costs for users.
  • Improve accessibility by allowing interactions without requiring users to hold ETH.

Introduction to Foundry-ZKsync cheatcodes

What are Cheatcodes?

Cheatcodes in Foundry provide enhanced testing capabilities, allowing developers to manipulate blockchain state for testing purposes. ZKsync extends Foundry’s standard cheatcodes with ZKsync-specific features.

ZKsync-Specific Cheatcodes

Some of the most useful cheatcodes for ZKsync development include:

  • zkRegisterContract – Registers bytecode for EraVM.
  • zkVm – Enables or disables EraVM.
  • zkVmSkip – Skips execution on EraVM and runs it in the EVM instead.
  • zkUseFactoryDep – Marks a contract as a factory dependency.
  • zkUsePaymaster – Enables a paymaster for the next transaction.

For a complete list, refer to the Foundry-ZKsync Cheatcodes documentation.

How it will work

  1. Deploy a Gasless Paymaster Contract - We will deploy a simple Gasless Paymaster contract that will cover the gas fees.
  2. Deploy a Counter Contract – We will deploy a simple Counter contract that can increment a stored value.
  3. Use the zkUsePaymaster Cheatcode – We will set up our deployment script to utilize a paymaster, allowing the transaction to be gasless.
  4. Deploy Using forge script – We will execute the deployment process with Foundry-ZKsync, ensuring the paymaster covers the transaction fees.
  5. Interact with Counter contract with our Gasless Paymaster using cast send.

Made with ❤️ by the ZKsync Community