or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

account-chain-state.mdadvanced-features.mdblockchain-data-reading.mdconfiguration.mdconnection-management.mdcontract-interactions.mdens-operations.mdevent-watching.mdindex.mdsigning-verification.mdtanstack-query.mdtransaction-management.md

account-chain-state.mddocs/

0

# Account & Chain State

1

2

Access to current account information, chain details, and connection status. Essential for understanding application state.

3

4

## Capabilities

5

6

### Get Account Information

7

8

Retrieves current connected account details and connection status.

9

10

```typescript { .api }

11

/**

12

* Gets current connected account information

13

* @param config - Wagmi configuration

14

* @returns Current account state

15

*/

16

function getAccount<config extends Config>(config: config): GetAccountReturnType<config>;

17

18

interface GetAccountReturnType<config extends Config> {

19

/** Primary connected address */

20

address?: Address;

21

/** All connected addresses */

22

addresses?: readonly Address[];

23

/** Current chain information */

24

chain?: Chain;

25

/** Current chain ID */

26

chainId?: config['chains'][number]['id'];

27

/** Active connector */

28

connector?: Connector;

29

/** Whether wallet is connected */

30

isConnected: boolean;

31

/** Whether connection is in progress */

32

isConnecting: boolean;

33

/** Whether wallet is disconnected */

34

isDisconnected: boolean;

35

/** Whether reconnection is in progress */

36

isReconnecting: boolean;

37

/** Current connection status */

38

status: 'connected' | 'connecting' | 'disconnected' | 'reconnecting';

39

}

40

```

41

42

**Usage Example:**

43

44

```typescript

45

import { getAccount } from '@wagmi/core'

46

47

const account = getAccount(config)

48

49

if (account.isConnected) {

50

console.log('Connected address:', account.address)

51

console.log('All addresses:', account.addresses)

52

console.log('Current chain:', account.chain?.name)

53

console.log('Connector:', account.connector?.name)

54

} else if (account.isConnecting) {

55

console.log('Connection in progress...')

56

} else {

57

console.log('Not connected')

58

}

59

60

// Check specific connection states

61

if (account.isReconnecting) {

62

console.log('Attempting to reconnect...')

63

}

64

```

65

66

### Get Chain ID

67

68

Retrieves the current chain ID.

69

70

```typescript { .api }

71

/**

72

* Gets current chain ID

73

* @param config - Wagmi configuration

74

* @returns Current chain ID

75

*/

76

function getChainId<config extends Config>(config: config): GetChainIdReturnType<config>;

77

78

type GetChainIdReturnType<config extends Config> = config['chains'][number]['id'];

79

```

80

81

**Usage Example:**

82

83

```typescript

84

import { getChainId } from '@wagmi/core'

85

86

const chainId = getChainId(config)

87

console.log('Current chain ID:', chainId)

88

89

// Use with chain information

90

const currentChain = config.chains.find(chain => chain.id === chainId)

91

console.log('Current chain name:', currentChain?.name)

92

```

93

94

### Get Configured Chains

95

96

Retrieves all configured blockchain networks.

97

98

```typescript { .api }

99

/**

100

* Gets all configured chains

101

* @param config - Wagmi configuration

102

* @returns Array of configured chains

103

*/

104

function getChains<config extends Config>(config: config): GetChainsReturnType<config>;

105

106

type GetChainsReturnType<config extends Config> = config['chains'];

107

```

108

109

**Usage Example:**

110

111

```typescript

112

import { getChains } from '@wagmi/core'

113

114

const chains = getChains(config)

115

console.log('Available chains:')

116

chains.forEach(chain => {

117

console.log(`- ${chain.name} (${chain.id})`)

118

})

119

120

// Find specific chain

121

const mainnet = chains.find(chain => chain.id === 1)

122

```

123

124

### Get Active Connections

125

126

Retrieves all active wallet connections.

127

128

```typescript { .api }

129

/**

130

* Gets current active connections

131

* @param config - Wagmi configuration

132

* @returns Array of active connections

133

*/

134

function getConnections<config extends Config>(config: config): GetConnectionsReturnType<config>;

135

136

interface GetConnectionsReturnType<config extends Config> {

137

/** Connected accounts */

138

accounts: readonly Address[];

139

/** Connection chain ID */

140

chainId: config['chains'][number]['id'];

141

/** Connected connector */

142

connector: Connector;

143

}[]

144

```

145

146

**Usage Example:**

147

148

```typescript

149

import { getConnections } from '@wagmi/core'

150

151

const connections = getConnections(config)

152

153

if (connections.length > 0) {

154

console.log('Active connections:')

155

connections.forEach((connection, index) => {

156

console.log(`Connection ${index + 1}:`)

157

console.log('- Connector:', connection.connector.name)

158

console.log('- Chain ID:', connection.chainId)

159

console.log('- Accounts:', connection.accounts)

160

})

161

} else {

162

console.log('No active connections')

163

}

164

165

// Get primary connection

166

const primaryConnection = connections[0]

167

```

168

169

### Get Available Connectors

170

171

Retrieves all available wallet connectors.

172

173

```typescript { .api }

174

/**

175

* Gets available connectors

176

* @param config - Wagmi configuration

177

* @returns Array of available connectors

178

*/

179

function getConnectors<config extends Config>(config: config): GetConnectorsReturnType<config>;

180

181

type GetConnectorsReturnType<config extends Config> = readonly Connector[];

182

```

183

184

**Usage Example:**

185

186

```typescript

187

import { getConnectors } from '@wagmi/core'

188

189

const connectors = getConnectors(config)

190

191

console.log('Available connectors:')

192

connectors.forEach(connector => {

193

console.log(`- ${connector.name} (${connector.id})`)

194

console.log(` Type: ${connector.type}`)

195

if (connector.icon) {

196

console.log(` Icon: ${connector.icon}`)

197

}

198

})

199

200

// Find specific connector

201

const metaMask = connectors.find(connector => connector.id === 'metaMask')

202

const injectedWallets = connectors.filter(connector => connector.type === 'injected')

203

```

204

205

### Get Clients

206

207

Retrieves viem clients for blockchain interactions.

208

209

```typescript { .api }

210

/**

211

* Gets viem client for chain operations

212

* @param config - Wagmi configuration

213

* @param parameters - Client parameters (optional)

214

* @returns Viem client instance

215

*/

216

function getClient<config extends Config>(

217

config: config,

218

parameters?: GetClientParameters<config>

219

): GetClientReturnType<config>;

220

221

interface GetClientParameters<config extends Config> {

222

/** Chain ID to get client for */

223

chainId?: config['chains'][number]['id'];

224

}

225

226

type GetClientReturnType<config extends Config> = Client;

227

228

/**

229

* Gets public (read-only) viem client

230

* @param config - Wagmi configuration

231

* @param parameters - Public client parameters (optional)

232

* @returns Public viem client

233

*/

234

function getPublicClient<config extends Config>(

235

config: config,

236

parameters?: GetPublicClientParameters<config>

237

): GetPublicClientReturnType<config>;

238

239

interface GetPublicClientParameters<config extends Config> {

240

/** Chain ID to get public client for */

241

chainId?: config['chains'][number]['id'];

242

}

243

244

type GetPublicClientReturnType<config extends Config> = PublicClient;

245

```

246

247

**Usage Example:**

248

249

```typescript

250

import { getClient, getPublicClient } from '@wagmi/core'

251

252

// Get client for current chain

253

const client = getClient(config)

254

255

// Get client for specific chain

256

const mainnetClient = getClient(config, { chainId: 1 })

257

258

// Get read-only client

259

const publicClient = getPublicClient(config)

260

261

// Use clients directly with viem

262

const blockNumber = await publicClient.getBlockNumber()

263

console.log('Latest block:', blockNumber)

264

```

265

266

## State Watching

267

268

### Watch Account Changes

269

270

Monitor account state changes in real-time.

271

272

```typescript { .api }

273

/**

274

* Watches for account changes

275

* @param config - Wagmi configuration

276

* @param parameters - Watch parameters

277

* @returns Unsubscribe function

278

*/

279

function watchAccount<config extends Config>(

280

config: config,

281

parameters: WatchAccountParameters<config>

282

): WatchAccountReturnType;

283

284

interface WatchAccountParameters<config extends Config> {

285

/** Callback when account changes */

286

onChange(account: GetAccountReturnType<config>): void;

287

}

288

289

type WatchAccountReturnType = () => void; // Unsubscribe function

290

```

291

292

**Usage Example:**

293

294

```typescript

295

import { watchAccount } from '@wagmi/core'

296

297

const unsubscribe = watchAccount(config, {

298

onChange(account) {

299

if (account.isConnected) {

300

console.log('Account connected:', account.address)

301

} else if (account.isDisconnected) {

302

console.log('Account disconnected')

303

}

304

305

// Handle account changes

306

if (account.address) {

307

// Update UI, fetch user data, etc.

308

updateUserInterface(account.address)

309

}

310

},

311

})

312

313

// Cleanup when component unmounts

314

// unsubscribe()

315

```

316

317

### Watch Chain Changes

318

319

Monitor chain ID changes in real-time.

320

321

```typescript { .api }

322

/**

323

* Watches for chain ID changes

324

* @param config - Wagmi configuration

325

* @param parameters - Watch parameters

326

* @returns Unsubscribe function

327

*/

328

function watchChainId<config extends Config>(

329

config: config,

330

parameters: WatchChainIdParameters<config>

331

): WatchChainIdReturnType;

332

333

interface WatchChainIdParameters<config extends Config> {

334

/** Callback when chain ID changes */

335

onChange(chainId: GetChainIdReturnType<config>): void;

336

}

337

338

type WatchChainIdReturnType = () => void;

339

```

340

341

**Usage Example:**

342

343

```typescript

344

import { watchChainId } from '@wagmi/core'

345

346

const unsubscribe = watchChainId(config, {

347

onChange(chainId) {

348

console.log('Chain changed to:', chainId)

349

350

// Find chain information

351

const chain = config.chains.find(c => c.id === chainId)

352

if (chain) {

353

console.log('Chain name:', chain.name)

354

// Update app state, refresh data, etc.

355

handleChainChange(chain)

356

}

357

},

358

})

359

360

// Cleanup

361

// unsubscribe()

362

```

363

364

### Watch Client Changes

365

366

Monitor viem client changes.

367

368

```typescript { .api }

369

/**

370

* Watches for client changes

371

* @param config - Wagmi configuration

372

* @param parameters - Watch parameters

373

* @returns Unsubscribe function

374

*/

375

function watchClient<config extends Config>(

376

config: config,

377

parameters: WatchClientParameters<config>

378

): WatchClientReturnType;

379

380

interface WatchClientParameters<config extends Config> {

381

/** Callback when client changes */

382

onChange(client: GetClientReturnType<config>): void;

383

}

384

385

type WatchClientReturnType = () => void;

386

387

/**

388

* Watches for public client changes

389

* @param config - Wagmi configuration

390

* @param parameters - Watch parameters

391

* @returns Unsubscribe function

392

*/

393

function watchPublicClient<config extends Config>(

394

config: config,

395

parameters: WatchPublicClientParameters<config>

396

): WatchPublicClientReturnType;

397

398

interface WatchPublicClientParameters<config extends Config> {

399

/** Callback when public client changes */

400

onChange(publicClient: GetPublicClientReturnType<config>): void;

401

}

402

403

type WatchPublicClientReturnType = () => void;

404

```

405

406

**Usage Example:**

407

408

```typescript

409

import { watchClient, watchPublicClient } from '@wagmi/core'

410

411

// Watch for client changes (typically when chain switches)

412

const unsubscribeClient = watchClient(config, {

413

onChange(client) {

414

console.log('Client changed:', client.chain?.name)

415

// Update app with new client

416

updateBlockchainConnection(client)

417

},

418

})

419

420

// Watch public client changes

421

const unsubscribePublicClient = watchPublicClient(config, {

422

onChange(publicClient) {

423

console.log('Public client changed')

424

// Refresh read-only data

425

refreshBlockchainData(publicClient)

426

},

427

})

428

```

429

430

## Combined State Example

431

432

Here's a comprehensive example showing how to use multiple state functions together:

433

434

```typescript

435

import {

436

getAccount,

437

getChainId,

438

getChains,

439

getConnections,

440

getConnectors,

441

watchAccount,

442

watchChainId

443

} from '@wagmi/core'

444

445

// Get current state

446

function getCurrentState() {

447

const account = getAccount(config)

448

const chainId = getChainId(config)

449

const chains = getChains(config)

450

const connections = getConnections(config)

451

const connectors = getConnectors(config)

452

453

return {

454

// Account info

455

isConnected: account.isConnected,

456

address: account.address,

457

connector: account.connector?.name,

458

459

// Chain info

460

chainId,

461

chainName: chains.find(c => c.id === chainId)?.name,

462

availableChains: chains.map(c => ({ id: c.id, name: c.name })),

463

464

// Connection info

465

connectionCount: connections.length,

466

availableConnectors: connectors.map(c => ({ id: c.id, name: c.name })),

467

}

468

}

469

470

// Set up watchers

471

const unsubscribeAccount = watchAccount(config, {

472

onChange: (account) => {

473

console.log('Account changed:', {

474

address: account.address,

475

isConnected: account.isConnected,

476

status: account.status,

477

})

478

},

479

})

480

481

const unsubscribeChain = watchChainId(config, {

482

onChange: (chainId) => {

483

const chain = config.chains.find(c => c.id === chainId)

484

console.log('Chain changed:', { chainId, name: chain?.name })

485

},

486

})

487

488

// Use in your app

489

const currentState = getCurrentState()

490

console.log('Current app state:', currentState)

491

```