Implementations
- Gelato Gasless SDK
- With 7702 Smart Account
- Relay API Endpoints
1
Initialize Relayer Client
Copy
Ask AI
import { createGelatoEvmRelayerClient } from '@gelatocloud/gasless';
import { createPublicClient, encodeFunctionData, formatUnits, http } from 'viem';
import { baseSepolia } from 'viem/chains';
const relayer = createGelatoEvmRelayerClient({
apiKey: process.env.GELATO_API_KEY,
testnet: true
});
const publicClient = createPublicClient({
chain: baseSepolia,
transport: http()
});
2
Estimate Gas
Copy
Ask AI
const data = encodeFunctionData({
abi: [{ type: 'function', name: 'increment', inputs: [], outputs: [] }],
functionName: 'increment'
});
const gasEstimate = await publicClient.estimateGas({
to: '0xE27C1359cf02B49acC6474311Bd79d1f10b1f8De',
data
});
console.log('Estimated gas:', gasEstimate.toString());
3
Get Fee Quote (ERC-20)
Get the fee quote in an ERC-20 token like USDC:
Copy
Ask AI
const USDC_ADDRESS = '0x036CbD53842c5426634e7929541eC2318f3dCF7e'; // Base Sepolia
const quote = await relayer.getFeeQuote({
chainId: baseSepolia.id,
gas: gasEstimate,
token: USDC_ADDRESS
});
console.log('Fee:', formatUnits(quote.fee, 6), 'USDC');
console.log('Expiry:', new Date(quote.expiry * 1000).toISOString());
4
Get Fee Data
Get current gas price and token rate:
Copy
Ask AI
const feeData = await relayer.getFeeData({
chainId: baseSepolia.id,
token: USDC_ADDRESS
});
console.log('Gas Price:', feeData.gasPrice.toString(), 'wei');
console.log('Rate (token/ETH):', feeData.rate);
1
Create Smart Account
Copy
Ask AI
import {
createGelatoSmartAccountClient,
toGelatoSmartAccount,
sponsored,
} from "@gelatocloud/gasless";
import { createPublicClient, http, type Hex, formatEther } from "viem";
import { privateKeyToAccount } from "viem/accounts";
import { baseSepolia } from "viem/chains";
const owner = privateKeyToAccount(process.env.PRIVATE_KEY as Hex);
const client = createPublicClient({
chain: baseSepolia,
transport: http(),
});
const account = toGelatoSmartAccount({
client,
owner,
});
const relayer = await createGelatoSmartAccountClient({
account,
apiKey: process.env.GELATO_API_KEY,
});
2
Get Fee Quote
Copy
Ask AI
const quote = await relayer.getFeeQuote({
payment: sponsored(),
calls: [
{
to: "0xE27C1359cf02B49acC6474311Bd79d1f10b1f8De",
data: "0xd09de08a",
},
],
});
console.log(`Estimated fee: ${formatEther(quote.estimatedFee)} ETH`);
3
Estimate for Multiple Transactions
Copy
Ask AI
const quote = await relayer.getFeeQuote({
payment: sponsored(),
calls: [
{
to: "0xE27C1359cf02B49acC6474311Bd79d1f10b1f8De",
data: "0xd09de08a",
},
{
to: "0xE27C1359cf02B49acC6474311Bd79d1f10b1f8De",
data: "0xd09de08a",
},
],
});
console.log(`Estimated fee: ${formatEther(quote.estimatedFee)} ETH`);
Use
https://api.gelato.cloud for mainnets, or https://api.t.gelato.cloud for testnets. Pass the API key in the X-API-Key header.1
Estimate Gas (Chain RPC)
Copy
Ask AI
const response = await fetch('https://sepolia.base.org', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'eth_estimateGas',
params: [{
to: '0xE27C1359cf02B49acC6474311Bd79d1f10b1f8De',
data: '0xd09de08a'
}]
})
});
const result = await response.json();
const gasEstimate = parseInt(result.result, 16);
2
Get Fee Quote
Copy
Ask AI
const USDC_ADDRESS = '0x036CbD53842c5426634e7929541eC2318f3dCF7e'; // Base Sepolia
const response = await fetch('https://api.t.gelato.cloud/rpc', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': process.env.GELATO_API_KEY
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'relayer_getFeeQuote',
params: {
chainId: '84532',
gas: gasEstimate.toString(),
token: USDC_ADDRESS
}
})
});
const data = await response.json();
console.log('Fee:', Number(data.result.fee) / 1e6, 'USDC');
console.log('Expiry:', new Date(data.result.expiry * 1000).toISOString());
3
Get Fee Data
Copy
Ask AI
const response = await fetch('https://api.t.gelato.cloud/rpc', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'X-API-Key': process.env.GELATO_API_KEY
},
body: JSON.stringify({
jsonrpc: '2.0',
id: 1,
method: 'relayer_getFeeData',
params: {
chainId: '84532',
token: USDC_ADDRESS
}
})
});
const data = await response.json();
console.log('Gas Price:', data.result.gasPrice, 'wei');
console.log('Rate (USDC/ETH):', data.result.rate);
Additional Resources
- Sponsor Gas with Gas Tank - Sponsored transactions
- Pay with ERC-20 Tokens - ERC-20 payments
- ERC-20 Payment Tokens - Supported tokens