Skip to content

Commit

Permalink
removed no longer need funcs, cleanedup up after conflict mismatches
Browse files Browse the repository at this point in the history
  • Loading branch information
Bacis committed Feb 6, 2024
1 parent 58717a7 commit a2c5757
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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 (
<ChainCard rgb={rgb} flexDirection="row" alignItems="center" flexWrap="wrap">
Expand Down Expand Up @@ -110,35 +96,16 @@ export default function SmartAccountCard({
</Text>
</>
) : null}
{isDeployed && smartAccountAddress ? (
<Button
size="md"
css={{ marginTop: 20, width: '100%' }}
onClick={sendTestTransaction}
disabled={!isActiveChain || loading}
>
{loading ? <Loading size="sm" /> : 'Send Test TX'}
</Button>
) : (
<>
<Button
disabled={!isActiveChain || loading}
disabled={!isActiveChain}
size="sm"
css={{ marginTop: 20, width: '100%' }}
onClick={() => window.open(FAUCET_URLS[chain?.name], '_blank')}
>
{name} Faucet
</Button>
<Button
disabled={!isActiveChain || loading}
size="sm"
css={{ marginTop: 10, width: '100%' }}
onClick={onCreateSmartAccount}
>
{loading ? <Loading size="sm" css={{ paddingTop: 10 }} /> : 'Create Smart Account'}
</Button>
</>
)}
</ChainCard>
)
}
54 changes: 6 additions & 48 deletions advanced/wallets/react-wallet-v2/src/hooks/useSmartAccount.ts
Original file line number Diff line number Diff line change
@@ -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<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({
Expand All @@ -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,
}
}
63 changes: 27 additions & 36 deletions advanced/wallets/react-wallet-v2/src/views/SessionProposalModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 []
Expand Down Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -344,14 +338,11 @@ export default function SessionProposalModal() {
})}

<Row style={{ color: 'GrayText' }}>Smart Accounts</Row>
{smartAccountChains.length &&
smartAccountChains.map((chain, i) => {
return (
<Row key={i}>
<ChainSmartAddressMini namespace={chain?.namespace!} />
</Row>
)
})}
{smartAccountAddress &&
<Row key={1}>
<ChainAddressMini key={1} address={smartAccountAddress} />
</Row>
}
</Grid>
<Grid>
<Row style={{ color: 'GrayText' }} justify="flex-end">
Expand Down

0 comments on commit a2c5757

Please sign in to comment.