Gas sponsorship mechanics
Verifying Paymasters, signing sponsorship payloads, and depositing gas budgets.
To sponsor a user's transaction, a Paymaster contract must intervene during the ERC-4337 execution flow. The EntryPoint contract checks that the Paymaster has deposited sufficient native tokens (e.g. MATIC) on-chain to cover the transaction cost, and then executes the transaction payload.
Let's look at the validation handshake that makes this secure.
1. The Verifying Paymaster Pattern
How does the Paymaster contract know it should pay for a specific user's action? If it sponsors everything blindly, malicious actors could drain its funds.
To solve this, we use the Verifying Paymaster pattern:
- The client SDK generates the
UserOperationpayload (excluding gas paymaster signatures). - The client sends the UserOp to the developer's backend server (or directly to a verified API gateway like Pimlico).
- The backend validates the request (e.g. checks if the user is logged in, has not exceeded their daily rate limit, and is calling an approved function).
- If approved, the backend signs the hash of the UserOp using a private key controlled by the Paymaster owner:
types.tstypescript
- The signature is appended to the
paymasterAndDatafield of theUserOperation. - When the EntryPoint contract validates the transaction on-chain, it extracts the signature from
paymasterAndDataand validates it against the registered Paymaster signer:SmartAccount.solsolidity
// The Verifying Paymaster verification flow: Client sends UserOp to Backend -> Backend checks limits and signs hash -> EntryPoint validates signature on-chain.
When I first set up a Paymaster on Polygon, I forgot to deposit funds into the EntryPoint contract for my Paymaster address. Even though my backend was generating valid signatures, all client transactions reverted with the error AA31: paymaster deposit too low. Remember: a Paymaster must maintain an active deposit of native tokens on the EntryPoint contract to fund executions.
2. Sponsoring Gas in Socio3 V2
In Socio3 V2, the frontend uses Viem's smartAccountClient to route actions. The client configuration specifies the paymaster endpoint:
This client automatically queries Pimlico's verifying paymaster API, populates the paymasterAndData field with the sponsorship signature, and submits it to the bundler.
Was this lesson helpful?
Let us know what you think of this specification. (submitting anonymously)
