From a2c5757aa15a21e91414a91871ae42a4852af4fc Mon Sep 17 00:00:00 2001 From: Andrius Bacianskas Date: Tue, 6 Feb 2024 09:59:46 +0100 Subject: [PATCH] removed no longer need funcs, cleanedup up after conflict mismatches --- .../src/components/SmartAccountCard.tsx | 35 +---------- .../src/hooks/useSmartAccount.ts | 54 ++-------------- .../src/views/SessionProposalModal.tsx | 63 ++++++++----------- 3 files changed, 34 insertions(+), 118 deletions(-) diff --git a/advanced/wallets/react-wallet-v2/src/components/SmartAccountCard.tsx b/advanced/wallets/react-wallet-v2/src/components/SmartAccountCard.tsx index dc716776f..e5b88c51a 100644 --- a/advanced/wallets/react-wallet-v2/src/components/SmartAccountCard.tsx +++ b/advanced/wallets/react-wallet-v2/src/components/SmartAccountCard.tsx @@ -31,11 +31,7 @@ export default function SmartAccountCard({ const { activeChainId } = useSnapshot(SettingsStore.state) const chain = allowedChains.find((c) => c.id.toString() === chainId.split(':')[1]) as Chain const { - deploy, - isDeployed, address: smartAccountAddress, - loading, - sendTestTransaction, } = useSmartAccount(eip155Wallets[address].getPrivateKey() as `0x${string}`, chain) function onCopy() { @@ -48,16 +44,6 @@ export default function SmartAccountCard({ SettingsStore.setActiveChainId(chainId) await updateSignClientChainId(chainId.toString(), address) } - - async function onCreateSmartAccount() { - try { - if (!isDeployed) { - await deploy() - } - } catch (error) { - console.error(error) - } - } return ( @@ -110,35 +96,16 @@ export default function SmartAccountCard({ ) : null} - {isDeployed && smartAccountAddress ? ( - - ) : ( <> - - )} ) } diff --git a/advanced/wallets/react-wallet-v2/src/hooks/useSmartAccount.ts b/advanced/wallets/react-wallet-v2/src/hooks/useSmartAccount.ts index 1d13a72bb..6bfdd6156 100644 --- a/advanced/wallets/react-wallet-v2/src/hooks/useSmartAccount.ts +++ b/advanced/wallets/react-wallet-v2/src/hooks/useSmartAccount.ts @@ -1,53 +1,16 @@ import { SmartAccountLib } from "@/lib/SmartAccountLib"; import SettingsStore from "@/store/SettingsStore"; -import { Chain, VITALIK_ADDRESS } from "@/utils/SmartAccountUtils"; -import { useCallback, useEffect, useState } from "react"; +import { Chain } from "@/utils/SmartAccountUtils"; +import { useEffect, useState } from "react"; import { useSnapshot } from "valtio"; import { Hex } from "viem"; -import { styledToast } from "@/utils/HelperUtil"; -import { TransactionExecutionError } from "viem"; import { SmartAccount } from "permissionless/accounts"; export default function useSmartAccount(signerPrivateKey: Hex, chain: Chain) { - const [loading, setLoading] = useState(false) const [client, setClient] = useState(); - const [isDeployed, setIsDeployed] = useState(false) const [address, setAddress] = useState() const { smartAccountSponsorshipEnabled } = useSnapshot(SettingsStore.state); - const execute = useCallback(async (callback: () => void) => { - try { - setLoading(true) - const res = await callback() - console.log('result:', res) - setLoading(false) - } - catch (e) { - if (e instanceof TransactionExecutionError) { - // shorten the error message - styledToast(e.cause.message, 'error') - } else if (e instanceof Error) { - styledToast(e.message, 'error') - } - console.error(e) - setLoading(false) - } - }, [setLoading]) - - const deploy = useCallback(async () => { - if (!client) return - execute(client?.deploySmartAccount) - }, [client, execute]) - - const sendTestTransaction = useCallback(async () => { - if (!client) return - execute(() => client?.sendTransaction({ - to: VITALIK_ADDRESS, - value: 0n, - data: '0x', - })) - }, [client, execute]) - useEffect(() => { if (!signerPrivateKey || !chain) return const smartAccountClient = new SmartAccountLib({ @@ -59,18 +22,13 @@ export default function useSmartAccount(signerPrivateKey: Hex, chain: Chain) { }, [signerPrivateKey, smartAccountSponsorshipEnabled, chain]) useEffect(() => { - client?.init() - .then(() => { - setIsDeployed(client?.isDeployed) - setAddress(client?.address) - }) + client?.getAccount() + .then((account: SmartAccount) => { + setAddress(account.address) + }) }, [client, chain]) return { address, - isDeployed, - deploy, - loading, - sendTestTransaction, } } \ No newline at end of file diff --git a/advanced/wallets/react-wallet-v2/src/views/SessionProposalModal.tsx b/advanced/wallets/react-wallet-v2/src/views/SessionProposalModal.tsx index 85056e545..037f24520 100644 --- a/advanced/wallets/react-wallet-v2/src/views/SessionProposalModal.tsx +++ b/advanced/wallets/react-wallet-v2/src/views/SessionProposalModal.tsx @@ -37,6 +37,7 @@ import { useSnapshot } from 'valtio' import SettingsStore from '@/store/SettingsStore' import { Chain, allowedChains } from '@/utils/SmartAccountUtils' import { Hex } from 'viem' +import useSmartAccount from '@/hooks/useSmartAccount' const StyledText = styled(Text, { fontWeight: 400 @@ -184,6 +185,10 @@ export default function SessionProposalModal() { [supportedChains] ) + + + console.log(smartAccountChains, "smartAccountChains") + // get required chains that are not supported by the wallet const notSupportedChains = useMemo(() => { if (!proposal) return [] @@ -226,37 +231,26 @@ export default function SessionProposalModal() { } }, []) + const namespaces = buildApprovedNamespaces({ + proposal: proposal.params, + supportedNamespaces + }) + const chainId = namespaces['eip155'].chains?.[0] + const chainIdParsed = chainId?.replace('eip155:', '') + const signerAddress = namespaces['eip155'].accounts[0].split(':')[2] + const wallet = eip155Wallets[signerAddress] + const chain = allowedChains.find(chain => chain.id.toString() === chainIdParsed)! + + const { + address: smartAccountAddress, + } = useSmartAccount(wallet.getPrivateKey() as Hex, chain) + // Hanlde approve action, construct session namespace const onApprove = useCallback(async () => { if (proposal) { setIsLoadingApprove(true) - const namespaces = buildApprovedNamespaces({ - proposal: proposal.params, - supportedNamespaces - }) - // TODO: improve for multi network - console.log('namespaces', namespaces['eip155']) - const namespaceChains = namespaces['eip155']?.chains?.map((c: string) => c.split(':')[1]) - const smartAccountEnabledChains: Chain[] = allowedChains.filter(chain => namespaceChains?.includes(chain.id.toString())) - // We find a request for a chain that is enabled for smart account - if (smartAccountEnabledChains.length) { - const signerAddress = namespaces['eip155'].accounts[0].split(':')[2] - const wallet = eip155Wallets[signerAddress] - const chain = smartAccountEnabledChains[0] - if (wallet) { - const smartAccountClient = new SmartAccountLib({ - privateKey: wallet.getPrivateKey() as Hex, - chain, - sponsored: smartAccountSponsorshipEnabled, - }) - await smartAccountClient.init() - const isDeployed = await smartAccountClient.checkIfSmartAccountDeployed() - console.log('isDeployed', isDeployed, smartAccountClient.address) - - if (isDeployed) { - namespaces.eip155.accounts = [...namespaces.eip155.accounts, `eip155:${chain.id}:${smartAccountClient.address}`] - } - } + if (wallet && smartAccountAddress) { + namespaces.eip155.accounts = [...namespaces.eip155.accounts, `eip155:${chain.id}:${smartAccountAddress}`] } console.log('approving namespaces:', namespaces) @@ -275,7 +269,7 @@ export default function SessionProposalModal() { } setIsLoadingApprove(false) ModalStore.close() - }, [proposal, supportedNamespaces, smartAccountSponsorshipEnabled]) + }, [chain, namespaces, proposal, smartAccountAddress, wallet]) // Hanlde reject action // eslint-disable-next-line react-hooks/rules-of-hooks @@ -344,14 +338,11 @@ export default function SessionProposalModal() { })} Smart Accounts - {smartAccountChains.length && - smartAccountChains.map((chain, i) => { - return ( - - - - ) - })} + {smartAccountAddress && + + + + }