-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
52 lines (43 loc) · 1.27 KB
/
utils.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
'use strict'
const bip39 = require('bip39')
const hdkey = require('hdkey')
const Web3 = require('web3')
const GAS_OVERESTIMATION = 1.1
function mnemonicToKeyPair (mnemonic, derivationPath, password) {
const seed = bip39.mnemonicToSeedSync(mnemonic, password)
const { privateKey, publicKey } = hdkey
.fromMasterSeed(seed)
.derive(derivationPath)
return { privateKey, publicKey }
}
function createWeb3 (url, mnemonic) {
const web3 = new Web3(url)
if (mnemonic) {
const derivationPath = "m/44'/60'/0'/0/0"
const privateKey = mnemonicToKeyPair(mnemonic, derivationPath).privateKey
web3.eth.accounts.wallet.create(0).add(`0x${privateKey.toString('hex')}`)
}
return web3
}
function createGetContract (web3, contracts) {
return function (contractName, contractAddr) {
return web3.eth.getChainId().then(function (chain) {
const { abi, address } = contracts[chain].find(
c => c.name === contractName
)
return new web3.eth.Contract(abi, address || contractAddr)
})
}
}
function estimateGasAndSend (tx, options) {
return tx
.estimateGas(options)
.then(gas =>
tx.send({ ...options, gas: Math.ceil(gas * GAS_OVERESTIMATION) })
)
}
module.exports = {
createGetContract,
createWeb3,
estimateGasAndSend
}