Introduction
Building decentralized applications requires specialized tools. This guide covers the essential Web3 development tools and resources.
Smart Contract Development
Frameworks
# Hardhat - Ethereum development environment
npm install --save-dev hardhat
# Create project
npx hardhat init
// hardhat.config.js
module.exports = {
solidity: "0.8.19",
networks: {
mainnet: {
url: process.env.MAINNET_RPC,
accounts: [process.env.PRIVATE_KEY]
}
}
};
// Deploy script
const hre = require("hardhat");
async function main() {
const Contract = await hre.ethers.getContractFactory("MyContract");
const contract = await Contract.deploy();
await contract.deployed();
console.log("Deployed to:", contract.address);
}
main();
Solidity Libraries
// OpenZeppelin Contracts
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyNFT is ERC721, Ownable {
uint256 public tokenCount;
constructor() ERC721("MyNFT", "MNFT") {}
function mint(address to) external onlyOwner {
_mint(to, tokenCount);
tokenCount++;
}
}
Blockchain SDKs
Ethers.js
import { ethers } from 'ethers';
// Connect to wallet
const provider = new ethers.JsonRpcProvider('https://eth-mainnet.g.alchemy.com/v2/KEY');
const wallet = new ethers.Wallet(privateKey, provider);
// Read contract
const contract = new ethers.Contract(contractAddress, abi, provider);
const balance = await contract.balanceOf(address);
// Write contract
const contractWithSigner = contract.connect(wallet);
const tx = await contractWithSigner.mint(address, amount);
await tx.wait();
Web3.js
import Web3 from 'web3';
const web3 = new Web3('https://eth-mainnet.g.alchemy.com/v2/KEY');
// Get accounts
const accounts = await web3.eth.getAccounts();
// Send transaction
await web3.eth.sendTransaction({
from: accounts[0],
to: '0x...',
value: web3.utils.toWei('0.1', 'ether')
});
Testing Tools
Smart Contract Testing
const { expect } = require("chai");
describe("MyToken", function() {
let token;
let owner;
let addr1;
beforeEach(async function() {
const Token = await ethers.getContractFactory("MyToken");
token = await Token.deploy();
[owner, addr1] = await ethers.getSigners();
});
it("Should mint tokens to owner", async function() {
await token.mint(owner.address, 100);
expect(await token.balanceOf(owner.address)).to.equal(100);
});
});
Frontend Integration
Wallet Connection
// Connect wallet
async function connectWallet() {
if (window.ethereum) {
const provider = new ethers.BrowserProvider(window.ethereum);
const accounts = await provider.send("eth_requestAccounts", []);
return accounts[0];
}
throw new Error("No wallet found");
}
// Listen for account changes
window.ethereum.on('accountsChanged', (accounts) => {
if (accounts.length === 0) {
// Disconnected
} else {
// Account changed
}
});
Indexing and Data
The Graph
# Subgraph schema
type Transfer @entity {
id: ID!
from: Bytes!
to: Bytes!
value: BigInt!
timestamp: BigInt!
}
type _Schema_
@fulltext(name: "tokenSearch", fields: [{name: "id"}])
Conclusion
Web3 development tools have matured significantly. These tools enable professional-grade dApp development.
Comments