* feat: added Goerli smart accounts * chore: remove viem version modifier. Split useSmartAccount usage into multiple lines * feat: fix error handling
53 lines
1.5 KiB
TypeScript
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,
|
|
}
|
|
} |