How to use the claimed gas grant | Pimlico Docs
Skip to content

How to use the claimed gas grant

Usage of Sponsorship Policies with permisionless.js

If you are using permissionless.js, you can use the PimlicoPaymasterClient to use sponsorship policies.

Create the clients

First we must create the public, (optionally) paymaster clients that will be used to interact with the SimpleAccount.

import { createPublicClient, http } from "viem";
 
const publicClient = createPublicClient({
	transport: http("https://sepolia.rpc.thirdweb.com"),
});
 
const pimlicoClient = createPimlicoClient({
	chain: sepolia,
	transport: http(
		"https://api.pimlico.io/v2/sepolia/rpc?apikey=YOUR_PIMLICO_API_KEY",
	),
	entryPoint: {
		address: entryPoint07Address,
		version: "0.7",
	},
});

Create an account

Now, create an account. This can any of the accounts supported by permissionless.js or custom accounts conforming to the interface. For this example, we'll use a Simple account.

import { privateKeyToAccount } from "viem/accounts";
import { toSimpleSmartAccount } from "permissionless/accounts";
 
export const simpleSmartAccount = await toSimpleSmartAccount({
	client: publicClient,
	owner: privateKeyToAccount("0xPRIVATE_KEY"),
	entryPoint: {
		address: entryPoint07Address,
		version: "0.7",
	},
});

Create the smart account client with middleware

When creating the smartAccountClient, we can pass in a middleware.sponsorUserOperation function that will be called before a user operation is signed and sent. This is where we can pass in the sponsorshipPolicyId that we want to use.

import { sepolia } from "viem/chains";
import { createSmartAccountClient } from "permissionless";
import { createPimlicoClient } from "permissionless/clients/pimlico";
import { entryPoint07Address } from "viem/account-abstraction";
 
export const smartAccountClient = createSmartAccountClient({
	account: simpleSmartAccount,
	chain: sepolia,
	bundlerTransport: http(
		"https://api.pimlico.io/v2/sepolia/rpc?apikey=YOUR_PIMLICO_API_KEY",
	),
	paymaster: pimlicoClient,
	paymasterContext: {
		sponsorshipPolicyId: "your_claimed_sponsorship_policy_id", 
	},
	userOperation: {
		estimateFeesPerGas: async () => {
			return (await pimlicoClient.getUserOperationGasPrice()).fast; // if using pimlico bundlers
		},
	},
});

Send a sponsored transaction

Finally, let's submit a transaction from the smart account. We will send a transaction to the 0xd8da6bf26964af9d7eed9e03e53415d37aa96045 (vitalik.eth) address with 0x1234 as example callData.

const txHash = await smartAccountClient.sendTransaction({
	to: "0xd8da6bf26964af9d7eed9e03e53415d37aa96045",
	value: 0n,
	data: "0x1234",
})
 
console.log(`User operation included: https://sepolia.etherscan.io/tx/${txHash}`)

Congratulations, you have sent a transaction using a gas grant! 🎉