Quickstart -- Core SDK
ZeroDev has two main packages: @zerodev/waas
which is designed for React projects, and @zerodev/sdk
which is our core SDK that can be used in any JavaScript environment.
In this tutorial, we will walk you through using the core SDK. If you are building a React project, we recommend getting started with the React SDK.
Getting Started
Create a new project with npm
(or whatever package manager you use):
mkdir zerodev
cd zerodev
npm init -y
Install the ZeroDev SDK and presets:
npm i @zerodev/sdk @zerodev/ecdsa-validator
Install dev packages for TypeScript:
npm i --save-dev @types/node tslib
Create the following tsconfig.json
(TypeScript config):
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"declaration": true,
"outDir": "./lib",
"strict": true,
"esModuleInterop": true
},
"include": ["./**/*.ts"]
}
Create a script index.ts
with the following code:
import { createKernelAccount, createKernelAccountClient, createZeroDevPaymasterClient } from "@zerodev/sdk"
import { KERNEL_V3_1, getEntryPoint } from "@zerodev/sdk/constants"
import { signerToEcdsaValidator } from "@zerodev/ecdsa-validator"
import { http, createPublicClient, zeroAddress } from "viem"
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"
import { sepolia } from "viem/chains"
const PROJECT_ID = '98fd43a8-fb2f-4948-a7ae-069f53969f73'
const BUNDLER_RPC = `https://rpc.zerodev.app/api/v2/bundler/${PROJECT_ID}`
const PAYMASTER_RPC = `https://rpc.zerodev.app/api/v2/paymaster/${PROJECT_ID}`
const chain = sepolia
const entryPoint = getEntryPoint("0.7")
const kernelVersion = KERNEL_V3_1
const main = async () => {
// Construct a signer
const privateKey = generatePrivateKey()
const signer = privateKeyToAccount(privateKey)
// Construct a public client
const publicClient = createPublicClient({
// Use your own RPC provider (e.g. Infura/Alchemy).
transport: http(RPC_URL),
chain,
})
// Construct a validator
const ecdsaValidator = await signerToEcdsaValidator(publicClient, {
signer,
entryPoint,
kernelVersion
})
// Construct a Kernel account
const account = await createKernelAccount(publicClient, {
plugins: {
sudo: ecdsaValidator,
},
entryPoint,
kernelVersion
})
// Construct a Kernel account client
const kernelClient = createKernelAccountClient({
account,
chain,
bundlerTransport: http(BUNDLER_RPC),
paymaster: {
getPaymasterData: (userOperation) => {
const zerodevPaymaster = createZeroDevPaymasterClient({
chain,
transport: http(PAYMASTER_RPC),
})
return zerodevPaymaster.sponsorUserOperation({
userOperation,
})
},
},
})
const accountAddress = kernelClient.account.address
console.log("My account:", accountAddress)
// Send a UserOp
const userOpHash = await kernelClient.sendUserOperation({
callData: await kernelClient.account.encodeCalls([{
to: zeroAddress,
value: BigInt(0),
data: "0x",
}]),
})
console.log("UserOp hash:", userOpHash)
console.log("Waiting for UserOp to complete...")
await kernelClient.waitForUserOperationReceipt({
hash: userOpHash,
})
console.log("View completed UserOp here: https://jiffyscan.xyz/userOpHash/" + userOpHash)
}
main()
Run it:
npx ts-node index.ts
You should see an output like this:
My account: 0xaf731E22Fe96979C5D864B07bad0EB999cDBbE76
UserOp hash: 0x7a8e0ba961cc0a34f745b81d64766f033269fee831104fee0269fa5bcc397dcb
Waiting for UserOp to complete...
View completed UserOp here: https://jiffyscan.xyz/userOpHash/0x7a8e0ba961cc0a34f745b81d64766f033269fee831104fee0269fa5bcc397dcb
Congrats -- you just sent your first gasless transaction with ZeroDev!
In this example, you used a public ZeroDev API key. Now learn how to set up your own ZeroDev project.