-
Notifications
You must be signed in to change notification settings - Fork 0
/
web3Setup.js
98 lines (90 loc) · 2.97 KB
/
web3Setup.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
94
95
96
97
98
const Web3 = require("web3");
const path = require("path");
const fs = require("fs");
//configure options for web3 Websocket
const options = {
timeout: 120000, // ms
// Useful if requests result are large
clientConfig: {
keepalive: true,
keepaliveInterval: 60000, // ms
maxReceivedFrameSize: 100000000, // bytes - default: 1MiB
maxReceivedMessageSize: 100000000 // bytes - default: 8MiB
},
// Enable auto reconnection
reconnect: {
auto: true,
delay: 5000, // ms
maxAttempts: 5,
onTimeout: true
}
};
// Instantiate providers for WSS and HTTPS protocols
const httpsProvider = new Web3.providers.HttpProvider(process.env.PROVIDER, options);
/**
* Function that gets the inventory contract ABI from the locally stored version or remote blob stored, based on environment
* @param type: A string defining the contract json to use
* @returns contract.abi: The abi to use for calling the contract
*/
const getABI = async (type) => {
let contract;
try {
contract = JSON.parse(
fs.readFileSync(
path.join(__dirname, `../quorum/build/contracts/${type}.json`),
"utf8"
)
);
return contract.abi;
} catch (e) {
throw `Error in getABI: ${e}`;
}
};
/**
* Function that gets the contract address from the locally stored version or remote blob stored, based on environment
* @param type: A string defining the contract build to retrieve
* @returns contract.abi: The abi to use for calling the contract
*/
const getContractAddress = async (type) => {
let contractAddress;
try {
contractAddress = fs.readFileSync(
path.join(__dirname, `../quorum/contractAddresses/${type}DeployedContractAddress.txt`),
"utf8"
);
return contractAddress;
} catch (e) {
throw `Error in getABI: ${e}`;
}
};
/**
* Function that returns the instantiated web3 websocket or https instance based on the protocol provided
* If protocol is not passed it will default to https
* @param protocol: wss or empty
* @returns web3: the instantiated web3 websocket or https instance
*/
const getWeb3 = (protocol) => {
switch (protocol) {
case "wss":
return new Web3(wssProvider);
default:
return new Web3(httpsProvider);
}
};
/**
* Function that gets contract object by taking in the compiled contract json name
* @param type: A string defining the contract json to use
* @returns myContract: web3 object for connecting the the contract
*/
const getContract = async (type, protocol) => {
const abi = await getABI(type);
const contractAddress = await getContractAddress(type);
const web3 = getWeb3(protocol);
const myContract = new web3.eth.Contract(abi, contractAddress);
web3.eth.handleRevert = true; // Allow readable error messages to come from blockchain
return myContract;
};
module.exports = {
getContract,
getWeb3
};