This repository has been archived by the owner on Feb 4, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
/
index.js
69 lines (58 loc) · 1.89 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
const Web3 = require('web3')
const privateKeyToAddress = require('ethereum-private-key-to-address')
const PrivateKeyProvider = require('truffle-privatekey-provider')
const chalk = require('chalk')
async function send (config) {
const privateKey = (config.from || '').replace(/^0x/, '')
const to = config.to
let amount = config.amount || 0
let value = config.value || 0
const network = (config.network || 'mainnet').toLowerCase()
let providerUri = `https://${network}.infura.io/`
if (/^(http|ws)/.test(network)) {
providerUri = network
} else if (network === 'local' || network === 'development') {
providerUri = 'http://localhost:8545'
}
const provider = new PrivateKeyProvider(privateKey, providerUri)
const web3 = new Web3(provider)
if (value && amount) {
console.warn(chalk.yellow('warning: both "value" and "amount" set. Using "value" instead.'))
}
if (value) {
amount = web3.utils.fromWei(value, 'ether')
} else {
value = web3.utils.toWei(amount, 'ether')
}
const from = privateKeyToAddress(privateKey)
const data = config.data || '0x'
const gas = config.gas || 21000
const gasPrice = config.gasPrice || await web3.eth.getGasPrice()
if (config.log) {
console.log(chalk.yellow('sending transaction:'))
console.log('\nnetwork: %s', network)
console.log('from: %s', from)
console.log('to: %s', to)
console.log('amount: %s ETH (%s wei)', amount, value)
console.log('gas: %s', gas)
console.log('gasPrice: %s gwei', web3.utils.fromWei(gasPrice, 'gwei'))
}
const txHash = await new Promise((resolve, reject) => {
web3.eth.sendTransaction({
from,
to,
value,
data,
gas,
gasPrice
}, (err, txHash) => {
if (err) return reject(err)
resolve(txHash)
})
})
if (config.log) {
console.log('\ntx hash: %s', txHash)
}
return txHash
}
module.exports = send