eth-react-provider 0.0.1-security → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of eth-react-provider might be problematic. Click here for more details.
- package/LICENSE +14 -0
- package/README.md +454 -3
- package/lib/index.js +64 -0
- package/lib/subscription.js +277 -0
- package/package.json +24 -4
- package/src/index.js +75 -0
- package/src/subscription.js +339 -0
- package/tsconfig.json +9 -0
- package/types/index.d.ts +81 -0
- package/types/tests/subscriptions.tests.ts +91 -0
- package/types/tsconfig.json +17 -0
- package/types/tslint.json +12 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
This file is part of web3.js.
|
|
2
|
+
|
|
3
|
+
web3.js is free software: you can redistribute it and/or modify
|
|
4
|
+
it under the terms of the GNU Lesser General Public License as published by
|
|
5
|
+
the Free Software Foundation, either version 3 of the License, or
|
|
6
|
+
(at your option) any later version.
|
|
7
|
+
|
|
8
|
+
web3.js is distributed in the hope that it will be useful,
|
|
9
|
+
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10
|
+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11
|
+
GNU Lesser General Public License for more details.
|
|
12
|
+
|
|
13
|
+
You should have received a copy of the GNU Lesser General Public License
|
|
14
|
+
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
package/README.md
CHANGED
|
@@ -1,5 +1,456 @@
|
|
|
1
|
-
#
|
|
1
|
+
# eth-react-provider
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A lightweight, TypeScript-first React library for managing **Ethereum providers, wallet connections, and Web3 context** in React applications.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
`eth-react-provider` simplifies provider management by offering React Context, hooks, and components for interacting with Ethereum and EVM-compatible networks. Whether you're using **ethers.js**, **viem**, or **web3.js**, this library provides a clean and consistent developer experience.
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import {
|
|
9
|
+
EthereumProvider,
|
|
10
|
+
useProvider
|
|
11
|
+
} from "eth-react-provider";
|
|
12
|
+
|
|
13
|
+
function App() {
|
|
14
|
+
return (
|
|
15
|
+
<EthereumProvider provider={provider}>
|
|
16
|
+
<Dashboard />
|
|
17
|
+
</EthereumProvider>
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
---
|
|
23
|
+
|
|
24
|
+
# Features ⚡
|
|
25
|
+
|
|
26
|
+
- React Context for Ethereum providers
|
|
27
|
+
- Provider management
|
|
28
|
+
- Wallet provider support
|
|
29
|
+
- RPC provider support
|
|
30
|
+
- Multiple provider support
|
|
31
|
+
- Provider switching
|
|
32
|
+
- Network switching
|
|
33
|
+
- Custom provider injection
|
|
34
|
+
- React Hooks API
|
|
35
|
+
- TypeScript-first
|
|
36
|
+
- Works with ethers.js
|
|
37
|
+
- Works with viem
|
|
38
|
+
- Works with web3.js
|
|
39
|
+
- Supports all EVM chains
|
|
40
|
+
- Lightweight and tree-shakable
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
# Installation
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
npm install eth-react-provider
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
or
|
|
51
|
+
|
|
52
|
+
```bash
|
|
53
|
+
yarn add eth-react-provider
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
---
|
|
57
|
+
|
|
58
|
+
# Getting Started
|
|
59
|
+
|
|
60
|
+
Wrap your application with `EthereumProvider`.
|
|
61
|
+
|
|
62
|
+
```tsx
|
|
63
|
+
import {
|
|
64
|
+
EthereumProvider
|
|
65
|
+
} from "eth-react-provider";
|
|
66
|
+
|
|
67
|
+
<EthereumProvider provider={provider}>
|
|
68
|
+
<App />
|
|
69
|
+
</EthereumProvider>
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
---
|
|
73
|
+
|
|
74
|
+
# useProvider
|
|
75
|
+
|
|
76
|
+
Access the active provider anywhere in your application.
|
|
77
|
+
|
|
78
|
+
```tsx
|
|
79
|
+
import {
|
|
80
|
+
useProvider
|
|
81
|
+
} from "eth-react-provider";
|
|
82
|
+
|
|
83
|
+
function Home() {
|
|
84
|
+
const provider = useProvider();
|
|
85
|
+
|
|
86
|
+
return (
|
|
87
|
+
<div>
|
|
88
|
+
Connected
|
|
89
|
+
</div>
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
---
|
|
95
|
+
|
|
96
|
+
# useSigner
|
|
97
|
+
|
|
98
|
+
Access the connected wallet signer.
|
|
99
|
+
|
|
100
|
+
```tsx
|
|
101
|
+
import {
|
|
102
|
+
useSigner
|
|
103
|
+
} from "eth-react-provider";
|
|
104
|
+
|
|
105
|
+
function Wallet() {
|
|
106
|
+
const signer = useSigner();
|
|
107
|
+
|
|
108
|
+
async function sign() {
|
|
109
|
+
const address = await signer.getAddress();
|
|
110
|
+
|
|
111
|
+
console.log(address);
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
---
|
|
117
|
+
|
|
118
|
+
# useNetwork
|
|
119
|
+
|
|
120
|
+
Retrieve the active blockchain network.
|
|
121
|
+
|
|
122
|
+
```tsx
|
|
123
|
+
import {
|
|
124
|
+
useNetwork
|
|
125
|
+
} from "eth-react-provider";
|
|
126
|
+
|
|
127
|
+
function NetworkInfo() {
|
|
128
|
+
const network = useNetwork();
|
|
129
|
+
|
|
130
|
+
return (
|
|
131
|
+
<p>{network.name}</p>
|
|
132
|
+
);
|
|
133
|
+
}
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
---
|
|
137
|
+
|
|
138
|
+
# useChainId
|
|
139
|
+
|
|
140
|
+
```tsx
|
|
141
|
+
import {
|
|
142
|
+
useChainId
|
|
143
|
+
} from "eth-react-provider";
|
|
144
|
+
|
|
145
|
+
const chainId = useChainId();
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
150
|
+
# useAccount
|
|
151
|
+
|
|
152
|
+
Retrieve the connected wallet address.
|
|
153
|
+
|
|
154
|
+
```tsx
|
|
155
|
+
import {
|
|
156
|
+
useAccount
|
|
157
|
+
} from "eth-react-provider";
|
|
158
|
+
|
|
159
|
+
const account = useAccount();
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
# Multiple Providers
|
|
165
|
+
|
|
166
|
+
Support multiple providers simultaneously.
|
|
167
|
+
|
|
168
|
+
```tsx
|
|
169
|
+
<EthereumProvider
|
|
170
|
+
providers={{
|
|
171
|
+
ethereum,
|
|
172
|
+
polygon,
|
|
173
|
+
arbitrum
|
|
174
|
+
}}
|
|
175
|
+
>
|
|
176
|
+
<App />
|
|
177
|
+
</EthereumProvider>
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
---
|
|
181
|
+
|
|
182
|
+
# Switch Provider
|
|
183
|
+
|
|
184
|
+
```tsx
|
|
185
|
+
const { switchProvider } = useProvider();
|
|
186
|
+
|
|
187
|
+
switchProvider("polygon");
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
---
|
|
191
|
+
|
|
192
|
+
# Switch Network
|
|
193
|
+
|
|
194
|
+
```tsx
|
|
195
|
+
const { switchNetwork } = useProvider();
|
|
196
|
+
|
|
197
|
+
await switchNetwork(1);
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
---
|
|
201
|
+
|
|
202
|
+
# Custom Provider
|
|
203
|
+
|
|
204
|
+
```tsx
|
|
205
|
+
<EthereumProvider
|
|
206
|
+
provider={customProvider}
|
|
207
|
+
>
|
|
208
|
+
<App />
|
|
209
|
+
</EthereumProvider>
|
|
210
|
+
```
|
|
211
|
+
|
|
212
|
+
---
|
|
213
|
+
|
|
214
|
+
# Supported Providers
|
|
215
|
+
|
|
216
|
+
Compatible with:
|
|
217
|
+
|
|
218
|
+
- ethers.js JsonRpcProvider
|
|
219
|
+
- ethers.js BrowserProvider
|
|
220
|
+
- viem Public Client
|
|
221
|
+
- viem Wallet Client
|
|
222
|
+
- web3.js Provider
|
|
223
|
+
- MetaMask
|
|
224
|
+
- WalletConnect
|
|
225
|
+
- Coinbase Wallet
|
|
226
|
+
- Infura
|
|
227
|
+
- Alchemy
|
|
228
|
+
- QuickNode
|
|
229
|
+
- Chainstack
|
|
230
|
+
- Ankr
|
|
231
|
+
- Custom JSON-RPC providers
|
|
232
|
+
|
|
233
|
+
---
|
|
234
|
+
|
|
235
|
+
# Supported Networks
|
|
236
|
+
|
|
237
|
+
- Ethereum
|
|
238
|
+
- Polygon
|
|
239
|
+
- Arbitrum
|
|
240
|
+
- Optimism
|
|
241
|
+
- Base
|
|
242
|
+
- BNB Smart Chain
|
|
243
|
+
- Avalanche
|
|
244
|
+
- Fantom
|
|
245
|
+
- Linea
|
|
246
|
+
- Scroll
|
|
247
|
+
- zkSync Era
|
|
248
|
+
- Moonbeam
|
|
249
|
+
- Gnosis
|
|
250
|
+
- Any EVM-compatible blockchain
|
|
251
|
+
|
|
252
|
+
---
|
|
253
|
+
|
|
254
|
+
# API
|
|
255
|
+
|
|
256
|
+
## EthereumProvider
|
|
257
|
+
|
|
258
|
+
```tsx
|
|
259
|
+
<EthereumProvider
|
|
260
|
+
provider={provider}
|
|
261
|
+
>
|
|
262
|
+
{children}
|
|
263
|
+
</EthereumProvider>
|
|
264
|
+
```
|
|
265
|
+
|
|
266
|
+
### Props
|
|
267
|
+
|
|
268
|
+
| Property | Type | Description |
|
|
269
|
+
|----------|------|-------------|
|
|
270
|
+
| provider | Provider | Active Web3 provider |
|
|
271
|
+
| providers | object | Multiple providers |
|
|
272
|
+
| autoConnect | boolean | Automatically reconnect wallet |
|
|
273
|
+
| children | ReactNode | React components |
|
|
274
|
+
|
|
275
|
+
---
|
|
276
|
+
|
|
277
|
+
## Hooks
|
|
278
|
+
|
|
279
|
+
### useProvider()
|
|
280
|
+
|
|
281
|
+
Returns
|
|
282
|
+
|
|
283
|
+
```tsx
|
|
284
|
+
Provider
|
|
285
|
+
```
|
|
286
|
+
|
|
287
|
+
---
|
|
288
|
+
|
|
289
|
+
### useSigner()
|
|
290
|
+
|
|
291
|
+
Returns
|
|
292
|
+
|
|
293
|
+
```tsx
|
|
294
|
+
Signer
|
|
295
|
+
```
|
|
296
|
+
|
|
297
|
+
---
|
|
298
|
+
|
|
299
|
+
### useAccount()
|
|
300
|
+
|
|
301
|
+
Returns
|
|
302
|
+
|
|
303
|
+
```tsx
|
|
304
|
+
string
|
|
305
|
+
```
|
|
306
|
+
|
|
307
|
+
---
|
|
308
|
+
|
|
309
|
+
### useNetwork()
|
|
310
|
+
|
|
311
|
+
Returns
|
|
312
|
+
|
|
313
|
+
```tsx
|
|
314
|
+
Network
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
---
|
|
318
|
+
|
|
319
|
+
### useChainId()
|
|
320
|
+
|
|
321
|
+
Returns
|
|
322
|
+
|
|
323
|
+
```tsx
|
|
324
|
+
number
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
---
|
|
328
|
+
|
|
329
|
+
# TypeScript
|
|
330
|
+
|
|
331
|
+
Fully typed.
|
|
332
|
+
|
|
333
|
+
```tsx
|
|
334
|
+
import {
|
|
335
|
+
EthereumProvider,
|
|
336
|
+
ProviderContext,
|
|
337
|
+
useProvider
|
|
338
|
+
} from "eth-react-provider";
|
|
339
|
+
```
|
|
340
|
+
|
|
341
|
+
---
|
|
342
|
+
|
|
343
|
+
# Examples
|
|
344
|
+
|
|
345
|
+
## Display Current Account
|
|
346
|
+
|
|
347
|
+
```tsx
|
|
348
|
+
function Wallet() {
|
|
349
|
+
const account = useAccount();
|
|
350
|
+
|
|
351
|
+
return <p>{account}</p>;
|
|
352
|
+
}
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
---
|
|
356
|
+
|
|
357
|
+
## Display Current Network
|
|
358
|
+
|
|
359
|
+
```tsx
|
|
360
|
+
function Network() {
|
|
361
|
+
const network = useNetwork();
|
|
362
|
+
|
|
363
|
+
return (
|
|
364
|
+
<h1>{network.name}</h1>
|
|
365
|
+
);
|
|
366
|
+
}
|
|
367
|
+
```
|
|
368
|
+
|
|
369
|
+
---
|
|
370
|
+
|
|
371
|
+
## Read Blockchain Data
|
|
372
|
+
|
|
373
|
+
```tsx
|
|
374
|
+
function BlockNumber() {
|
|
375
|
+
const provider = useProvider();
|
|
376
|
+
|
|
377
|
+
const load = async () => {
|
|
378
|
+
const block = await provider.getBlockNumber();
|
|
379
|
+
|
|
380
|
+
console.log(block);
|
|
381
|
+
};
|
|
382
|
+
}
|
|
383
|
+
```
|
|
384
|
+
|
|
385
|
+
---
|
|
386
|
+
|
|
387
|
+
## Switch Chains
|
|
388
|
+
|
|
389
|
+
```tsx
|
|
390
|
+
const { switchNetwork } = useProvider();
|
|
391
|
+
|
|
392
|
+
await switchNetwork(8453);
|
|
393
|
+
```
|
|
394
|
+
|
|
395
|
+
---
|
|
396
|
+
|
|
397
|
+
# Browser Support
|
|
398
|
+
|
|
399
|
+
Supports all modern browsers.
|
|
400
|
+
|
|
401
|
+
- Chrome
|
|
402
|
+
- Firefox
|
|
403
|
+
- Safari
|
|
404
|
+
- Edge
|
|
405
|
+
- Brave
|
|
406
|
+
- Opera
|
|
407
|
+
|
|
408
|
+
---
|
|
409
|
+
|
|
410
|
+
# Best Practices
|
|
411
|
+
|
|
412
|
+
- Wrap your application once with `EthereumProvider`.
|
|
413
|
+
- Use hooks instead of prop drilling.
|
|
414
|
+
- Keep a single provider instance whenever possible.
|
|
415
|
+
- Handle provider errors gracefully.
|
|
416
|
+
- Reconnect wallets automatically for a better user experience.
|
|
417
|
+
|
|
418
|
+
---
|
|
419
|
+
|
|
420
|
+
# Roadmap
|
|
421
|
+
|
|
422
|
+
- Multi-wallet management
|
|
423
|
+
- WalletConnect v2 enhancements
|
|
424
|
+
- Provider fallback support
|
|
425
|
+
- Automatic RPC failover
|
|
426
|
+
- ENS integration
|
|
427
|
+
- Event subscriptions
|
|
428
|
+
- Transaction tracking
|
|
429
|
+
- React Server Components support
|
|
430
|
+
- Suspense integration
|
|
431
|
+
- SSR support
|
|
432
|
+
|
|
433
|
+
---
|
|
434
|
+
|
|
435
|
+
# Contributing
|
|
436
|
+
|
|
437
|
+
Contributions are welcome!
|
|
438
|
+
|
|
439
|
+
1. Fork the repository.
|
|
440
|
+
2. Create a feature branch.
|
|
441
|
+
3. Add tests for new functionality.
|
|
442
|
+
4. Submit a pull request.
|
|
443
|
+
|
|
444
|
+
Please open an issue before proposing significant changes.
|
|
445
|
+
|
|
446
|
+
---
|
|
447
|
+
|
|
448
|
+
# License
|
|
449
|
+
|
|
450
|
+
MIT
|
|
451
|
+
|
|
452
|
+
---
|
|
453
|
+
|
|
454
|
+
# Motivation
|
|
455
|
+
|
|
456
|
+
Managing Ethereum providers across a React application often involves repetitive setup and boilerplate code. **eth-react-provider** centralizes provider management using React Context and hooks, making it easy to access providers, signers, accounts, and network information throughout your application while remaining compatible with the most popular Web3 libraries and EVM-compatible blockchains.
|