-
Notifications
You must be signed in to change notification settings - Fork 0
/
transfer.js
34 lines (28 loc) · 1.3 KB
/
transfer.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
const { Web3 } = require('web3');
// Connect to the Ethereum network using the HTTP provider
const ganacheUrl = 'http://127.0.0.1:7545';
const httpProvider = new Web3.providers.HttpProvider(ganacheUrl);
const web3 = new Web3(httpProvider);
async function main() {
try {
// Get the current block number from the network
const currentBlockNumber = await web3.eth.getBlockNumber();
console.log('Current block number:', currentBlockNumber);
// Get the list of accounts in the connected node (e.g., Ganache)
const accounts = await web3.eth.getAccounts();
// Send a transaction to the network and wait for the transaction to be mined.
// Note that sending a transaction with Ganache will cause it, in its default configuration, to min a new block.
const transactionReceipt = await web3.eth.sendTransaction({
from: accounts[0],
to: accounts[1],
value: web3.utils.toWei('0.001', 'ether'),
});
console.log('Transaction Receipt:', transactionReceipt);
// Get the updated block number
const updatedBlockNumber = await web3.eth.getBlockNumber();
console.log('Updated block number:', updatedBlockNumber);
} catch (error) {
console.error('An error occurred:', error);
}
}
main();