wallet-connect-web-examples/advanced/wallets/react-wallet-v2/src/hooks/useSmartAccount.ts
tomiir 61b181b9d4
feat: Goerli Smart Accounts. (#347)
* feat: added Goerli smart accounts

* chore: remove viem version modifier. Split useSmartAccount usage into multiple lines

* feat: fix error handling
2023-12-14 11:55:20 -06:00

53 lines
1.5 KiB
TypeScript

import { SmartAccountLib } from "@/lib/SmartAccountLib";
import { useCallback, useEffect, useState } from "react";
export default function useSmartAccount(signerPrivateKey: `0x${string}`) {
const [loading, setLoading] = useState(false)
const [client, setClient] = useState<SmartAccountLib>();
const [isDeployed, setIsDeployed] = useState(false)
const [address, setAddress] = useState<`0x${string}`>()
const execute = useCallback(async (callback: () => void) => {
try {
setLoading(true)
await callback()
setLoading(false)
}
catch (e) {
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?.sendTestTransaction)
}, [client, execute])
useEffect(() => {
const smartAccountClient = new SmartAccountLib(signerPrivateKey, 'goerli')
setClient(smartAccountClient)
}, [signerPrivateKey])
useEffect(() => {
client?.checkIfSmartAccountDeployed()
.then((deployed: boolean) => {
setIsDeployed(deployed)
setAddress(client?.address)
})
}, [client])
return {
address,
isDeployed,
deploy,
loading,
sendTestTransaction,
}
}