Skip to content

Commit

Permalink
change tut
Browse files Browse the repository at this point in the history
  • Loading branch information
plusminushalf committed Aug 27, 2024
1 parent 4a75d6b commit 6c59bb5
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 54 deletions.
17 changes: 6 additions & 11 deletions tutorial-1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import { createBundlerClient, createPaymasterClient, entryPoint07Address } from

const apiKey = process.env.PIMLICO_API_KEY
if (!apiKey) throw new Error("Missing PIMLICO_API_KEY")
const paymasterUrl = `https://api.pimlico.io/v2/sepolia/rpc?apikey=${apiKey}`

const privateKey =
(process.env.PRIVATE_KEY as Hex) ??
Expand All @@ -24,10 +23,6 @@ export const publicClient = createPublicClient({
transport: http("https://rpc.ankr.com/eth_sepolia"),
})

export const paymasterClient = createPaymasterClient({
transport: http(paymasterUrl),
})

const account = await toSafeSmartAccount({
client: publicClient,
owner: privateKeyToAccount(privateKey),
Expand All @@ -44,10 +39,10 @@ console.log({

console.log(`Smart account address: https://sepolia.etherscan.io/address/${account.address}`)

const bundlerUrl = `https://api.pimlico.io/v2/sepolia/rpc?apikey=${apiKey}`
const pimlicoUrl = `https://api.pimlico.io/v2/sepolia/rpc?apikey=${apiKey}`

const bundlerClient = createPimlicoClient({
transport: http(bundlerUrl),
const pimlicoClient = createPimlicoClient({
transport: http(pimlicoUrl),
entryPoint: {
address: entryPoint07Address,
version: "0.7",
Expand All @@ -57,11 +52,11 @@ const bundlerClient = createPimlicoClient({
const smartAccountClient = createBundlerClient({
account,
chain: sepolia,
transport: http(bundlerUrl),
paymaster: paymasterClient,
transport: http(pimlicoUrl),
paymaster: pimlicoClient,
userOperation: {
estimateFeesPerGas: async () => {
return (await bundlerClient.getUserOperationGasPrice()).fast
return (await pimlicoClient.getUserOperationGasPrice()).fast
},
}
})
Expand Down
75 changes: 32 additions & 43 deletions tutorial-3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ import { writeFileSync } from "fs"
import { createSmartAccountClient } from "permissionless"
import { toSafeSmartAccount } from "permissionless/accounts"
import { createPimlicoClient } from "permissionless/clients/pimlico"
import { Hex, createClient, createPublicClient, encodeFunctionData, http, parseAbiItem } from "viem"
import { GetPaymasterDataParameters } from "viem/account-abstraction/actions/paymaster/getPaymasterData"
import { Hex, createPublicClient, encodeFunctionData, http, parseAbiItem } from "viem"
import { entryPoint07Address } from "viem/account-abstraction"
import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"
import { sepolia } from "viem/chains"
import { GetPaymasterStubDataParameters } from "viem/account-abstraction/actions/paymaster/getPaymasterStubData"

const erc20PaymasterAddress = "0x000000000041F3aFe8892B48D88b6862efe0ec8d" as const
const usdcAddress = "0x1c7D4B196Cb0C7B01d743Fbc6116a902379C7238"
Expand All @@ -27,10 +25,10 @@ const publicClient = createPublicClient({
})

const apiKey = process.env.PIMLICO_API_KEY // REPLACE THIS
const bundlerUrl = `https://api.pimlico.io/v2/sepolia/rpc?apikey=${apiKey}`
const pimlicoUrl = `https://api.pimlico.io/v2/sepolia/rpc?apikey=${apiKey}`

const bundlerClient = createPimlicoClient({
transport: http(bundlerUrl),
const pimlicoClient = createPimlicoClient({
transport: http(pimlicoUrl),
entryPoint: {
address: entryPoint07Address,
version: "0.7",
Expand Down Expand Up @@ -75,49 +73,40 @@ if (senderUsdcBalance < 1_000_000n) {

console.log(`Smart account USDC balance: ${Number(senderUsdcBalance) / 1000000} USDC`)

const erc20Paymaster = createClient({
transport: http(bundlerUrl),
}).extend((_) => ({
getPaymasterData: async (
parameters: GetPaymasterDataParameters,
) => {
const gasEstimates = await bundlerClient.estimateUserOperationGas({
...parameters,
paymaster: erc20PaymasterAddress,
})
return {
paymaster: erc20PaymasterAddress,
paymasterData: "0x" as Hex,
paymasterPostOpGasLimit: gasEstimates.paymasterPostOpGasLimit ?? 0n,
paymasterVerificationGasLimit: gasEstimates.paymasterVerificationGasLimit ?? 0n,
}
},
getPaymasterStubData: async (
parameters: GetPaymasterStubDataParameters,
) => {
const gasEstimates = await bundlerClient.estimateUserOperationGas({
...parameters,
paymaster: erc20PaymasterAddress
})

return {
paymaster: erc20PaymasterAddress,
paymasterData: "0x" as Hex,
paymasterPostOpGasLimit: gasEstimates.paymasterPostOpGasLimit ?? 0n,
paymasterVerificationGasLimit: gasEstimates.paymasterVerificationGasLimit ?? 0n
}
}
}))

const smartAccountClient = createSmartAccountClient({
client: publicClient,
account,
chain: sepolia,
transport: http(bundlerUrl),
paymaster: erc20Paymaster,
transport: http(pimlicoUrl),
paymaster: {
async getPaymasterData(parameters) {
const gasEstimates = await pimlicoClient.estimateUserOperationGas({
...parameters,
paymaster: erc20PaymasterAddress,
})
return {
paymaster: erc20PaymasterAddress,
paymasterData: "0x" as Hex,
paymasterPostOpGasLimit: gasEstimates.paymasterPostOpGasLimit ?? 0n,
paymasterVerificationGasLimit: gasEstimates.paymasterVerificationGasLimit ?? 0n,
}
},
async getPaymasterStubData(parameters) {
const gasEstimates = await pimlicoClient.estimateUserOperationGas({
...parameters,
paymaster: erc20PaymasterAddress
})
return {
paymaster: erc20PaymasterAddress,
paymasterData: "0x" as Hex,
paymasterPostOpGasLimit: gasEstimates.paymasterPostOpGasLimit ?? 0n,
paymasterVerificationGasLimit: gasEstimates.paymasterVerificationGasLimit ?? 0n
}
}
},
userOperation: {
estimateFeesPerGas: async () => {
return (await bundlerClient.getUserOperationGasPrice()).fast
return (await pimlicoClient.getUserOperationGasPrice()).fast
},
}
})
Expand Down

0 comments on commit 6c59bb5

Please sign in to comment.