-
Notifications
You must be signed in to change notification settings - Fork 3
/
galtproject-gpc.js
118 lines (96 loc) · 3.17 KB
/
galtproject-gpc.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
const homedir = require('os').homedir;
const path = require('path');
const fs = require('fs');
const YAML = require('yamljs');
const assert = require('assert');
const web3 = require('web3');
const _ = require('lodash');
const HDWalletProvider = require('@truffle/hdwallet-provider');
const wsProviders = {};
const httpProviders = {};
const DEFAULT_MNEMONIC = 'explain tackle mirror kit van hammer degree position ginger unfair soup bonus';
const defaultHTTPRPC = (network) => `https://http-rpc.${network}.galtproject.io`;
const defaultWSRPC = (network) => `wss://wss-rpc.${network}.galtproject.io`;
const configFilePath = path.join(homedir(), `.gpc/keys.yaml`);
const mnemonic = () => {
try {
return require(configFilePath).mnemonic;
} catch (e) {
return DEFAULT_MNEMONIC;
}
};
const settingsForNetwork = (network) => {
if (!fs.existsSync(configFilePath)) {
throw new Error(`GPC: Please, create a config file at "${configFilePath}"`);
}
const config = YAML.load(configFilePath);
return config[network];
};
// Lazily loaded provider
const httpProviderForNetwork = (network) => () => {
let { rpc, keys } = settingsForNetwork(network);
rpc = rpc.http || defaultHTTPRPC(network);
assert(keys.length > 0, `GPC: No private key for ${network} specified in ~/.gpc/keys.yaml`);
// TODO: improve
return new HDWalletProvider(keys, rpc);
};
const wsProviderForNetwork = (network) => () => {
network = network || process.env.NETWORK || 'local';
if (!wsProviders[network]) {
let { rpc, key, keys } = settingsForNetwork(network);
rpc = rpc.ws || defaultWSRPC(network);
if (!keys || keys.length === 0) {
assert(key.length > 0, `GPC: No private key for ${network} specified in ~/.gpc/keys.yaml`);
keys = [key];
}
const provider = new web3.providers.WebsocketProvider(rpc);
const hdProvider = new HDWalletProvider(keys, provider);
console.log(`GPC: A new WS provider created for "${network}" network`);
console.log('GPC: RPC endpoint:', rpc);
console.log('GPC: Unlocked accounts:', hdProvider.getAddresses());
console.log('');
wsProviders[network] = hdProvider;
}
return wsProviders[network];
};
/**
* Get OZ loader
*
* @param options ether config object or a network name string
* @returns {{defaultSender: *, loader: any}}
*/
function getLoader(options) {
let network;
if (typeof options === 'string') {
network = options;
} else {
network = options.network;
}
const { setupLoader } = require('@openzeppelin/contract-loader');
let provider = options.provider || wsProviderForNetwork(network)();
const defaultSender = provider.addresses[0];
if (options.gsnSupport) {
const { GSNProvider } = require('@openzeppelin/gsn-provider');
provider = new GSNProvider(provider, _.extend({ debug: false, useGSN: false }, options.gsnOptions || {}));
}
return {
loader: setupLoader(
_.extend(
{
provider,
useGSN: true,
defaultGas: 8000000,
defaultGasPrice: 10 ** 9,
},
options.loaderOptions || {}
)
),
provider,
defaultSender,
};
}
module.exports = {
getLoader,
wsProviderForNetwork,
httpProviderForNetwork,
};