Skip to content

fix:smart accounts init due to rpc error #853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 58 additions & 14 deletions advanced/wallets/react-wallet-v2/src/hooks/useSmartAccounts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { EIP155Chain } from '@/data/EIP155Data'
import SettingsStore from '@/store/SettingsStore'
import SettingsStore, {
SAFE_SMART_ACCOUNTS_ENABLED_KEY,
ZERO_DEV_SMART_ACCOUNTS_ENABLED_KEY,
BICONOMY_SMART_ACCOUNTS_ENABLED_KEY
} from '@/store/SettingsStore'
import {
createOrRestoreBiconomySmartAccount,
createOrRestoreKernelSmartAccount,
createOrRestoreSafeSmartAccount,
smartAccountWallets
} from '@/utils/SmartAccountUtil'
import { styledToast } from '@/utils/HelperUtil'

import { useSnapshot } from 'valtio'

Expand All @@ -19,19 +24,58 @@ export default function useSmartAccounts() {

const initializeSmartAccounts = async (privateKey: string) => {
if (smartAccountEnabled) {
if (kernelSmartAccountEnabled) {
const { kernelSmartAccountAddress } = await createOrRestoreKernelSmartAccount(privateKey)
SettingsStore.setKernelSmartAccountAddress(kernelSmartAccountAddress)
}
if (safeSmartAccountEnabled) {
const { safeSmartAccountAddress } = await createOrRestoreSafeSmartAccount(privateKey)
SettingsStore.setSafeSmartAccountAddress(safeSmartAccountAddress)
}
if (biconomySmartAccountEnabled) {
const { biconomySmartAccountAddress } = await createOrRestoreBiconomySmartAccount(
privateKey
)
SettingsStore.setBiconomySmartAccountAddress(biconomySmartAccountAddress)
try {
const promises = []

if (kernelSmartAccountEnabled) {
promises.push(
(async () => {
try {
const address = await createOrRestoreKernelSmartAccount(privateKey)
SettingsStore.setKernelSmartAccountAddress(address)
} catch (error) {
SettingsStore.state.kernelSmartAccountEnabled = false
localStorage.removeItem(ZERO_DEV_SMART_ACCOUNTS_ENABLED_KEY)
styledToast('Kernel smart account initialization failed', 'warning')
}
})()
)
}

if (safeSmartAccountEnabled) {
promises.push(
(async () => {
try {
const address = await createOrRestoreSafeSmartAccount(privateKey)
SettingsStore.setSafeSmartAccountAddress(address)
} catch (error) {
SettingsStore.state.safeSmartAccountEnabled = false
localStorage.removeItem(SAFE_SMART_ACCOUNTS_ENABLED_KEY)
styledToast('Safe smart account initialization failed', 'warning')
}
})()
)
}

if (biconomySmartAccountEnabled) {
promises.push(
(async () => {
try {
const address = await createOrRestoreBiconomySmartAccount(privateKey)
SettingsStore.setBiconomySmartAccountAddress(address)
} catch (error) {
SettingsStore.state.biconomySmartAccountEnabled = false
localStorage.removeItem(BICONOMY_SMART_ACCOUNTS_ENABLED_KEY)
styledToast('Biconomy smart account initialization failed', 'warning')
}
})()
)
}

await Promise.all(promises)
} catch (error) {
console.error('Error initializing smart accounts:', error)
styledToast('Error initializing smart accounts', 'error')
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,7 @@ import {
createSmartAccountClient
} from 'permissionless'
import { PimlicoBundlerActions, pimlicoBundlerActions } from 'permissionless/actions/pimlico'
import {
PIMLICO_NETWORK_NAMES,
publicClientUrl,
publicRPCUrl,
UrlConfig
} from '@/utils/SmartAccountUtil'
import { PIMLICO_NETWORK_NAMES, publicClientUrl, UrlConfig } from '@/utils/SmartAccountUtil'
import { EntryPoint } from 'permissionless/types/entrypoint'
import { Erc7579Actions, erc7579Actions } from 'permissionless/actions/erc7579'
import { SmartAccount } from 'permissionless/accounts'
Expand Down Expand Up @@ -85,7 +80,6 @@ export abstract class SmartAccountLib implements EIP155Wallet {
entryPointVersion = 6
}: SmartAccountLibOptions) {
const apiKey = process.env.NEXT_PUBLIC_PIMLICO_KEY
const publicClientRPCUrl = process.env.NEXT_PUBLIC_LOCAL_CLIENT_URL || publicRPCUrl({ chain })
const paymasterUrl = ({ chain }: UrlConfig) => {
const localPaymasterUrl = process.env.NEXT_PUBLIC_LOCAL_PAYMASTER_URL
if (localPaymasterUrl) {
Expand Down
65 changes: 41 additions & 24 deletions advanced/wallets/react-wallet-v2/src/store/SettingsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import {
} from '@/utils/SmartAccountUtil'
import { Verify, SessionTypes } from '@walletconnect/types'
import { proxy } from 'valtio'
import { styledToast } from '@/utils/HelperUtil'

const TEST_NETS_ENABLED_KEY = 'TEST_NETS'
const CA_ENABLED_KEY = 'CHAIN_ABSTRACTION'
const SMART_ACCOUNTS_ENABLED_KEY = 'SMART_ACCOUNTS'
const ZERO_DEV_SMART_ACCOUNTS_ENABLED_KEY = 'ZERO_DEV_SMART_ACCOUNTS'
const SAFE_SMART_ACCOUNTS_ENABLED_KEY = 'SAFE_SMART_ACCOUNTS'
const BICONOMY_SMART_ACCOUNTS_ENABLED_KEY = 'BICONOMY_SMART_ACCOUNTS'
export const SMART_ACCOUNTS_ENABLED_KEY = 'SMART_ACCOUNTS'
export const ZERO_DEV_SMART_ACCOUNTS_ENABLED_KEY = 'ZERO_DEV_SMART_ACCOUNTS'
export const SAFE_SMART_ACCOUNTS_ENABLED_KEY = 'SAFE_SMART_ACCOUNTS'
export const BICONOMY_SMART_ACCOUNTS_ENABLED_KEY = 'BICONOMY_SMART_ACCOUNTS'
const MODULE_MANAGEMENT_ENABLED_KEY = 'MODULE_MANAGEMENT'

/**
Expand Down Expand Up @@ -213,13 +214,19 @@ const SettingsStore = {

async toggleKernelSmartAccountsEnabled() {
state.kernelSmartAccountEnabled = !state.kernelSmartAccountEnabled

if (state.kernelSmartAccountEnabled) {
const { eip155Addresses, eip155Wallets } = createOrRestoreEIP155Wallet()
const { kernelSmartAccountAddress } = await createOrRestoreKernelSmartAccount(
eip155Wallets[eip155Addresses[0]].getPrivateKey()
)
SettingsStore.setKernelSmartAccountAddress(kernelSmartAccountAddress)
localStorage.setItem(ZERO_DEV_SMART_ACCOUNTS_ENABLED_KEY, 'YES')
try {
const { eip155Addresses, eip155Wallets } = createOrRestoreEIP155Wallet()
const address = await createOrRestoreKernelSmartAccount(
eip155Wallets[eip155Addresses[0]].getPrivateKey()
)
SettingsStore.setKernelSmartAccountAddress(address)
localStorage.setItem(ZERO_DEV_SMART_ACCOUNTS_ENABLED_KEY, 'YES')
} catch (error) {
state.kernelSmartAccountEnabled = false
styledToast('Failed to initialize Kernel smart account', 'error')
}
} else {
removeSmartAccount(SettingsStore.state.kernelSmartAccountAddress)
SettingsStore.setKernelSmartAccountAddress('')
Expand All @@ -231,13 +238,19 @@ const SettingsStore = {

async toggleSafeSmartAccountsEnabled() {
state.safeSmartAccountEnabled = !state.safeSmartAccountEnabled

if (state.safeSmartAccountEnabled) {
const { eip155Addresses, eip155Wallets } = createOrRestoreEIP155Wallet()
const { safeSmartAccountAddress } = await createOrRestoreSafeSmartAccount(
eip155Wallets[eip155Addresses[0]].getPrivateKey()
)
SettingsStore.setSafeSmartAccountAddress(safeSmartAccountAddress)
localStorage.setItem(SAFE_SMART_ACCOUNTS_ENABLED_KEY, 'YES')
try {
const { eip155Addresses, eip155Wallets } = createOrRestoreEIP155Wallet()
const address = await createOrRestoreSafeSmartAccount(
eip155Wallets[eip155Addresses[0]].getPrivateKey()
)
SettingsStore.setSafeSmartAccountAddress(address)
localStorage.setItem(SAFE_SMART_ACCOUNTS_ENABLED_KEY, 'YES')
} catch (error) {
state.safeSmartAccountEnabled = false
styledToast('Failed to initialize Safe smart account', 'error')
}
} else {
removeSmartAccount(SettingsStore.state.safeSmartAccountAddress)
SettingsStore.setSafeSmartAccountAddress('')
Expand All @@ -249,18 +262,22 @@ const SettingsStore = {

async toggleBiconomySmartAccountsEnabled() {
state.biconomySmartAccountEnabled = !state.biconomySmartAccountEnabled

if (state.biconomySmartAccountEnabled) {
const { eip155Addresses, eip155Wallets } = createOrRestoreEIP155Wallet()
const { biconomySmartAccountAddress } = await createOrRestoreBiconomySmartAccount(
eip155Wallets[eip155Addresses[0]].getPrivateKey()
)
SettingsStore.setBiconomySmartAccountAddress(biconomySmartAccountAddress)
localStorage.setItem(BICONOMY_SMART_ACCOUNTS_ENABLED_KEY, 'YES')
try {
const { eip155Addresses, eip155Wallets } = createOrRestoreEIP155Wallet()
const address = await createOrRestoreBiconomySmartAccount(
eip155Wallets[eip155Addresses[0]].getPrivateKey()
)
SettingsStore.setBiconomySmartAccountAddress(address)
localStorage.setItem(BICONOMY_SMART_ACCOUNTS_ENABLED_KEY, 'YES')
} catch (error) {
state.biconomySmartAccountEnabled = false
styledToast('Failed to initialize Biconomy smart account', 'error')
}
} else {
removeSmartAccount(SettingsStore.state.biconomySmartAccountAddress)
SettingsStore.setBiconomySmartAccountAddress('')
state.moduleManagementEnabled = false
localStorage.removeItem(MODULE_MANAGEMENT_ENABLED_KEY)
localStorage.removeItem(BICONOMY_SMART_ACCOUNTS_ENABLED_KEY)
}
}
Expand Down
Loading