This repository has been archived by the owner on Feb 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathindex.js
93 lines (86 loc) · 3.18 KB
/
index.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const debug = require("debug")("provider");
const Web3 = require("web3");
const { createInterfaceAdapter } = require("@truffle/interface-adapter");
const wrapper = require("./wrapper");
const DEFAULT_NETWORK_CHECK_TIMEOUT = 5000;
module.exports = {
wrap: function (provider, options) {
return wrapper.wrap(provider, options);
},
create: function (options) {
const provider = this.getProvider(options);
return this.wrap(provider, options);
},
getProvider: function (options) {
let provider;
if (options.provider && typeof options.provider === "function") {
provider = options.provider();
} else if (options.provider) {
provider = options.provider;
} else if (options.websockets || /^wss?:\/\//.test(options.url)) {
provider = new Web3.providers.WebsocketProvider(
options.url || "ws://" + options.host + ":" + options.port
);
} else {
provider = new Web3.providers.HttpProvider(
options.url || `http://${options.host}:${options.port}`,
{ keepAlive: false }
);
}
return provider;
},
testConnection: function (options) {
let networkCheckTimeout, networkType;
const { networks, network } = options;
if (networks && networks[network]) {
networkCheckTimeout =
networks[network].networkCheckTimeout || DEFAULT_NETWORK_CHECK_TIMEOUT;
networkType = networks[network].type;
} else {
networkCheckTimeout = DEFAULT_NETWORK_CHECK_TIMEOUT;
}
const provider = this.getProvider(options);
const { host } = provider;
const interfaceAdapter = createInterfaceAdapter({ provider, networkType });
return new Promise((resolve, reject) => {
const noResponseFromNetworkCall = setTimeout(() => {
let errorMessage =
"There was a timeout while attempting to connect to the network at " +
host +
".\n Check to see that your provider is valid." +
"\n If you have a slow internet connection, try configuring a longer " +
"timeout in your Truffle config. Use the " +
"networks[networkName].networkCheckTimeout property to do this.";
if (network === "dashboard") {
errorMessage +=
"\n Also make sure that your Truffle Dashboard browser " +
"tab is open and connected to MetaMask.";
}
throw new Error(errorMessage);
}, networkCheckTimeout);
let networkCheckDelay = 1;
(function networkCheck() {
setTimeout(async () => {
try {
await interfaceAdapter.getBlockNumber();
clearTimeout(noResponseFromNetworkCall);
clearTimeout(networkCheck);
return resolve(true);
} catch (error) {
console.log(
"> Something went wrong while attempting to connect to the " +
"network at " +
host +
". Check your network configuration."
);
clearTimeout(noResponseFromNetworkCall);
clearTimeout(networkCheck);
return reject(error);
}
networkCheckDelay *= 2;
networkCheck();
}, networkCheckDelay);
})();
});
}
};