This repository has been archived by the owner on Jun 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
web3.js
117 lines (98 loc) · 3.72 KB
/
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
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
const net = require('net')
const assert = require('assert')
const Web3 = require('web3')
const ganache = require('ganache-core')
const seedGanacheAccounts = require('./seed-ganache-accounts')
const { has, get, isFunction, isInteger, isString } = require('lodash')
const makeWeb3Require = require('./web3-require')
const web3Deploy = require('./web3-deploy')
const web3At = require('./web3-at')
const DEFAULT_PROVIDER = 'ganache'
const DEFAULT_ACCOUNTS = 1000
const DEFAULT_GAS_LIMIT = 50000000
const DEFAULT_CHAIN_ID = 0x11
const DEFAULT_LOGGER = {
log() {}
}
const providers = {
ganache({ accounts: n = DEFAULT_ACCOUNTS, chainId = DEFAULT_CHAIN_ID, gasLimit = DEFAULT_GAS_LIMIT, logger = DEFAULT_LOGGER, blocktime } = {}) {
const accounts = seedGanacheAccounts(n)
const provider = ganache.provider({
logger,
unlocked_accounts: accounts.map(_1 => _1.address),
accounts,
network_id: chainId,
gasLimit,
blocktime
})
// Apparently there are a lot of listeners.
if (provider.getMaxListeners() === 10) {
provider.setMaxListeners(35)
}
return { provider, accounts, close: null }
}
}
/**
* @param {object} options
* @param {number} options.accounts Number of accounts to generate.
* @param {string} options.root Contract's root for `web3.require` so `solc` compiler knows where to look for `.sol` files.
* @param {string} options.evmVersion EVM version for solc, ie. `spuriousDragon`.
* @param {string} options.allowPaths Set to '../,' to allow `import "../Foo.sol"` etc.
* @param {solc} options.solc Alternative to root, pass `solc` compiler directly.
*
* @return {object} { web3: Web3, accounts: array, provider }
*/
function makeWeb3(options = {}) {
// Make sure we don't get ganache like keys.
if (has(options, 'networkId') || has(options, 'network_id')) {
throw new TypeError('Please use chainId instead of networkId or network_id.')
}
// Make sure chain id is sane and in safe range.
if (has(options, 'chainId')) {
if (!isInteger(options.chainId) || (options.chainId < 0 || options.chainId > 109)) {
throw new TypeError(`Expected chainId to be integer in 0 to 109 range, got ${options.chainId}.`)
}
}
let provider, accounts, close
const optionsProvider = get(options, 'provider', DEFAULT_PROVIDER)
// Pass provider as is if it's not a string.
if (!isString(optionsProvider)) {
provider = optionsProvider
} else if (optionsProvider === DEFAULT_PROVIDER) {
const result = providers[optionsProvider](options)
provider = result.provider
accounts = result.accounts
close = result.close
} else if (optionsProvider.startsWith('ipc://')) {
provider = new Web3.providers.IpcProvider(optionsProvider, net)
} else {
provider = optionsProvider
}
const web3 = new Web3(provider)
// replace provider string with the auto-detected provider
if (isString(provider)) {
provider = web3.currentProvider
}
// Decorate with `require`.
const root = get(options, 'root')
const solc = get(options, 'solc')
const evmVersion = get(options, 'evmVersion')
const allowPaths = get(options, 'allowPaths')
assert(!web3.require, 'Can\'t overwrite web3.require.')
web3.require = makeWeb3Require({ root, solc, evmVersion, allowPaths })
// Decorate with `deploy`.
assert(!web3.deploy, 'Can\'t overwrite web3.deploy.')
web3.deploy = web3Deploy
// Decorate with `at`.
assert(!web3.at, 'Can\'t overwrite web3.at.')
web3.at = web3At
// Decorate with `close` to close provider - if it's one of those that are hanging.
assert(!web3.close, 'Can\'t overwrite close.')
web3.close = function () {
if (isFunction(close)) {
return close.call(web3)
}
}
return { web3, accounts, provider }
}
module.exports = makeWeb3