Experiment – 3B
Name: Ansari Mohammed Shanouf Valijan
Class: B.E. Computer Engineering, Semester - VII
UID: 2021300004
Batch: A
Objective:
To perform Web3 scripting by considering Ganache as the test blockchain network and
MetaMask as the Wallet.
Implementation:
Following is a step-by-step implementation as it was carried out to code the five tasks that
were mentioned during the lab session-
Starting ganache local blockchain environment (localhost:8545) after installing it
Importing all the test accounts to MetaMask wallet locally-
Writing the web3 script to carry out the five tasks
const {Web3} = require('web3');
const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545'));
async function viewAccounts() {
const accounts = await web3.eth.getAccounts();
for (const account of accounts) {
const balance = await web3.eth.getBalance(account);
console.log(`Account: ${account}, Balance: ${web3.utils.fromWei(balance, 'ether')}
ETH`);
}
}
// viewAccounts();
async function sendTransaction(fromAccount, toAccount, amount) {
const transaction = {
from: fromAccount,
to: toAccount,
value: web3.utils.toWei(amount.toString(), 'ether'),
gas: 2000000,
};
try {
const receipt = await web3.eth.sendTransaction(transaction);
console.log('Transaction successful with hash:', receipt.transactionHash);
} catch (error) {
console.error('Transaction failed:', error);
}
}
// sendTransaction('0xDD79402872cCc4Cb95Ce58B4E803259801B92768',
'0xFC22f956D95d3f59F19ccE92dF1FEa31bD98f788', 10);
async function getNumberOfBlocks() {
const blockNumber = await web3.eth.getBlockNumber();
console.log('Number of Blocks:', blockNumber);
}
// getNumberOfBlocks()
async function getGasValueOfLastTransaction() {
const latestBlock = await web3.eth.getBlock('latest');
const lastTransaction = latestBlock.transactions[latestBlock.transactions.length - 1];
if (lastTransaction) {
const transaction = await web3.eth.getTransaction(lastTransaction);
console.log('Gas Used in Last Transaction:', transaction.gas);
} else {
console.log('No transactions in the latest block.');
}
}
// getGasValueOfLastTransaction()
async function getContractAddress() {
const contractAddress = '0x1234567890abcdef1234567890abcdef12345678';
console.log('Contract Address:', contractAddress);
}
(async () => {
console.log('\n\n----------Viewing all accounts----------')
await viewAccounts();
const accounts = await web3.eth.getAccounts();
console.log('\n\n----------Performing a transaction from account 1 to account 2-------
---')
await sendTransaction(accounts[0], accounts[1], '100'); // Send 1 Ether from
account[0] to account[1]
console.log('\n\n----------Viewing the updated accounts----------')
await viewAccounts()
console.log('\n\n----------Getting the number of blocks in the blockchain----------')
await getNumberOfBlocks();
console.log('\n\n----------Viewing the gas value of last transaction----------')
await getGasValueOfLastTransaction();
console.log('\n\n----------Viewing contract address----------')
await getContractAddress(); // Update with your actual contract address
})();
Running the script and viewing the outputs-
Conclusion:
By performing this experiment, I was able to get familiar with Ganache and MetaMask. I
understood how web3 scripting may be used to interact with ganache. Further, I was able to
comprehend the role of ganache as a test blockchain local network and how it may be used
while building a Decentralized application (any application that exploits the benefits of
blockchain technology in general).