generated from CAJUNGOTSHOP/Paytr-33
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathweb3.js
55 lines (41 loc) · 1.7 KB
/
web3.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
const Web3 = require('web3');
// Connect to an Ethereum node. Replace YOUR_INFURA_PROJECT_URL with your Infura project URL
const web3 = new Web3(new Web3.providers.HttpProvider('YOUR_INFURA_PROJECT_URL'));
// The address from which to transfer funds
const fromAddress = '0x7713974908Be4BEd47172370115e8b1219F4A5f0';
// The address to which to transfer funds
const toAddress = '0xD8e224d85900dA192af04BeB4D698433f5E2777B';
// The private key of the sender's address (DO NOT SHARE THIS)
// Load from environment variable or secure storage
const privateKey = process.env.PRIVATE_KEY;
async function transferFunds() {
console.log('transferFunds function was called');
// Get the transaction count to use as the nonce
const nonce = await web3.eth.getTransactionCount(fromAddress);
// Get the balance of the sender
const balance = await web3.eth.getBalance(fromAddress);
// Calculate the gas price
const gasPrice = await web3.eth.getGasPrice();
// Estimate the gas needed for the transaction
const gasEstimate = 21000;
// Calculate the amount to transfer (balance - gas fees)
const amount = balance - (gasEstimate * gasPrice);
// Create the transaction object
const tx = {
from: fromAddress,
to: toAddress,
value: amount,
gas: gasEstimate,
gasPrice: gasPrice,
nonce: nonce,
chainId: 1 // Mainnet chain ID
};
// Sign the transaction
const signedTx = await web3.eth.accounts.signTransaction(tx, privateKey);
// Send the transaction
web3.eth.sendSignedTransaction(signedTx.rawTransaction)
.on('receipt', console.log)
.on('error', console.error);
}
// Call the transfer function
transferFunds();