Page cover

Web3 Dev Toolkit: Libraries and Frameworks

1.1 Web3 Dev Toolkit Overview

Web3 Dev Toolkit within CapsureLabs is designed to equip developers with essential tools, libraries, and frameworks for creating, testing, and deploying decentralized applications (dApps). This toolkit supports integration with popular JavaScript libraries such as Web3.js and Ethers.js to interact with the Ethereum blockchain, manage smart contracts, and build user interfaces for decentralized applications.


A JavaScript library for interacting with Ethereum, including smart contract functions, wallet management, and transaction handling.


1.3 Code Using Web3.js

Setup and Installation

npm install web3

Connect to Ethereum

const Web3 = require('web3');
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID');

// Check connection
web3.eth.getBlockNumber()
  .then(block => console.log('Current block number:', block))
  .catch(err => console.error('Error connecting:', err));

Interacting with a Smart Contract

const tokenAddress = '0xYourTokenAddress';
const walletAddress = '0xYourWalletAddress';
const abi = [/* ERC-20 ABI here */];

const contract = new web3.eth.Contract(abi, tokenAddress);

contract.methods.balanceOf(walletAddress).call()
  .then(balance => console.log('Token balance:', balance))
  .catch(error => console.error('Error fetching balance:', error));

Sending a Transaction

const sender = '0xSenderAddress';
const receiver = '0xReceiverAddress';
const amount = web3.utils.toWei('0.1', 'ether');

web3.eth.sendTransaction({
  from: sender,
  to: receiver,
  value: amount
}).then(receipt => {
  console.log('Transaction successful:', receipt);
}).catch(err => {
  console.error('Transaction error:', err);
});

1.4 Code Using Ethers.js

Setup and Installation

npm install ethers

Connect to Ethereum

const { ethers } = require('ethers');
const provider = new ethers.providers.InfuraProvider('mainnet', 'YOUR_INFURA_PROJECT_ID');

// Check connection
provider.getBlockNumber()
  .then(blockNumber => console.log('Current block number:', blockNumber))
  .catch(err => console.error('Error connecting:', err));

Interacting with a Smart Contract

const tokenAddress = '0xYourTokenAddress';
const walletAddress = '0xYourWalletAddress';
const abi = [/* ERC-20 ABI here */];
const provider = new ethers.providers.InfuraProvider('mainnet', 'YOUR_INFURA_PROJECT_ID');

const contract = new ethers.Contract(tokenAddress, abi, provider);

async function getBalance() {
  try {
    const balance = await contract.balanceOf(walletAddress);
    console.log('Token balance:', ethers.utils.formatUnits(balance, 18));
  } catch (error) {
    console.error('Error fetching balance:', error);
  }
}

getBalance();

Sending a Transaction

const walletMnemonic = ethers.Wallet.fromMnemonic('your mnemonic here');
const wallet = walletMnemonic.connect(provider);

async function sendEther() {
  const tx = {
    to: '0xReceiverAddress',
    value: ethers.utils.parseEther('0.1')
  };

  try {
    const transactionResponse = await wallet.sendTransaction(tx);
    console.log('Transaction successful:', transactionResponse);
  } catch (error) {
    console.error('Transaction error:', error);
  }
}

sendEther();

Last updated