Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 8c5ea34

Browse files
Providers Tutorial (#6095)
* add web3 providers tutorial * few corrections at Web3 Providers guide * update IpcProvider imports
1 parent f2abd6a commit 8c5ea34

File tree

18 files changed

+640
-27
lines changed

18 files changed

+640
-27
lines changed

docs/docs/guides/web3_migration_guide/providers_migration_guide.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ You can use it by installing `web3-providers-ipc` and creating a new instance. S
245245
you can pass it on to the Web3 instance.
246246

247247
```ts
248-
import IpcProvider from 'web3-providers-ipc';
248+
import { IpcProvider } from 'web3-providers-ipc';
249249

250250
const ipcProvider = new IpcProvider('/Users/myuser/Library/Ethereum/geth.ipc');
251251
```

docs/docs/guides/web3_providers_guide/index.md

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ The key rule for setting provider is as follows:
6161
### Local Geth Node
6262

6363
```ts
64-
const Web3 = require('web3');
64+
const { Web3 } = require('web3');
6565
const web3 = new Web3('http://localhost:8545');
6666
// or
6767
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
@@ -86,7 +86,7 @@ const web3 = new Web3(
8686

8787
```ts
8888
// Using a remote node provider, like Alchemy (https://www.alchemyapi.io/supernode), is simple.
89-
const Web3 = require('web3');
89+
const { Web3 } = require('web3');
9090
const web3 = new Web3('https://eth-mainnet.alchemyapi.io/v2/your-api-key');
9191
```
9292

@@ -96,17 +96,33 @@ As stated above, the injected provider should be in compliance with [EIP-1193](h
9696

9797
The web3.js 4.x Provider specifications are defined in [web3 base provider](https://github.com/ChainSafe/web3.js/blob/4.x/packages/web3-types/src/web3_base_provider.ts) for Injected Providers.
9898

99-
```ts
100-
const Web3 = require('web3');
101-
// Using an EIP1193 provider like MetaMask can be injected
102-
103-
if (window.ethereum) {
104-
// Check if ethereum object exists
105-
await window.ethereum.request();
106-
window.web3 = new Web3(window.ethereum); // inject provider
107-
}
99+
```html
100+
<script src="https://cdn.jsdelivr.net/npm/web3@4.0.1-rc.1/dist/web3.min.js"></script>
101+
<script>
102+
window.addEventListener('load', function () {
103+
// Check if web3 is available
104+
if (typeof window.ethereum !== 'undefined') {
105+
// Use the browser injected Ethereum provider
106+
web3 = new Web3(window.ethereum);
107+
// Request access to the user's MetaMask account
108+
window.ethereum.enable();
109+
// Get the user's accounts
110+
web3.eth.getAccounts().then(function (accounts) {
111+
// Show the first account
112+
document.getElementById('log').innerHTML =
113+
'Connected with MetaMask account: ' + accounts[0];
114+
});
115+
} else {
116+
// If web3 is not available, give instructions to install MetaMask
117+
document.getElementById('log').innerHTML =
118+
'Please install MetaMask to connect with the Ethereum network';
119+
}
120+
});
121+
</script>
108122
```
109123

124+
Note that the above code should be hosted in a web server (that could be a simple local web server), because many browser does not support this feature for static files located on your machine.
125+
110126
### Provider Options
111127

112128
There are differences in the objects that could be passed in the Provider constructors.

0 commit comments

Comments
 (0)