From 5b860818f162bbde12bbfda2b3f992d70695ad70 Mon Sep 17 00:00:00 2001 From: Luka Isailovic Date: Tue, 16 Jul 2024 16:38:26 +0200 Subject: [PATCH] fix: lint --- .../src/lib/smart-accounts/SmartAccountLib.ts | 5 +- .../builders/SafeUserOpBuilder.ts | 150 ++++++++++-------- .../smart-accounts/builders/UserOpBuilder.ts | 41 ++--- .../react-wallet-v2/src/pages/api/build.ts | 21 ++- .../react-wallet-v2/src/utils/ChainUtil.ts | 19 ++- .../react-wallet-v2/src/utils/HelperUtil.ts | 1 - .../src/utils/SmartAccountUtil.ts | 6 +- 7 files changed, 123 insertions(+), 120 deletions(-) diff --git a/advanced/wallets/react-wallet-v2/src/lib/smart-accounts/SmartAccountLib.ts b/advanced/wallets/react-wallet-v2/src/lib/smart-accounts/SmartAccountLib.ts index 854b6132d..890a3eb1a 100644 --- a/advanced/wallets/react-wallet-v2/src/lib/smart-accounts/SmartAccountLib.ts +++ b/advanced/wallets/react-wallet-v2/src/lib/smart-accounts/SmartAccountLib.ts @@ -23,7 +23,7 @@ import { SmartAccountClient, SmartAccountClientConfig, bundlerActions, - createSmartAccountClient, + createSmartAccountClient } from 'permissionless' import { PimlicoBundlerActions, pimlicoBundlerActions } from 'permissionless/actions/pimlico' import { bundlerUrl, paymasterUrl, publicClientUrl } from '@/utils/SmartAccountUtil' @@ -75,9 +75,6 @@ export abstract class SmartAccountLib implements EIP155Wallet { sponsored = false, entryPointVersion = 6 }: SmartAccountLibOptions) { - - - let entryPoint: EntryPoint = ENTRYPOINT_ADDRESS_V06 if (entryPointVersion === 7) { entryPoint = ENTRYPOINT_ADDRESS_V07 diff --git a/advanced/wallets/react-wallet-v2/src/lib/smart-accounts/builders/SafeUserOpBuilder.ts b/advanced/wallets/react-wallet-v2/src/lib/smart-accounts/builders/SafeUserOpBuilder.ts index 18abe4ee4..bac5fc43e 100644 --- a/advanced/wallets/react-wallet-v2/src/lib/smart-accounts/builders/SafeUserOpBuilder.ts +++ b/advanced/wallets/react-wallet-v2/src/lib/smart-accounts/builders/SafeUserOpBuilder.ts @@ -1,80 +1,90 @@ -import { generatePrivateKey, privateKeyToAccount } from "viem/accounts"; -import { FillUserOpParams, FillUserOpResponse, SendUserOpWithSigantureParams, SendUserOpWithSigantureResponse, UserOpBuilder } from "./UserOpBuilder"; -import { createPublicClient, http } from "viem"; -import { signerToSafeSmartAccount } from "permissionless/accounts"; -import { createSmartAccountClient, ENTRYPOINT_ADDRESS_V07, getUserOperationHash } from "permissionless"; -import { createPimlicoBundlerClient, createPimlicoPaymasterClient } from "permissionless/clients/pimlico"; -import { bundlerUrl, paymasterUrl, publicClientUrl } from "@/utils/SmartAccountUtil"; - -import { getChainById } from "@/utils/ChainUtil"; +import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts' +import { + FillUserOpParams, + FillUserOpResponse, + SendUserOpWithSigantureParams, + SendUserOpWithSigantureResponse, + UserOpBuilder +} from './UserOpBuilder' +import { createPublicClient, http } from 'viem' +import { signerToSafeSmartAccount } from 'permissionless/accounts' +import { + createSmartAccountClient, + ENTRYPOINT_ADDRESS_V07, + getUserOperationHash +} from 'permissionless' +import { + createPimlicoBundlerClient, + createPimlicoPaymasterClient +} from 'permissionless/clients/pimlico' +import { bundlerUrl, paymasterUrl, publicClientUrl } from '@/utils/SmartAccountUtil' +import { getChainById } from '@/utils/ChainUtil' const PIMLICO_API_KEY = process.env['NEXT_PUBLIC_PIMLICO_KEY'] export class SafeUserOpBuilder implements UserOpBuilder { + async fillUserOp(params: FillUserOpParams): Promise { + const privateKey = generatePrivateKey() + const signer = privateKeyToAccount(privateKey) + const chain = getChainById(params.chainId) + const publicClient = createPublicClient({ + transport: http(publicClientUrl({ chain })) + }) - async fillUserOp(params: FillUserOpParams): Promise { - - const privateKey = generatePrivateKey() - const signer = privateKeyToAccount(privateKey) - const chain = getChainById(params.chainId) - - const publicClient = createPublicClient({ - transport: http(publicClientUrl({ chain })) - }) + const paymasterClient = createPimlicoPaymasterClient({ + transport: http(paymasterUrl({ chain }), { + timeout: 30000 + }), + entryPoint: ENTRYPOINT_ADDRESS_V07 + }) - const paymasterClient = createPimlicoPaymasterClient({ - transport: http(paymasterUrl({ chain }), { - timeout: 30000 - }), - entryPoint: ENTRYPOINT_ADDRESS_V07, - }) + const bundlerTransport = http(bundlerUrl({ chain }), { + timeout: 30000 + }) + const pimlicoBundlerClient = createPimlicoBundlerClient({ + transport: bundlerTransport, + entryPoint: ENTRYPOINT_ADDRESS_V07 + }) - const bundlerTransport = http(bundlerUrl({ chain }), { - timeout: 30000 - }) - const pimlicoBundlerClient = createPimlicoBundlerClient({ - transport: bundlerTransport, - entryPoint: ENTRYPOINT_ADDRESS_V07, - }) - - const safeAccount = await signerToSafeSmartAccount(publicClient, { - entryPoint: ENTRYPOINT_ADDRESS_V07, - signer: signer, - safeVersion: "1.4.1", - address: params.account - }) - - const smartAccountClient = createSmartAccountClient({ - account: safeAccount, - entryPoint: ENTRYPOINT_ADDRESS_V07, - chain, - bundlerTransport, - middleware: { - sponsorUserOperation: paymasterClient.sponsorUserOperation, // optional - gasPrice: async () => (await pimlicoBundlerClient.getUserOperationGasPrice()).fast, // if using pimlico bundler - }, - }) - const account = smartAccountClient.account - const userOp = await smartAccountClient.prepareUserOperationRequest({ - userOperation: { - callData: await account.encodeCallData(params.calls) - }, - account: account - }) - const hash = getUserOperationHash({ - userOperation: userOp, - chainId: chain.id, - entryPoint: ENTRYPOINT_ADDRESS_V07 - }) - return { - userOp, - hash - } - } - sendUserOpWithSignature(params: SendUserOpWithSigantureParams): Promise { - throw new Error("Method not implemented."); - } + const safeAccount = await signerToSafeSmartAccount(publicClient, { + entryPoint: ENTRYPOINT_ADDRESS_V07, + signer: signer, + safeVersion: '1.4.1', + address: params.account + }) -} \ No newline at end of file + const smartAccountClient = createSmartAccountClient({ + account: safeAccount, + entryPoint: ENTRYPOINT_ADDRESS_V07, + chain, + bundlerTransport, + middleware: { + sponsorUserOperation: paymasterClient.sponsorUserOperation, // optional + gasPrice: async () => (await pimlicoBundlerClient.getUserOperationGasPrice()).fast // if using pimlico bundler + } + }) + const account = smartAccountClient.account + const userOp = await smartAccountClient.prepareUserOperationRequest({ + userOperation: { + callData: await account.encodeCallData(params.calls) + }, + account: account + }) + const hash = getUserOperationHash({ + userOperation: userOp, + chainId: chain.id, + entryPoint: ENTRYPOINT_ADDRESS_V07 + }) + return { + userOp, + hash + } + } + sendUserOpWithSignature( + params: SendUserOpWithSigantureParams + ): Promise { + throw new Error('Method not implemented.') + } +} diff --git a/advanced/wallets/react-wallet-v2/src/lib/smart-accounts/builders/UserOpBuilder.ts b/advanced/wallets/react-wallet-v2/src/lib/smart-accounts/builders/UserOpBuilder.ts index c849d929a..2d064c3a5 100644 --- a/advanced/wallets/react-wallet-v2/src/lib/smart-accounts/builders/UserOpBuilder.ts +++ b/advanced/wallets/react-wallet-v2/src/lib/smart-accounts/builders/UserOpBuilder.ts @@ -1,35 +1,36 @@ -import { UserOperation } from "permissionless"; -import { Address, Hex } from "viem"; +import { UserOperation } from 'permissionless' +import { Address, Hex } from 'viem' type Call = { to: Address; value: bigint; data: Hex } -type UserOp = UserOperation<"v0.7"> +type UserOp = UserOperation<'v0.7'> export type FillUserOpParams = { - chainId: number; - account: Address; - calls: Call[]; - capabilities: { - paymasterService?: { url: string }; - permissions?: { context: Hex }; - }; + chainId: number + account: Address + calls: Call[] + capabilities: { + paymasterService?: { url: string } + permissions?: { context: Hex } + } } export type FillUserOpResponse = { userOp: UserOp hash: Hex -} +} export type SendUserOpWithSigantureParams = { - chainId: Hex; - userOp: UserOp; - signature: Hex; - permissionsContext?: Hex; + chainId: Hex + userOp: UserOp + signature: Hex + permissionsContext?: Hex } export type SendUserOpWithSigantureResponse = { - receipt: Hex + receipt: Hex } - export interface UserOpBuilder { - fillUserOp(params: FillUserOpParams): Promise - sendUserOpWithSignature(params: SendUserOpWithSigantureParams): Promise -} \ No newline at end of file + fillUserOp(params: FillUserOpParams): Promise + sendUserOpWithSignature( + params: SendUserOpWithSigantureParams + ): Promise +} diff --git a/advanced/wallets/react-wallet-v2/src/pages/api/build.ts b/advanced/wallets/react-wallet-v2/src/pages/api/build.ts index 1cf72e3a8..9b0af1311 100644 --- a/advanced/wallets/react-wallet-v2/src/pages/api/build.ts +++ b/advanced/wallets/react-wallet-v2/src/pages/api/build.ts @@ -1,13 +1,12 @@ -import { SafeUserOpBuilder } from "@/lib/smart-accounts/builders/SafeUserOpBuilder"; -import { FillUserOpResponse } from "@/lib/smart-accounts/builders/UserOpBuilder"; -import { NextApiRequest, NextApiResponse } from "next"; +import { SafeUserOpBuilder } from '@/lib/smart-accounts/builders/SafeUserOpBuilder' +import { FillUserOpResponse } from '@/lib/smart-accounts/builders/UserOpBuilder' +import { NextApiRequest, NextApiResponse } from 'next' - export default async function handler( - req: NextApiRequest, - res: NextApiResponse - ) { - const builder = new SafeUserOpBuilder() - const response = await builder.fillUserOp(req.body) - res.status(200).json(response) -} \ No newline at end of file + req: NextApiRequest, + res: NextApiResponse +) { + const builder = new SafeUserOpBuilder() + const response = await builder.fillUserOp(req.body) + res.status(200).json(response) +} diff --git a/advanced/wallets/react-wallet-v2/src/utils/ChainUtil.ts b/advanced/wallets/react-wallet-v2/src/utils/ChainUtil.ts index ec5d8815d..7934acc6c 100644 --- a/advanced/wallets/react-wallet-v2/src/utils/ChainUtil.ts +++ b/advanced/wallets/react-wallet-v2/src/utils/ChainUtil.ts @@ -1,13 +1,12 @@ -import * as chains from "viem/chains"; -import { Chain } from "viem/chains"; - +import * as chains from 'viem/chains' +import { Chain } from 'viem/chains' export function getChainById(chainId: number): Chain { - for (const chain of Object.values(chains)) { - if (chain.id === chainId) { - return chain; - } + for (const chain of Object.values(chains)) { + if (chain.id === chainId) { + return chain } - - throw new Error(`Chain with id ${chainId} not found`); - } \ No newline at end of file + } + + throw new Error(`Chain with id ${chainId} not found`) +} diff --git a/advanced/wallets/react-wallet-v2/src/utils/HelperUtil.ts b/advanced/wallets/react-wallet-v2/src/utils/HelperUtil.ts index e2a55ae16..1894b2e0a 100644 --- a/advanced/wallets/react-wallet-v2/src/utils/HelperUtil.ts +++ b/advanced/wallets/react-wallet-v2/src/utils/HelperUtil.ts @@ -212,4 +212,3 @@ export function bigIntReplacer(_key: string, value: any) { return value } - diff --git a/advanced/wallets/react-wallet-v2/src/utils/SmartAccountUtil.ts b/advanced/wallets/react-wallet-v2/src/utils/SmartAccountUtil.ts index 908533517..a5a6aa14c 100644 --- a/advanced/wallets/react-wallet-v2/src/utils/SmartAccountUtil.ts +++ b/advanced/wallets/react-wallet-v2/src/utils/SmartAccountUtil.ts @@ -7,8 +7,6 @@ import { sepolia } from 'viem/chains' import { SafeSmartAccountLib } from '@/lib/smart-accounts/SafeSmartAccountLib' import { SmartAccountLib } from '@/lib/smart-accounts/SmartAccountLib' - - // Entrypoints [I think this is constant but JIC] export const ENTRYPOINT_ADDRESSES: Record = { Sepolia: '0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789', @@ -147,7 +145,7 @@ export type UrlConfig = { chain: Chain | ViemChain } -export const publicClientUrl = ({ chain }: UrlConfig) => { +export const publicClientUrl = ({ chain }: UrlConfig) => { return process.env.NEXT_PUBLIC_LOCAL_CLIENT_URL || publicRPCUrl({ chain }) } @@ -174,4 +172,4 @@ export const bundlerUrl = ({ chain }: UrlConfig) => { return localBundlerUrl } return `https://api.pimlico.io/v1/${PIMLICO_NETWORK_NAMES[chain.name]}/rpc?apikey=${apiKey}` -} \ No newline at end of file +}