-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendTx.js
43 lines (38 loc) · 1.23 KB
/
sendTx.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
async function main() {
require("dotenv").config();
const { API_URL, PRIVATE_KEY } = process.env;
const { createAlchemyWeb3 } = require("@alch/alchemy-web3");
const web3 = createAlchemyWeb3(API_URL);
const myAddress = "0xf405535E43BeeF232e67D95F77F36E6a0B4cfAe1"; //TODO: replace this address with your own public address
const nonce = await web3.eth.getTransactionCount(myAddress, "latest"); // nonce starts counting from 0
const transaction = {
to: "0x31B98D14007bDEe637298086988A0bBd31184523", // faucet address to return eth
value: 100,
gas: 30000,
maxFeePerGas: 1000000108,
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();