-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathutils.js
More file actions
38 lines (30 loc) · 1.24 KB
/
utils.js
File metadata and controls
38 lines (30 loc) · 1.24 KB
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
const Tx = require("ethereumjs-tx");
async function signAndSend({web3, payloadData, from, pk, gasPrice, gas, to, nonce, value}) {
let trxNonce = nonce;
if (nonce == -1 || !nonce) trxNonce = await web3.eth.getTransactionCount(from);
console.log(`Trx nonce ${trxNonce}`);
const totalFee = gasPrice * gas;
const balance = (await web3.eth.getBalance(from)) * 1;
if (balance < totalFee) {
console.warn('Too hi fee, not enough balance', balance/1e18, 'totalFee', totalFee/1e18);
return {hash: '', nonce: undefined};
}
let rawTx = {
nonce: web3.utils.toHex(trxNonce),
gasPrice: web3.utils.toHex(gasPrice),
gasLimit: web3.utils.toHex(gas * 1),
to: to,
from: from,
value: web3.utils.toHex(value ? value : 0),
data: payloadData
};
const bufferPrivateKey = Buffer.from(pk, 'hex');
let tx = new Tx(rawTx);
tx.sign(bufferPrivateKey);
let serializedTx = tx.serialize();
const hash = await new Promise((resolve, reject) => web3.eth.sendSignedTransaction('0x' + serializedTx.toString('hex'))
.on('error', (err) => reject(err))
.on('transactionHash', (hash) => resolve(hash)));
return {hash, nonce};
}
module.exports = {signAndSend};