Skip to content

Commit

Permalink
feat: useCreateSessionKey
Browse files Browse the repository at this point in the history
  • Loading branch information
jxom committed Nov 19, 2024
1 parent 09424f4 commit 94a5bbb
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 15 deletions.
48 changes: 40 additions & 8 deletions examples/wagmi/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { formatEther, parseEther } from 'viem'
import { type BaseError, useAccount, useConnect, useReadContract } from 'wagmi'
import { useCallsStatus, useSendCalls } from 'wagmi/experimental'
import { ExperimentERC20 } from './contracts'
import { useCreateAccount, useDisconnect } from './hooks'
import { useCreateAccount, useCreateSessionKey, useDisconnect } from './hooks'

export function App() {
const { isConnected } = useAccount()
Expand All @@ -13,6 +13,7 @@ export function App() {
{isConnected && (
<>
<Balance />
<CreateSessionKey />
<Mint />
</>
)}
Expand Down Expand Up @@ -94,6 +95,23 @@ function Balance() {
)
}

function CreateSessionKey() {
const { data, error, mutate: createSessionKey } = useCreateSessionKey()

return (
<div>
<h2>Create Session Key</h2>
<button onClick={() => createSessionKey()} type="button">
Create Session Key
</button>
{data && <div>Session Key created.</div>}
{error && (
<div>Error: {(error as BaseError).shortMessage || error.message}</div>
)}
</div>
)
}

function Mint() {
const { address } = useAccount()
const { data: id, error, isPending, sendCalls } = useSendCalls()
Expand All @@ -112,9 +130,13 @@ function Mint() {
return (
<div>
<h2>Mint EXP</h2>
<button
disabled={isPending}
onClick={() =>
<form
onSubmit={(e) => {
e.preventDefault()

const formData = new FormData(e.target as HTMLFormElement)
const sessionKeyEnabled = Boolean(formData.get('sessionKeyEnabled'))

sendCalls({
calls: [
{
Expand All @@ -124,12 +146,22 @@ function Mint() {
args: [address!, parseEther('100')],
},
],
capabilities: {
sessionKey: {
enabled: sessionKeyEnabled,
},
},
})
}
type="button"
}}
>
{isPending ? 'Confirming...' : 'Mint 100 EXP'}
</button>
<button disabled={isPending} type="submit">
{isPending ? 'Confirming...' : 'Mint 100 EXP'}
</button>
<label>
<input name="sessionKeyEnabled" type="checkbox" />
Use Session Key
</label>
</form>
{id && <div>Transaction Hash: {id}</div>}
{isConfirming && 'Waiting for confirmation...'}
{isConfirmed && 'Transaction confirmed.'}
Expand Down
30 changes: 29 additions & 1 deletion examples/wagmi/src/hooks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMutation } from '@tanstack/react-query'
import type { EIP1193Provider } from 'viem'
import type { Address, EIP1193Provider, Hex } from 'viem'
import {
type Connector,
ConnectorNotFoundError,
Expand All @@ -26,6 +26,34 @@ export function useCreateAccount() {
})
}

export function useCreateSessionKey() {
const { connector } = useAccount()
return useMutation({
mutationFn: async () => {
if (!connector) throw new ConnectorNotFoundError()
const provider = (await connector.getProvider()) as EIP1193Provider
return provider.request<{
Method: 'experimental_createSessionKey'
Parameters:
| [
{
address?: Address | undefined
expiry?: number | undefined
},
]
| []
ReturnType: {
expiry: number
id: Hex
}
}>({
method: 'experimental_createSessionKey',
params: [],
})
},
})
}

export function useDisconnect() {
const { connector } = useAccount()
const { disconnectAsync } = useDisconnect_wagmi()
Expand Down
11 changes: 6 additions & 5 deletions src/internal/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,12 @@ export function from<
{
address,
expiry = Math.floor(Date.now() / 1000) + 60 * 15, // 15 minutes
},
] = params as RpcSchema.ExtractParams<
RpcSchema_internal.Schema,
'experimental_createSessionKey'
>
} = {},
] =
(params as RpcSchema.ExtractParams<
RpcSchema_internal.Schema,
'experimental_createSessionKey'
>) ?? []

const account = address
? state.accounts.find((account) =>
Expand Down
2 changes: 1 addition & 1 deletion src/internal/rpcSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export type Schema = RpcSchema.From<
| {
Request: {
method: 'experimental_createSessionKey'
params: [CreateSessionKeyParameters]
params?: [CreateSessionKeyParameters] | undefined
}
ReturnType: CreateSessionKeyReturnType
}
Expand Down

0 comments on commit 94a5bbb

Please sign in to comment.