Skip to content

Commit

Permalink
chore: kristoph refactor + multi chain support
Browse files Browse the repository at this point in the history
  • Loading branch information
tomiir committed Feb 1, 2024
1 parent bc8acfd commit b48343c
Show file tree
Hide file tree
Showing 13 changed files with 306 additions and 279 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import { Hex } from 'viem'
import ChainAddressMini from './ChainAddressMini'
import { createOrRestoreEIP155Wallet, eip155Wallets } from '@/utils/EIP155WalletUtil'
import { Spinner } from '@nextui-org/react'
import { Chain, allowedChains } from '@/utils/SmartAccountUtils'
import { useSnapshot } from 'valtio'
import SettingsStore from '@/store/SettingsStore'

interface Props {
namespace: string
Expand All @@ -18,7 +21,8 @@ const getKey = (namespace?: string) => {
}

export default function ChainSmartAddressMini({ namespace }: Props) {
const { address } = useSmartAccount(getKey(namespace) as `0x${string}`)
const { activeChainId } = useSnapshot(SettingsStore.state)
const { address } = useSmartAccount(getKey(namespace) as `0x${string}`, allowedChains.find((c) => c.id.toString() === activeChainId) as Chain)

if (!address) return <Spinner />
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import { updateSignClientChainId } from '@/utils/WalletConnectUtil'
import { Avatar, Button, Text, Tooltip, Loading } from '@nextui-org/react'
import { eip155Wallets } from '@/utils/EIP155WalletUtil'
import Image from 'next/image'
import { useState, useEffect } from 'react'
import { useState } from 'react'
import { useSnapshot } from 'valtio'
import useSmartAccount from '@/hooks/useSmartAccount'
import { Chain, allowedChains } from '@/utils/SmartAccountUtils'

interface Props {
name: string
Expand All @@ -28,13 +29,14 @@ export default function SmartAccountCard({
}: Props) {
const [copied, setCopied] = useState(false)
const { activeChainId } = useSnapshot(SettingsStore.state)
const chain = allowedChains.find((c) => c.id.toString() === chainId.split(':')[0]) as Chain
const {
deploy,
isDeployed,
address: smartAccountAddress,
loading,
sendTestTransaction,
} = useSmartAccount(eip155Wallets[address].getPrivateKey() as `0x${string}`)
} = useSmartAccount(eip155Wallets[address].getPrivateKey() as `0x${string}`, chain)

function onCopy() {
navigator?.clipboard?.writeText(address)
Expand Down
4 changes: 3 additions & 1 deletion advanced/wallets/react-wallet-v2/src/data/EIP155Data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ export const EIP155_TEST_CHAINS: Record<string,EIP155Chain> = {
rgb: '99, 125, 234',
rpc: 'https://rpc.sepolia.org',
namespace: 'eip155',
smartAccountEnabled: true,
},
'eip155:43113': {
chainId: 43113,
Expand All @@ -96,7 +97,8 @@ export const EIP155_TEST_CHAINS: Record<string,EIP155Chain> = {
logo: '/chain-logos/eip155-137.png',
rgb: '130, 71, 229',
rpc: 'https://matic-mumbai.chainstacklabs.com',
namespace: 'eip155'
namespace: 'eip155',
smartAccountEnabled: true,
},
'eip155:420': {
chainId: 420,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export default function useInitialization() {
// restart transport if relayer region changes
const onRelayerRegionChange = useCallback(() => {
try {
web3wallet.core.relayer.restartTransport(relayerRegionURL)
web3wallet?.core?.relayer.restartTransport(relayerRegionURL)
prevRelayerURLValue.current = relayerRegionURL
} catch (err: unknown) {
alert(err)
Expand Down
21 changes: 14 additions & 7 deletions advanced/wallets/react-wallet-v2/src/hooks/useSmartAccount.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { SmartAccountLib } from "@/lib/SmartAccountLib";
import SettingsStore from "@/store/SettingsStore";
import { Chain, VITALIK_ADDRESS } from "@/utils/SmartAccountUtils";
import { useCallback, useEffect, useState } from "react";
import { useSnapshot } from "valtio";
import { Hex } from "viem";

export default function useSmartAccount(signerPrivateKey?: Hex) {
export default function useSmartAccount(signerPrivateKey: Hex, chain: Chain) {
const [loading, setLoading] = useState(false)
const [client, setClient] = useState<SmartAccountLib>();
const [isDeployed, setIsDeployed] = useState(false)
Expand All @@ -14,7 +15,8 @@ export default function useSmartAccount(signerPrivateKey?: Hex) {
const execute = useCallback(async (callback: () => void) => {
try {
setLoading(true)
await callback()
const res = await callback()
console.log('result:', res)
setLoading(false)
}
catch (e) {
Expand All @@ -30,26 +32,31 @@ export default function useSmartAccount(signerPrivateKey?: Hex) {

const sendTestTransaction = useCallback(async () => {
if (!client) return
execute(client?.sendTestTransaction)
execute(() => client?.sendTransaction({
to: VITALIK_ADDRESS,
value: 0n,
data: '0x',
}))
}, [client, execute])

useEffect(() => {
if (!signerPrivateKey) return
console.log('chain', chain)
if (!signerPrivateKey || !chain) return
const smartAccountClient = new SmartAccountLib({
chain,
privateKey: signerPrivateKey,
chain: 'goerli',
sponsored: smartAccountSponsorshipEnabled,
})
setClient(smartAccountClient)
}, [signerPrivateKey, smartAccountSponsorshipEnabled])
}, [signerPrivateKey, smartAccountSponsorshipEnabled, chain])

useEffect(() => {
client?.checkIfSmartAccountDeployed()
.then((deployed: boolean) => {
setIsDeployed(deployed)
setAddress(client?.address)
})
}, [client])
}, [client, chain])


return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default function useWalletConnectEventsManager(initialized: boolean) {
* Set up WalletConnect event listeners
*****************************************************************************/
useEffect(() => {
if (initialized) {
if (initialized && web3wallet) {
//sign
web3wallet.on('session_proposal', onSessionProposal)
web3wallet.on('session_request', onSessionRequest)
Expand Down
Loading

0 comments on commit b48343c

Please sign in to comment.