Experiment 3B
Experiment 3B
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-
// viewAccounts();
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);
// 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();
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).