Skip to content
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 21 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions apps/demo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
"sonner": "1.4.3",
"tailwind-merge": "2.2.1",
"vaul": "0.9.0",
"viem": "2.10.3",
"wagmi": "2.9.10",
"viem": "2.16.2",
"wagmi": "2.10.9",
"zustand": "4.5.2"
},
"devDependencies": {
Expand Down
6 changes: 5 additions & 1 deletion apps/laboratory/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,8 @@ NEXT_PUBLIC_SECURE_SITE_SDK_URL=""
SOCIAL_TEST_EMAIL=""
SOCIAL_TEST_PASSWORD=""
NEXT_PUBLIC_DEFAULT_LOG_LEVEL=""

NEXT_PUBLIC_PIMLICO_KEY=""
# local env setup guide https://docs.pimlico.io/permissionless/how-to/local-testing"
NEXT_PUBLIC_LOCAL_BUNDLER_URL=
NEXT_PUBLIC_LOCAL_PAYMASTER_URL=
NEXT_PUBLIC_LOCAL_CLIENT_URL=
8 changes: 5 additions & 3 deletions apps/laboratory/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,21 +41,23 @@
"@solana/wallet-adapter-wallets": "0.19.32",
"@solana/web3.js": "1.91.7",
"@tanstack/react-query": "5.24.8",
"@wagmi/connectors": "5.0.8",
"@wagmi/connectors": "5.0.21",
"@walletconnect/utils": "2.13.1",
"@web3modal/ethers": "5.0.6",
"@web3modal/siwe": "5.0.6",
"@web3modal/solana": "5.0.6",
"@web3modal/wagmi": "5.0.6",
"bs58": "6.0.0",
"ethers": "6.13.0",
"framer-motion": "10.17.9",
"next": "14.2.3",
"next-auth": "4.24.5",
"permissionless": "0.1.31",
"react-icons": "4.12.0",
"rpc-websockets": "7.11.0",
"valtio": "1.11.2",
"viem": "2.10.3",
"wagmi": "2.9.10"
"viem": "2.16.2",
"wagmi": "2.10.9"
},
"devDependencies": {
"@aws-sdk/client-cloudwatch": "3.509.0",
Expand Down
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)
Copy link
Collaborator

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?

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 apps/laboratory/src/components/Wagmi/WagmiPermissionsTest.tsx
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>
)
}
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>
)
}
Loading
Loading