Skip to content

Commit

Permalink
save and use chainId of granted Permissions
Browse files Browse the repository at this point in the history
  • Loading branch information
KannuSingh committed Sep 20, 2024
1 parent 6078803 commit 5800cc0
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Button, Flex, Stack, Text } from '@chakra-ui/react'
import { useAccount } from 'wagmi'
import { useReadContract } from 'wagmi'
import { useState } from 'react'
import { useChakraToast } from '../Toast'
Expand All @@ -8,10 +7,10 @@ import { abi as donutContractAbi, address as donutContractaddress } from '../../
import { useLocalEcdsaKey } from '../../context/LocalEcdsaKeyContext'
import { useERC7715Permissions } from '../../hooks/useERC7715Permissions'
import { executeActionsWithECDSAAndCosignerPermissions } from '../../utils/ERC7715Utils'
import { getChain } from '../../utils/NetworksUtil'

export function WagmiPurchaseDonutAsyncPermissionsTest() {
const { privateKey } = useLocalEcdsaKey()
const { chain } = useAccount()
const { grantedPermissions, pci } = useERC7715Permissions()

const {
Expand All @@ -23,7 +22,7 @@ export function WagmiPurchaseDonutAsyncPermissionsTest() {
abi: donutContractAbi,
address: donutContractaddress,
functionName: 'getBalance',
args: [grantedPermissions?.signerData?.submitToAddress || '0x']
args: [grantedPermissions?.permissions?.signerData?.submitToAddress || '0x']
})

const [isTransactionPending, setTransactionPending] = useState<boolean>(false)
Expand All @@ -32,8 +31,13 @@ export function WagmiPurchaseDonutAsyncPermissionsTest() {
async function onPurchaseDonutWithPermissions() {
setTransactionPending(true)
try {
const chainId = grantedPermissions?.chainId
if (!chainId) {
throw new Error('Chain ID not available for granted permissions')
}
const chain = getChain(chainId)
if (!chain) {
throw new Error(`Account connected chain not available`)
throw new Error('Invalid chain')
}
if (!privateKey) {
throw new Error(`Unable to get dApp private key`)
Expand All @@ -60,7 +64,7 @@ export function WagmiPurchaseDonutAsyncPermissionsTest() {
actions: purchaseDonutCallDataExecution,
chain,
ecdsaPrivateKey: privateKey as `0x${string}`,
permissions: grantedPermissions,
permissions: grantedPermissions.permissions,
pci
})
if (txHash) {
Expand Down Expand Up @@ -91,7 +95,7 @@ export function WagmiPurchaseDonutAsyncPermissionsTest() {
return (
<Stack direction={['column', 'column', 'row']}>
<Button
isDisabled={!grantedPermissions}
isDisabled={!grantedPermissions?.permissions || !grantedPermissions.chainId}
isLoading={isTransactionPending}
onClick={onPurchaseDonutWithPermissions}
>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import { encodeFunctionData, parseEther } from 'viem'
import { abi as donutContractAbi, address as donutContractaddress } from '../../utils/DonutContract'
import { useERC7715Permissions } from '../../hooks/useERC7715Permissions'
import { usePasskey } from '../../context/PasskeyContext'
import { sepolia } from 'viem/chains'
import { executeActionsWithPasskeyAndCosignerPermissions } from '../../utils/ERC7715Utils'
import { getChain } from '../../utils/NetworksUtil'

export function WagmiPurchaseDonutSyncPermissionsTest() {
const { passkeyId } = usePasskey()
Expand All @@ -22,7 +22,7 @@ export function WagmiPurchaseDonutSyncPermissionsTest() {
abi: donutContractAbi,
address: donutContractaddress,
functionName: 'getBalance',
args: [grantedPermissions?.signerData?.submitToAddress || '0x']
args: [grantedPermissions?.permissions?.signerData?.submitToAddress || '0x']
})

const [isTransactionPending, setTransactionPending] = useState<boolean>(false)
Expand All @@ -34,6 +34,14 @@ export function WagmiPurchaseDonutSyncPermissionsTest() {
if (!grantedPermissions) {
throw Error('No permissions available')
}
const chainId = grantedPermissions?.chainId
if (!chainId) {
throw new Error('Chain ID not available for granted permissions')
}
const chain = getChain(chainId)
if (!chain) {
throw new Error('Invalid chain')
}
if (!pci) {
throw Error('No WC cosigner data(PCI) available')
}
Expand All @@ -52,9 +60,9 @@ export function WagmiPurchaseDonutSyncPermissionsTest() {
]
const txHash = await executeActionsWithPasskeyAndCosignerPermissions({
actions: purchaseDonutCallDataExecution,
chain: sepolia,
chain,
passkeyId,
permissions: grantedPermissions,
permissions: grantedPermissions.permissions,
pci
})
if (txHash) {
Expand Down Expand Up @@ -86,7 +94,7 @@ export function WagmiPurchaseDonutSyncPermissionsTest() {
return (
<Stack direction={['column', 'column', 'row']}>
<Button
isDisabled={!grantedPermissions}
isDisabled={!grantedPermissions?.permissions || !grantedPermissions.chainId}
isLoading={isTransactionPending}
onClick={onPurchaseDonutWithPermissions}
>
Expand Down
6 changes: 3 additions & 3 deletions apps/laboratory/src/context/ERC7715PermissionsContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import type { AddPermissionResponse } from '../utils/WalletConnectCosignerUtils'

export type ERC7715PermissionsContextType = {
projectId: string
grantedPermissions: GrantPermissionsReturnType | undefined
grantedPermissions: { permissions: GrantPermissionsReturnType; chainId: number } | undefined
setGrantedPermissions: React.Dispatch<
React.SetStateAction<GrantPermissionsReturnType | undefined>
React.SetStateAction<{ permissions: GrantPermissionsReturnType; chainId: number } | undefined>
>
wcCosignerData: AddPermissionResponse | undefined
setWCCosignerData: React.Dispatch<React.SetStateAction<AddPermissionResponse | undefined>>
Expand Down Expand Up @@ -41,7 +41,7 @@ export function ERC7715PermissionsProvider({ children }: ERC7715PermissionsProvi
throw new Error('NEXT_PUBLIC_PROJECT_ID is not set')
}
const [grantedPermissions, setGrantedPermissions] = useLocalStorageState<
GrantPermissionsReturnType | undefined
{ permissions: GrantPermissionsReturnType; chainId: number } | undefined
>(GRANTED_PERMISSIONS_KEY, undefined)
const [wcCosignerData, setWCCosignerData] = useLocalStorageState<
AddPermissionResponse | undefined
Expand Down
5 changes: 4 additions & 1 deletion apps/laboratory/src/hooks/useERC7715Permissions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,10 @@ export function useERC7715Permissions() {
factoryData: approvedPermissions.factoryData || ''
}
})
setGrantedPermissions(approvedPermissions)
setGrantedPermissions({
permissions: approvedPermissions,
chainId: chain.id
})

return { approvedPermissions }
}
Expand Down

0 comments on commit 5800cc0

Please sign in to comment.