config.ts import { sepolia } from " viem/chains " ;
export const chain = sepolia ;
export const ALCHEMY_API_KEY = " <YOUR_API_KEY> " ;
export const PRIVATE_KEY = " <YOUR_PRIVATE_KEY> " ; estimateAndSendUserOperation.ts import { createBundlerClient } from " viem/account-abstraction " ;
import { createClient , type Hex } from " viem " ;
import { privateKeyToAccount } from " viem/accounts " ;
import { alchemyTransport } from " @alchemy/common " ;
import { toModularAccountV2 } from " @alchemy/smart-accounts " ;
import { estimateFeesPerGas } from " @alchemy/aa-infra " ;
import { chain , ALCHEMY_API_KEY , PRIVATE_KEY } from " ./config " ;
const transport = alchemyTransport ( { apiKey : ALCHEMY_API_KEY } ) ;
export async function estimateAndSendUserOperation () {
const rpcClient = createClient ( { chain , transport } ) ;
const account = await toModularAccountV2 ( {
client : rpcClient ,
owner : privateKeyToAccount ( PRIVATE_KEY as Hex ) ,
} ) ;
const bundlerClient = createBundlerClient ( {
account ,
client : rpcClient ,
chain ,
transport ,
userOperation : { estimateFeesPerGas } ,
} ) ;
try {
const hash = await bundlerClient . sendUserOperation ( {
calls : [
{ to : " 0x0000000000000000000000000000000000000000 " , value : 0 n , data : " 0x " } ,
] ,
} ) ;
const receipt = await bundlerClient . waitForUserOperationReceipt ( { hash } ) ;
console . log ( " UserOperation receipt: " , receipt ) ;
} catch ( error ) {
console . error ( " Error processing UserOperation: " , error ) ;
}
} getUserOperationByHash.ts import { createBundlerClient } from " viem/account-abstraction " ;
import { createClient , type Hash } from " viem " ;
import { alchemyTransport } from " @alchemy/common " ;
import { chain , ALCHEMY_API_KEY } from " ./config " ;
const transport = alchemyTransport ( { apiKey : ALCHEMY_API_KEY } ) ;
export async function getUserOperationByHash ( uoHash : Hash ) {
const rpcClient = createClient ( { chain , transport } ) ;
const bundlerClient = createBundlerClient ( {
client : rpcClient ,
chain ,
transport ,
} ) ;
try {
const userOp = await bundlerClient . getUserOperation ( { hash : uoHash } ) ;
console . log ( " User Operation: " , userOp ) ;
} catch ( error ) {
console . error ( " Error retrieving UserOperation: " , error ) ;
}
}