-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb3.js
30 lines (22 loc) · 925 Bytes
/
web3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// *Udemy* way to do it (even though they put it in a web3.js script)
// We want to inject a certain version of Web3
// To not take the one from Metamask, since we don't know the version
//called from pages/index.js from NextJS
// could also be called by App.js from React, but doing it differently there
import Web3 from 'web3';
let web3;
if (typeof window !== "undefined" && typeof window.ethereum !== "undefined") {
// We are in the browser and metamask is running.
window.ethereum.request({ method: "eth_requestAccounts" });
web3 = new Web3(window.ethereum);
console.log("Using Metamask")
} else {
// We are on the server *OR* the user is not running metamask
//Create our own instance of web3
const provider = new Web3.providers.HttpProvider(
'https://rinkeby.infura.io/v3/327486c4905348609d2424aec513b322'
);
web3 = new Web3(provider);
console.log("Using Infura")
}
export default web3;