* feat: added Goerli smart accounts * chore: remove viem version modifier. Split useSmartAccount usage into multiliune * feat: fix error handling * feat: added smart wallets to proposal modal * feat: added paymasters to be able to freely transact on testnet with Pimlico sponsorship. Serialize SA addresses in relay response * feat: adapted SmartWalletLib interface to be able to use same methods as EIP155Lib. Hooked up all operations on EIP155RequestHandler to smart accounts. * chore: remove logs * feat: added spinner to modal footer. Use it when handling eip155 requests * feat: added sponsorship toggle in settings * feat: upgraded to permissionless@0.0.16 * fix: conflicts after merge * chore: unify loaders * chore: kristoph refactor + multi chain support * fix: chain Id issue. remove unused logs
76 lines
2.3 KiB
TypeScript
76 lines
2.3 KiB
TypeScript
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";
|
|
import { styledToast } from "@/utils/HelperUtil";
|
|
import { TransactionExecutionError } from "viem";
|
|
|
|
export default function useSmartAccount(signerPrivateKey: Hex, chain: Chain) {
|
|
const [loading, setLoading] = useState(false)
|
|
const [client, setClient] = useState<SmartAccountLib>();
|
|
const [isDeployed, setIsDeployed] = useState(false)
|
|
const [address, setAddress] = useState<Hex>()
|
|
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({
|
|
chain,
|
|
privateKey: signerPrivateKey,
|
|
sponsored: smartAccountSponsorshipEnabled,
|
|
})
|
|
setClient(smartAccountClient)
|
|
}, [signerPrivateKey, smartAccountSponsorshipEnabled, chain])
|
|
|
|
useEffect(() => {
|
|
client?.checkIfSmartAccountDeployed()
|
|
.then((deployed: boolean) => {
|
|
setIsDeployed(deployed)
|
|
setAddress(client?.address)
|
|
})
|
|
}, [client, chain])
|
|
|
|
|
|
return {
|
|
address,
|
|
isDeployed,
|
|
deploy,
|
|
loading,
|
|
sendTestTransaction,
|
|
}
|
|
} |