@smartforge/gasless
v2.0.0
Published
Gasless transaction engine for sponsored transactions with usage limits.
Readme
@smartforge/gasless
Gasless transaction engine for sponsored transactions with usage limits.
Installation
npm install @smartforge/gasless
# or
pnpm add @smartforge/gaslessUsage
Setup
import { GaslessEngine } from "@smartforge/gasless";
const gaslessEngine = new GaslessEngine({
relayerPrivateKey: process.env.RELAYER_PRIVATE_KEY!,
monthlyLimit: BigInt("1000000000000000000"), // 1 ETH in wei
chainId: 8453, // Base
rpcUrl: "https://mainnet.base.org",
});Check Gas Budget
const month = gaslessEngine.getCurrentMonth(); // "2024-12"
const budget = await gaslessEngine.checkGasBudget(
projectId,
month,
async (projectId, month) => {
// Fetch usage from database
return await db.getGasUsage(projectId, month);
}
);
if (budget.allowed) {
console.log("Remaining gas:", budget.remaining.toString());
} else {
console.error("Budget exceeded:", budget.error);
}Check if Transaction Can Be Sponsored
const canSponsor = await gaslessEngine.canSponsor(
{
projectId: "project-id",
contractAddress: "0x...",
functionName: "mint",
args: [to, amount],
from: "0x...",
gasEstimate: 100000n,
},
async (projectId, month) => {
return await db.getGasUsage(projectId, month);
}
);
if (canSponsor.allowed) {
// Proceed with sponsored transaction
} else {
console.error(canSponsor.error);
}Submit Sponsored Transaction
const result = await gaslessEngine.submitSponsoredTransaction({
projectId: "project-id",
contractAddress: "0x...",
functionName: "mint",
args: [to, amount],
from: "0x...",
gasEstimate: 100000n,
});
if (result.success) {
console.log("Transaction hash:", result.txHash);
} else {
console.error("Error:", result.error);
}API
GaslessEngine
Main gasless transaction engine.
Methods:
checkGasBudget()- Check remaining gas budgetestimateGas()- Estimate gas for transactioncanSponsor()- Check if transaction can be sponsoredsubmitSponsoredTransaction()- Submit sponsored transactiongetCurrentMonth()- Get current month string (YYYY-MM)
Configuration
relayerPrivateKey- Private key of the relayer walletmonthlyLimit- Monthly gas limit in weichainId- Chain ID (default: 8453 for Base)rpcUrl- RPC endpoint URL
License
ISC
