Go Gasless
Last updated
Last updated
With Cometh Connect, you can pay the transaction gas fees of your users. You will need to add the contract address of your transaction as a sponsored address for your project. Remember, the contract address corresponds to the "to" field of your transaction.
To authorize the sponsorship of a given contract address, you need to add it to your sponsored addresses in the dashboard. From there, we will accept sponsoring transactions targeting this contract.
More details about the Cometh paymaster API :
You'll first need to instantiate a paymaster Client, you'll need to get a paymasterUrl from the cometh dashboard.
import { createComethPaymasterClient } from "@cometh/connect-sdk-4337";
const paymasterClient = await createComethPaymasterClient({
transport: http(paymasterUrl),
chain,
publicClient,
})
You'll then need to add a paymaster methods on your client creation: getUserOperationGasPrice.
const smartAccountClient = createSmartAccountClient({
account: smartAccount,
chain,
bundlerTransport: http(bundlerUrl),
paymaster: paymasterClient,
userOperation: {
estimateFeesPerGas: async () => {
return await paymasterClient.getUserOperationGasPrice();
},
},
})
Here is the full overview:
import { createSafeSmartAccount,
createSmartAccountClient,
createComethPaymasterClient } from "@cometh/connect-sdk-4337";
import { encodeFunctionData } from "viem";
import countContractAbi from "@/contract/counterABI.json";
const apiKey = process.env.COMETH_API_KEY;
const bundlerUrl = process.env.4337_BUNDLER_URL;
const paymasterUrl = process.env.4337_PAYMASTER_URL
const publicClient = createPublicClient({
chain: arbitrumSepolia,
transport: http(),
cacheTime: 60_000,
batch: {
multicall: { wait: 50 },
},
});
const smartAccount = await createSafeSmartAccount({
apiKey,
publicClient,
chain: arbitrumSepolia,
});
const paymasterClient = await createComethPaymasterClient({
transport: http(paymasterUrl),
chain,
publicClient,
})
const smartAccountClient = createSmartAccountClient({
account: smartAccount,
chain,
paymaster: paymasterClient,
userOperation: {
estimateFeesPerGas: async () => {
return await paymasterClient.getUserOperationGasPrice();
},
}
})
// Counter address that is sponsored
const COUNTER_CONTRACT_ADDRESS = "0x4FbF9EE4B2AF774D4617eAb027ac2901a41a7b5F";
const calldata = encodeFunctionData({
abi: countContractAbi,
functionName: "count",
});
const txHash = await smartAccountClient.sendTransaction(
{
to: COUNTER_CONTRACT_ADDRESS,
data: calldata,
}
);