Gasless transaction engine for sponsored transactions with usage limits.
npm install @smartforge/gaslessGasless transaction engine for sponsored transactions with usage limits.
``bash`
npm install @smartforge/gaslessor
pnpm add @smartforge/gasless
`typescript
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",
});
`
`typescript
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);
}
`
`typescript
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);
}
`
`typescript
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);
}
`
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)
- relayerPrivateKey - Private key of the relayer walletmonthlyLimit
- - Monthly gas limit in weichainId
- - Chain ID (default: 8453 for Base)rpcUrl` - RPC endpoint URL
-
ISC