Hooks.useUpgradeAccount
Hook to upgrade an Externally-Owned Account (EOA) into a Porto Account.
Internally uses wallet_prepareUpgradeAccount & wallet_upgradeAccount.
Usage
example.ts
import { Hooks } from 'porto/wagmi'
import { privateKeyToAccount } from 'viem/accounts'
import { porto } from 'wagmi/connectors'
import { config } from './config'
const eoa = privateKeyToAccount('0x...')
function Example() {
const upgradeAccount = Hooks.useUpgradeAccount()
return (
<button onClick={() => upgradeAccount.mutate({
account: eoa,
connector: porto(),
})}>Upgrade Account</button>
)
}Grant Permissions
You can grant permissions for an application to perform actions on behalf of the account by providing the grantPermissions parameter.
import { Hooks } from 'porto/wagmi'
import { parseEther } from 'viem'
import { privateKeyToAccount } from 'viem/accounts'
import { porto } from 'wagmi/connectors'
const eoa = privateKeyToAccount('0x...')
const token = '0xaf3b0a5b4becc4fa1dfafe74580efa19a2ea49fa'
function Example() {
const upgradeAccount = Hooks.useUpgradeAccount()
return (
<button onClick={() => upgradeAccount.mutate({
account: eoa,
connector: porto(),
grantPermissions: {
expiry: Math.floor(Date.now() / 1_000) + 60 * 60, // 1 hour
feeToken: {
limit: '1',
symbol: 'USDC',
},
permissions: {
calls: [{
signature: 'transfer(address,uint256)',
to: token,
}],
spend: [{
limit: parseEther('50'), // 50 EXP
period: 'day',
token: token,
}],
},
},
})}>Upgrade Account</button>
)
}Parameters
account
Account
The EOA to upgrade.
connector
Connector | CreateConnectorFn
The connector to use for the connection.
grantPermissions
Permissions to grant to the account.
type GrantPermissions = {
/** Expiry timestamp for the permissions */
expiry: number
/**
* Fee token that will be used with these permissions.
*/
feeToken: {
/** Value of the limit in the symbols's unit (e.g. '1' = 1 USDC). */
limit: string
/** Symbol of the fee token. `undefined` for native token. */
symbol?: Token.Symbol | undefined
} | undefined,
/** Key to grant permissions to. Defaults to a wallet-managed key. */
key?: {
/** Public key. Accepts an address for `address` & `secp256k1` types. */
publicKey?: `0x${string}`
/** Key type. */
type?: 'address' | 'p256' | 'secp256k1' | 'webauthn-p256'
}
/** Permissions to grant */
permissions: {
/** Call permissions */
calls: {
/** Function signature or 4-byte signature */
signature?: string
/** Authorized target address */
to?: `0x${string}`
}[]
/** Spend permissions */
spend: {
/** Spending limit (in wei) per period */
limit: `0x${string}`
/** Period of the spend limit */
period: 'minute' | 'hour' | 'day' | 'week' | 'month' | 'year'
/** ERC20 token to set the limit on (defaults to native token) */
token?: `0x${string}`
}[]
}
}Return Value
address
Address
Address of the upgraded account.