-
Notifications
You must be signed in to change notification settings - Fork 0
/
ethTxHandler.js
63 lines (58 loc) · 1.91 KB
/
ethTxHandler.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
var Web3 = require('web3');
var axios = require('axios');
var web3 = new Web3(new Web3.providers.HttpProvider('https://goerli.infura.io/v3/0104fc70dc744b10bcbb51ae406d21ca'));
async function getCurrentGasPrices() {
let response = await axios.get('https://ethgasstation.info/json/ethgasAPI.json');
let prices = {
low: response.data.safeLow / 10,
medium: response.data.average / 10,
high: response.data.fast / 10
};
return prices;
}
let signSendTx = async (txParams, privateKey) => {
console.log("here1");
txParams.nonce = web3.utils.toHex(await web3.eth.getTransactionCount(txParams.from, 'pending'));
console.log("txParams: ", txParams);
console.log("nonce: ");
console.log(txParams.nonce);
// txParams.gasPrice = web3.eth.gasPrice;
txParams.data = web3.utils.toHex(txParams.data)
console.log("here2");
console.log(txParams);
txParams.gas = (await web3.eth.estimateGas({ to: txParams.to, data: txParams.data })) * 5;
console.log("gas: ", txParams.gas);
let transaction = {
transactionHash: ''
};
web3.eth.accounts.signTransaction(txParams, privateKey)
.then(async (signedTx) => {
const rawTx = (signedTx.rawTransaction || signedTx.raw);
console.log("rawTx: ", rawTx);
let tx = await web3.eth.sendSignedTransaction(rawTx);
return tx;
})
.then((tx) => {
console.log("tx: ", tx);
transaction.transactionHash = tx.transactionHash;
})
.catch((err) => {
console.log(err);
return err;
});
return transaction.transactionHash;
}
let getReceipt = async (txHash) => {
try {
let transaction = (await web3.eth.getTransaction(txHash));
let inp = web3.utils.hexToUtf8(transaction.input);
return inp;
} catch(err) {
return new Error(err);
}
}
module.exports = {
signSendTx,
getCurrentGasPrices,
getReceipt
};