Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
lukaisailovic committed Jul 16, 2024
1 parent cbd8060 commit 5b86081
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<FillUserOpResponse> {
const privateKey = generatePrivateKey()
const signer = privateKeyToAccount(privateKey)
const chain = getChainById(params.chainId)

const publicClient = createPublicClient({
transport: http(publicClientUrl({ chain }))
})

async fillUserOp(params: FillUserOpParams): Promise<FillUserOpResponse> {

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<SendUserOpWithSigantureResponse> {
throw new Error("Method not implemented.");
}
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<SendUserOpWithSigantureResponse> {
throw new Error('Method not implemented.')
}
}
Original file line number Diff line number Diff line change
@@ -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<FillUserOpResponse>
sendUserOpWithSignature(params: SendUserOpWithSigantureParams): Promise<SendUserOpWithSigantureResponse>
}
fillUserOp(params: FillUserOpParams): Promise<FillUserOpResponse>
sendUserOpWithSignature(
params: SendUserOpWithSigantureParams
): Promise<SendUserOpWithSigantureResponse>
}
21 changes: 10 additions & 11 deletions advanced/wallets/react-wallet-v2/src/pages/api/build.ts
Original file line number Diff line number Diff line change
@@ -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<FillUserOpResponse>
) {
const builder = new SafeUserOpBuilder()
const response = await builder.fillUserOp(req.body)
res.status(200).json(response)
}
req: NextApiRequest,
res: NextApiResponse<FillUserOpResponse>
) {
const builder = new SafeUserOpBuilder()
const response = await builder.fillUserOp(req.body)
res.status(200).json(response)
}
19 changes: 9 additions & 10 deletions advanced/wallets/react-wallet-v2/src/utils/ChainUtil.ts
Original file line number Diff line number Diff line change
@@ -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`);
}
}

throw new Error(`Chain with id ${chainId} not found`)
}
1 change: 0 additions & 1 deletion advanced/wallets/react-wallet-v2/src/utils/HelperUtil.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,4 +212,3 @@ export function bigIntReplacer(_key: string, value: any) {

return value
}

Original file line number Diff line number Diff line change
Expand Up @@ -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<Chain['name'], Hex> = {
Sepolia: '0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789',
Expand Down Expand Up @@ -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 })
}

Expand All @@ -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}`
}
}

0 comments on commit 5b86081

Please sign in to comment.