Wagmi
A wagmi app connects to Tempo ID, and sends a transfer with the access key of the connection.
Overview
The wagmi plugin of accounts/wagmi adds a wagmi config whose one connector is the instance, a wallet that Tempo ID backs. A sign-in connects wagmi, a sign-out or the expiry of the session's access key disconnects it, and wagmi's hooks sign with the session's access key, with no prompt. The page's origin is its client ID, so the app needs no registration.
The demo connects, sends 1 OUSD on Tempo testnet with a wagmi/tempo hook, and disconnects.
Walkthrough
The wagmi example runs these steps in a React app.
Install the packages
@wagmi/core is an optional peer dependency of accounts, so the app installs it beside wagmi.
Create the instance
import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { wagmi } from 'accounts/wagmi'
export const accounts = Accounts.create({
// Each connection requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [
viem(),
wagmi(),
],
})Provide the config
Pass accounts.wagmi to wagmi's provider, which restores the connection of a saved session at mount.
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import { createRoot } from 'react-dom/client'
import { WagmiProvider } from 'wagmi'
import { App } from './App.js'
import { accounts } from './tempo.config.js'
const queryClient = new QueryClient()
createRoot(document.getElementById('root')!).render(
<WagmiProvider config={accounts.wagmi}>
<QueryClientProvider client={queryClient}>
<App />
</QueryClientProvider>
</WagmiProvider>,
)
import { useConnect, useConnection, useConnectors } from 'wagmi'
export function App() {
const connection = useConnection()
const [connector] = useConnectors()
const connect = useConnect()
if (connection.status !== 'connected')
return (
<button
disabled={connect.isPending}
onClick={() => connect.mutate({ connector: connector! })}
type="button"
>
Connect with Tempo
</button>
)
return <p>Tempo Account {connection.address}</p>
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { wagmi } from 'accounts/wagmi'
export const accounts = Accounts.create({
// Each connection requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [viem(), wagmi()],
})Connect from a click
connect.mutate opens Tempo ID's dialog, which requests the access key. useConnection then returns the Tempo Account's address.
If the user closes the dialog or denies the request, connect throws Viem's UserRejectedRequestError, and wagmi stays disconnected.
import { useConnect, useConnection, useConnectors } from 'wagmi'
export function App() {
const connection = useConnection()
const [connector] = useConnectors()
const connect = useConnect()
if (connection.status !== 'connected')
return (
<button
disabled={connect.isPending}
onClick={() => connect.mutate({ connector: connector! })}
type="button"
>
Connect with Tempo
</button>
)
return <p>Tempo Account {connection.address}</p>
}Send a transfer
Hooks.token.useTransferSync sends a transfer, and returns its receipt.
An app without React passes accounts.wagmi to the actions of @wagmi/core, such as Actions.token.transferSync of @wagmi/core/tempo.
import { parseUnits } from 'viem'
import { Addresses } from 'viem/tempo'
import { useConnect, useConnection, useConnectors } from 'wagmi'
import { Hooks } from 'wagmi/tempo'
export function App() {
const connection = useConnection()
const [connector] = useConnectors()
const connect = useConnect()
const send = Hooks.token.useTransferSync()
if (connection.status !== 'connected')
return (
<button
disabled={connect.isPending}
onClick={() => connect.mutate({ connector: connector! })}
type="button"
>
Connect with Tempo
</button>
)
return (
<main>
<p>Tempo Account {connection.address}</p>
<button
disabled={send.isPending}
onClick={() =>
send.mutate({
amount: parseUnits('1', 6),
to: '0x000000000000000000000000000000000000dEaD',
token: Addresses.ousd,
})
}
type="button"
>
Send OUSD
</button>
<p role="status">
{send.data && `Sent in ${send.data.receipt.transactionHash}.`}
</p>
</main>
)
}Disconnect
disconnect.mutate signs the user out of the app with accounts.signOut.
import { useDisconnect } from 'wagmi'
export function Disconnect() {
const disconnect = useDisconnect()
return (
<button
onClick={() => disconnect.mutate()}
type="button"
>
Disconnect
</button>
)
}Use cases
Each use case sets an option of the wagmi plugin or of its config, or uses the tempoId connector.
Connect without an access key
accessKey of wagmi's connect overrides the instance's access key for one connection. false connects with no access key, and signs in again over a held session.
import { connect } from '@wagmi/core'
import { accounts } from './tempo.config.js'
export function connectWithoutAccessKey() {
const [connector] = accounts.wagmi.connectors
return connect(accounts.wagmi, { accessKey: false, connector })
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { wagmi } from 'accounts/wagmi'
export const accounts = Accounts.create({
// Each connection requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [viem(), wagmi()],
})Add other wallets
connectors adds connectors after the instance's, such as an injected wallet. Without it, the config holds only Tempo ID.
import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { wagmi } from 'accounts/wagmi'
import { injected } from 'wagmi/connectors'
export const accounts = Accounts.create({
// Each connection requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [viem(), wagmi({ connectors: [injected()] })],
})Render on a server
accounts.wagmi called with overrides returns a new config, such as one with wagmi's ssr for a page that a server renders. A read without a call returns the default config.
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
import type { ReactNode } from 'react'
import { WagmiProvider } from 'wagmi'
import { accounts } from './tempo.config.js'
const config = accounts.wagmi({ ssr: true })
const queryClient = new QueryClient()
export function Providers(props: { children: ReactNode }) {
return (
<WagmiProvider config={config}>
<QueryClientProvider client={queryClient}>
{props.children}
</QueryClientProvider>
</WagmiProvider>
)
}import { Accounts } from 'accounts'
import { viem } from 'accounts/plugins'
import { wagmi } from 'accounts/wagmi'
export const accounts = Accounts.create({
// Each connection requests an access key that sends up to 100 OUSD for a day.
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
chain: 'testnet',
plugins: [viem(), wagmi()],
})Add Tempo ID to an existing config
An app with its own wagmi config adds the tempoId connector, which takes the options of Accounts.create but chain. It creates the instance on the config's first chain, unless its options name another chain of the config.
import { tempoId } from 'accounts/wagmi'
import { createConfig, http } from 'wagmi'
import { tempoModerato } from 'viem/tempo/chains'
export const config = createConfig({
chains: [tempoModerato],
connectors: [
tempoId({
accessKey: () => ({
expiry: '1d',
limits: [{ limit: '100', token: 'ousd' }],
}),
}),
],
transports: { [tempoModerato.id]: http() },
})