-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtx.js
38 lines (32 loc) · 1.39 KB
/
tx.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
async function main() {
require('dotenv').config();
const { ALCHEMY_API_KEY_HTTP, PRIVATE_KEY } = process.env;
const { createAlchemyWeb3 } = require("@alch/alchemy-web3");
const web3 = createAlchemyWeb3(ALCHEMY_API_KEY_HTTP);
const myAddress = '0x610Ae88399fc1687FA7530Aac28eC2539c7d6d63' //TODO: replace this address with your own public address
const nonce = await web3.eth.getTransactionCount(myAddress, 'latest'); // nonce starts counting from 0
web3.eth.getBalance(myAddress, function(err, result) {
if (err) {
console.log(err)
} else {
console.log(web3.utils.fromWei(result, "ether") + " ETH")
}
})
const transaction = {
'to': '0x0e5069514a3Dd613350BAB01B58FD850058E5ca4', // faucet address to return eth
'value': 100,
'gas': 30000,
'maxFeePerGas': 2600000000,
'nonce': nonce,
// optional data field to send message or execute smart contract
};
const signedTx = await web3.eth.accounts.signTransaction(transaction, PRIVATE_KEY);
web3.eth.sendSignedTransaction(signedTx.rawTransaction, function(error, hash) {
if (!error) {
console.log("🎉 The hash of your transaction is: ", hash, "\n Check Alchemy's Mempool to view the status of your transaction!");
} else {
console.log("❗Something went wrong while submitting your transaction:", error)
}
});
}
main();