223 lines
4.5 KiB
Plaintext

---
description: Request permissions from a wallet to perform actions on behalf of a user.
---
# grantPermissions
Request permissions from a wallet to perform actions on behalf of a user.
[Read more.](https://eips.ethereum.org/EIPS/eip-7715)
:::warning[Warning]
This is an experimental action that is not supported in most wallets. It is recommended to have a fallback mechanism if using this in production.
:::
## Usage
:::code-group
```ts twoslash [example.ts]
import { parseEther } from 'viem'
import { account, walletClient } from './config'
const result = await walletClient.grantPermissions({ // [!code focus:99]
account,
expiry: 1716846083638,
permissions: [
{
type: 'native-token-transfer',
data: {
ticker: 'ETH',
},
policies: [
{
type: 'token-allowance',
data: {
allowance: parseEther('1'),
},
},
],
},
],
})
```
```ts twoslash [config.ts] filename="config.ts"
import 'viem/window'
// ---cut---
import { createWalletClient, custom } from 'viem'
import { mainnet } from 'viem/chains'
import { erc7715Actions } from 'viem/experimental'
export const walletClient = createWalletClient({
chain: mainnet,
transport: custom(window.ethereum!),
}).extend(erc7715Actions())
export const [account] = await walletClient.getAddresses()
```
:::
## Returns
`GrantPermissionsReturnType`
Response from the wallet after issuing permissions.
## Parameters
### account
- **Type:** `Account | Address | undefined`
The Account to scope the permissions to.
```ts twoslash
import { parseEther } from 'viem'
import { account, walletClient } from './config'
const result = await walletClient.grantPermissions({
account, // [!code focus]
expiry: 1716846083638,
permissions: [
{
type: 'native-token-transfer',
data: {
ticker: 'ETH',
},
policies: [
{
type: 'token-allowance',
data: {
allowance: parseEther('1'),
},
},
],
},
],
})
```
### expiry
- **Type:** `number`
The timestamp (in seconds) when the permissions will expire.
```ts twoslash
import { parseEther } from 'viem'
import { account, walletClient } from './config'
const result = await walletClient.grantPermissions({
account,
expiry: 1716846083638, // [!code focus]
permissions: [
{
type: 'native-token-transfer',
data: {
ticker: 'ETH',
},
policies: [
{
type: 'token-allowance',
data: {
allowance: parseEther('1'),
},
},
],
},
],
})
```
### permissions
- **Type:** `Permission[]`
Set of Permissions & Policies to grant to the user.
```ts twoslash
// @noErrors
import { parseEther } from 'viem'
import { account, walletClient } from './config'
const result = await walletClient.grantPermissions({
account,
expiry: 1716846083638,
permissions: [ // [!code focus:19]
{
type: 'native-token-transfer',
data: {
ticker: 'ETH',
},
policies: [
{
type: 'token-allowance',
data: {
allowance: parseEther('1'),
},
},
],
},
{
type: '
// ^|
}
],
})
```
:::tip
The `type` property is constrained to the canonical set of [Permission & Policy types as
defined in ERC-7715](https://github.com/pedrouid/ERCs/blob/19c16341c57f6ac8770cb778d60845dcf30f6a40/ERCS/erc-7715.md#permissions), however, consumers can utilize a `custom` property on `type` as an escape hatch to use custom Permission or Policy types:
```ts twoslash
// @noErrors
import { parseEther } from 'viem'
import { account, walletClient } from './config'
const result = await walletClient.grantPermissions({
account,
expiry: 1716846083638,
permissions: [
{
type: { custom: 'example' }, // [!code focus]
data: {
value: '0xdeadbeef',
}
}
],
})
```
:::
### signer
- **Type:** `Signer | undefined`
Custom signer type to scope the permissions to.
```ts twoslash
import { parseEther } from 'viem'
import { account, walletClient } from './config'
const result = await walletClient.grantPermissions({
expiry: 1716846083638,
permissions: [
{
type: 'native-token-limit',
data: {
amount: parseEther('0.5'),
},
required: true,
},
],
signer: { // [!code focus]
type: 'key', // [!code focus]
data: { // [!code focus]
id: '...' // [!code focus]
} // [!code focus]
} // [!code focus]
})
```