Quickstart
This guide will walk you through setting up your project, creating a smart account directly from a local key (without the embedded wallet widget), and executing a transaction.
We will use the @0xgasless/smart-account package and viem.
Prerequisites
Section titled “Prerequisites”- Node.js installed
- A package manager (
npm,yarn,pnpm, orbun) - An API key from the 0xGasless Dashboard
1. Setup Your Project
Section titled “1. Setup Your Project”Create a new directory and initialize the project:
mkdir my-smart-account-appcd my-smart-account-appnpm init -yInstall the dependencies:
npm install viem @0xgasless/smart-accountnpm install --save-dev typescript @types/node ts-nodeCreate a tsconfig.json:
npx tsc --initUpdate your package.json to allow ES modules:
{ "name": "my-smart-account-app", "version": "1.0.0", "type": "module", "scripts": { "start": "ts-node src/index.ts" }}2. Initialize the Account
Section titled “2. Initialize the Account”Create a src directory and an index.ts file inside it. Add the following code to initialize the smart account signer using a local private key.
Note: Never commit private keys to GitHub. We use a hardcoded one here for demonstration purposes, but you should use environment variables (
process.env.PRIVATE_KEY) in production.
import { createSmartAccountClient } from "@0xgasless/smart-account";import { privateKeyToAccount } from "viem/accounts";import { parseAbi, encodeFunctionData } from "viem";
const API_KEY = "YOUR_API_KEY"; // Replace with your actual keyconst BUNDLER_URL = "https://bundler.0xgasless.com/11155111"; // Sepoliaconst PAYMASTER_URL = `https://paymaster.0xgasless.com/v1/11155111/rpc/${API_KEY}`;
// Generate a random local key for testingconst EOA = privateKeyToAccount("0x...");
async function main() { console.log("Setting up smart account...");
// Create the smart account client const smartAccount = await createSmartAccountClient({ signer: EOA, bundlerUrl: BUNDLER_URL, paymasterUrl: PAYMASTER_URL, chainId: 11155111, });
const address = await smartAccount.getAddress(); console.log("Smart Account deployed (or counterfactual) address:", address);3. Execute a Transaction
Section titled “3. Execute a Transaction”Let’s mint a free NFT on Sepolia to test our gasless setup.
console.log("Preparing transaction...");
const nftContractAddress = "0xYourTestNFTAddress"; // Replace with a verifiable testnet contract const abi = parseAbi(["function safeMint(address to)"]);
// Encode the contract call const callData = encodeFunctionData({ abi, functionName: "safeMint", args: [address], // Minting the NFT to the smart account itself });
console.log("Sending UserOperation to the Bundler...");
const userOpResponse = await smartAccount.sendTransaction({ to: nftContractAddress, data: callData, value: 0n, });
const hash = await userOpResponse.waitForTxHash(); console.log("UserOperation Hash:", hash);
const receipt = await userOpResponse.wait(); console.log("Transaction mined! Receipt:", receipt.receipt.transactionHash);}
main().catch(console.error);4. Run it
Section titled “4. Run it”Execute the script:
npm startIf successful, you will see the smart account address and the transaction hash logged to your console. Since we provided a paymasterUrl, the user pays zero gas — 0xGasless sponsors it through your Paymaster policy.
Next Steps
Section titled “Next Steps”- Learn how Bundlers process these UserOperations.
- Understand how Paymasters sponsor the transactions you just sent.