-
Notifications
You must be signed in to change notification settings - Fork 1.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat ERC7715 grant_permissions support on lab #2500
Merged
Merged
Changes from all commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
614dc78
added custom connector and permissiosn dapp structure
KannuSingh aea0b7f
added requestPermissions, clear permissions, purchase donut buttons
KannuSingh 494a068
fix and export types
KannuSingh 2a92fbe
Update .env.example
KannuSingh a6771d0
add reading donut count
KannuSingh 12fbdbe
fix Chain type
KannuSingh 6ab4278
refactor code
KannuSingh 577701c
added GrantedPermissionContext
KannuSingh 3ea0b50
Merge branch 'main' into feat-erc7715-permissions-support
KannuSingh eeca121
update package deps
KannuSingh 8f651f7
run prettier
KannuSingh 465b6e0
refactor code
KannuSingh 0e859df
fix imports
KannuSingh 33ae7fd
code clean up
KannuSingh 7791e10
add env NEXT_PUBLIC_LOCAL_BUNDLER_URL
KannuSingh 5e3bb48
Update .env.example
KannuSingh 791f3eb
remove foundry chain
KannuSingh 9d02122
remove unnecessary destructuring
KannuSingh fa69385
Merge branch 'main' into feat-erc7715-permissions-support
KannuSingh 2cd6d20
Update package-lock.json
KannuSingh 4eed226
Merge branch 'main' into feat-erc7715-permissions-support
KannuSingh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
apps/laboratory/src/components/Wagmi/WagmiCreatePrivateKeySignerTest.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { useState, useEffect } from 'react' | ||
import { Button, Stack } from '@chakra-ui/react' | ||
import { generatePrivateKey, privateKeyToAccount } from 'viem/accounts' | ||
import { useChakraToast } from '../Toast' | ||
import { LOCAL_SIGNER_KEY, setItem, getItem } from '../../utils/LocalStorage' | ||
|
||
export function WagmiCreatePrivateKeySignerTest() { | ||
const toast = useChakraToast() | ||
const [signerAddress, setSignerAddress] = useState<string | undefined>() | ||
|
||
function onCreateNewPrivateKey() { | ||
try { | ||
const privateKey = generatePrivateKey() | ||
setItem(LOCAL_SIGNER_KEY, privateKey) | ||
const account = privateKeyToAccount(privateKey) | ||
setSignerAddress(account.address) | ||
toast({ | ||
title: 'Created new local signer', | ||
description: account.address, | ||
type: 'success' | ||
}) | ||
} catch { | ||
toast({ | ||
title: 'Failure', | ||
description: 'Failed to create new signer', | ||
type: 'error' | ||
}) | ||
} | ||
} | ||
|
||
useEffect(() => { | ||
const storedLocalSignerPrivateKey = getItem(LOCAL_SIGNER_KEY) | ||
enesozturk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (storedLocalSignerPrivateKey) { | ||
const account = privateKeyToAccount(storedLocalSignerPrivateKey as `0x${string}`) | ||
setSignerAddress(account.address) | ||
} | ||
}, []) | ||
|
||
return ( | ||
<Stack direction={['column', 'column', 'row']} align={'center'}> | ||
<Button data-testid="sign-message-button" onClick={onCreateNewPrivateKey}> | ||
Create New Signer | ||
</Button> | ||
{signerAddress && <div>{signerAddress}</div>} | ||
</Stack> | ||
) | ||
} |
51 changes: 51 additions & 0 deletions
51
apps/laboratory/src/components/Wagmi/WagmiPermissionsTest.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import { Box, Card, CardBody, CardHeader, Heading, Stack, StackDivider } from '@chakra-ui/react' | ||
import { WagmiCreatePrivateKeySignerTest } from './WagmiCreatePrivateKeySignerTest' | ||
import { WagmiRequestPermissionsTest } from './WagmiRequestPermissionsTest' | ||
import { WagmiPurchaseDonutWithPermissionsTest } from './WagmiPurchaseDonutPermissionsTest' | ||
import { WagmiSendCallsTest } from './WagmiSendCallsTest' | ||
import { WagmiGetCallsStatusTest } from './WagmiGetCallsStatusTest' | ||
|
||
export function WagmiPermissionsTest() { | ||
return ( | ||
<Card marginTop={10} marginBottom={10}> | ||
<CardHeader> | ||
<Heading size="md">Test Interactions</Heading> | ||
</CardHeader> | ||
|
||
<CardBody> | ||
<Stack divider={<StackDivider />} spacing="4"> | ||
<Box> | ||
<Heading size="xs" textTransform="uppercase" pb="2"> | ||
Send Calls (Atomic Batch) | ||
</Heading> | ||
<WagmiSendCallsTest /> | ||
</Box> | ||
<Box> | ||
<Heading size="xs" textTransform="uppercase" pb="2"> | ||
Get Calls Status | ||
</Heading> | ||
<WagmiGetCallsStatusTest /> | ||
</Box> | ||
<Box> | ||
<Heading size="xs" textTransform="uppercase" pb="2"> | ||
New Private Key | ||
</Heading> | ||
<WagmiCreatePrivateKeySignerTest /> | ||
</Box> | ||
<Box> | ||
<Heading size="xs" textTransform="uppercase" pb="2"> | ||
Request Permissions | ||
</Heading> | ||
<WagmiRequestPermissionsTest /> | ||
</Box> | ||
<Box> | ||
<Heading size="xs" textTransform="uppercase" pb="2"> | ||
Purchase Donut With Permissions | ||
</Heading> | ||
<WagmiPurchaseDonutWithPermissionsTest /> | ||
</Box> | ||
</Stack> | ||
</CardBody> | ||
</Card> | ||
) | ||
} |
109 changes: 109 additions & 0 deletions
109
apps/laboratory/src/components/Wagmi/WagmiPurchaseDonutPermissionsTest.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { Button, Flex, Stack, Text } from '@chakra-ui/react' | ||
import { useAccount, useReadContract } from 'wagmi' | ||
import { useState } from 'react' | ||
import { useChakraToast } from '../Toast' | ||
import { encodeFunctionData, parseEther } from 'viem' | ||
import { abi as donutContractAbi, address as donutContractaddress } from '../../utils/DonutContract' | ||
import { usePermissions } from '../../hooks/usePermissions' | ||
import { useGrantedPermissions } from '../../hooks/useGrantedPermissions' | ||
import { useLocalSigner } from '../../hooks/useLocalSigner' | ||
|
||
export function WagmiPurchaseDonutWithPermissionsTest() { | ||
const { address, chain } = useAccount() | ||
const { buildAndSendTransactionsECDSAKeyAndPermissions } = usePermissions() | ||
const { signerPrivateKey: ecdsaPrivateKey } = useLocalSigner() | ||
|
||
const { grantedPermissions } = useGrantedPermissions() | ||
const { | ||
data: donutsOwned, | ||
refetch: fetchDonutsOwned, | ||
isLoading: donutsQueryLoading, | ||
isRefetching: donutsQueryRefetching | ||
} = useReadContract({ | ||
abi: donutContractAbi, | ||
address: donutContractaddress, | ||
functionName: 'getBalance', | ||
args: [grantedPermissions?.signerData?.submitToAddress || address] | ||
}) | ||
|
||
const [isTransactionPending, setTransactionPending] = useState<boolean>(false) | ||
const toast = useChakraToast() | ||
|
||
async function onPurchaseDonutWithPermissions() { | ||
setTransactionPending(true) | ||
try { | ||
if (!grantedPermissions) { | ||
throw Error('No permissions available') | ||
} | ||
if (!chain) { | ||
throw new Error(`chain ${chain}`) | ||
} | ||
if (!ecdsaPrivateKey) { | ||
throw new Error(`Invalid ecdsaPrivateKey:${ecdsaPrivateKey}`) | ||
} | ||
const purchaseDonutCallData = encodeFunctionData({ | ||
abi: donutContractAbi, | ||
functionName: 'purchase', | ||
args: [1] | ||
}) | ||
const purchaseDonutCallDataExecution = [ | ||
{ | ||
target: donutContractaddress as `0x${string}`, | ||
value: parseEther('0.0001'), | ||
callData: purchaseDonutCallData | ||
} | ||
] | ||
const txHash = await buildAndSendTransactionsECDSAKeyAndPermissions({ | ||
actions: purchaseDonutCallDataExecution, | ||
ecdsaPrivateKey: ecdsaPrivateKey as `0x${string}`, | ||
permissions: grantedPermissions, | ||
chain | ||
}) | ||
if (txHash) { | ||
toast({ | ||
title: 'Transaction success', | ||
description: txHash, | ||
type: 'success' | ||
}) | ||
await fetchDonutsOwned() | ||
} | ||
} catch (error) { | ||
toast({ | ||
title: 'Transaction Failed', | ||
description: `${error}`, | ||
type: 'error' | ||
}) | ||
} | ||
setTransactionPending(false) | ||
} | ||
|
||
if (!grantedPermissions) { | ||
return ( | ||
<Text fontSize="md" color="yellow"> | ||
Dapp does not have the permissions | ||
</Text> | ||
) | ||
} | ||
|
||
return ( | ||
<Stack direction={['column', 'column', 'row']}> | ||
<Button | ||
isDisabled={!grantedPermissions} | ||
isLoading={isTransactionPending} | ||
onClick={onPurchaseDonutWithPermissions} | ||
> | ||
Purchase Donut | ||
</Button> | ||
<Flex alignItems="center"> | ||
{donutsQueryLoading || donutsQueryRefetching ? ( | ||
<Text>Fetching donuts...</Text> | ||
) : ( | ||
<> | ||
<Text marginRight="5px">Crypto donuts left:</Text> | ||
<Text>{donutsOwned?.toString()}</Text> | ||
</> | ||
)} | ||
</Flex> | ||
</Stack> | ||
) | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we remove this at some point?