-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathweb3-service-loader.js
45 lines (43 loc) · 2.31 KB
/
web3-service-loader.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/**
* To anyone using this class.
* Web3ServiceLoader is a singleton factory
* web3js wants to live in the global space and the exports for it tend to come and go.
* We ignore the global web3 because it could be put there by anything including MetaMask which frequently uses a very outdated version of web3
* Most users of this service are building wallets and tools to interact with their own dapps and it is important the latest stable features are made available to them
*
* What we have here is...
* Web3ServiceLoader which is a static class with a single static method
* The single static method is getInstance and it accepts a provider as an argument.
* We highly recommend infura.io for this
*
* When you call Web3ServiceLoader.getInstance(provider) for the very first time, it will load web3.min.js
* It will then instantiate a new Web3 instance with the provider you pass in and return the new instance.
*
* From there, any subsequent calls to getInstance() will return the same web3js instance
*
* In order to prevent chaos, you should only use the service loader in a single place in your app
*
* For telepathy this should be in your app root and should be called only once by the onReady method.
* If you import this in multiple places you could get unpredictable behavior, however we try to guard against it by attaching ourselves to window
* Nevertheless, telepathy is blindingly fast and multi-threaded,
* therefore it's possible to get multiple web3 instances all going at once if you are importing this script all over the place
*/
export class Web3ServiceLoader {
static async getInstance(provider){
console.log("Web3Service entering getInstance with ",provider);
if(!window.Web3Service){
window.Web3Service = Web3ServiceLoader;
}
if(!window.Web3Service.loading){
window.Web3Service.loading = true;
if(!Web3ServiceLoader.instance){
let data = await (await fetch("https://telepathic-elements.github.io/web3-service-loader/web3.min.js")).text();
await eval(data);
Web3ServiceLoader.instance = await new Web3(provider);
}
window.Web3Service.loading = false;
}
console.log("Web3Service leaving getInstance");
return Web3ServiceLoader.instance;
}
};