Bắt đầu với TheRPC
Tham khảo API
Ethereum API
Core API
Hướng dẫn

JavaScript/TypeScript Tools

JavaScript offers several powerful libraries for Ethereum development, with web3.js and ethers.js being the most popular choices.

Chú ý!
Xin chào! Lưu ý thân thiện: trang này nhằm cung cấp cho bạn tổng quan chi tiết về việc làm việc với Javascript/Typescript các yêu cầu. Đối với ví dụ mã thực tế, hãy xem API Methods documentation nơi bạn sẽ tìm thấy các ví dụ sẵn sàng sử dụng trong tất cả các ngôn ngữ được hỗ trợ!

# Web3.js

The original and most widely used Ethereum JavaScript API.

import Web3 from 'web3';
const web3 = new Web3('YOUR_ETHEREUM_NODE_URL');

// Get balance
const getBalance = async (address) => {
	const balance = await web3.eth.getBalance(address);
	const balanceInEth = web3.utils.fromWei(balance, 'ether');
	return balanceInEth;
};

// Send transaction
const sendTransaction = async (from, to, value) => {
	const tx = {
		from,
		to,
		value: web3.utils.toWei(value, 'ether'),
	};
	return await web3.eth.sendTransaction(tx);
};
  • GitHub: web3.js
  • Documentation: web3js.readthedocs.io
  • Features:
    • Complete Ethereum API coverage
    • Extensive community support
    • WebSocket support
    • Contract interactions
    • ENS support

# Ethers.js

Modern, complete and compact library.

import { ethers } from 'ethers';
const provider = new ethers.JsonRpcProvider('YOUR_ETHEREUM_NODE_URL');

// Get balance
const getBalance = async (address) => {
	const balance = await provider.getBalance(address);
	return ethers.formatEther(balance);
};

// Send transaction
const sendTransaction = async (wallet, to, value) => {
	const tx = await wallet.sendTransaction({
		to,
		value: ethers.parseEther(value),
	});
	return await tx.wait();
};
  • GitHub: ethers.js
  • Documentation: docs.ethers.org
  • Features:
    • TypeScript support
    • Smaller bundle size
    • Better security features
    • ENS support
    • Extensive testing

# TypeScript Support

Both libraries provide excellent TypeScript support. When using TypeScript, you get:

  • Type definitions out of the box
  • Better IDE support
  • Compile-time error checking
  • Enhanced code documentation
import { BigNumber } from 'ethers';

interface TransactionData {
	to: string;
	value: BigNumber;
	gasLimit?: BigNumber;
}

const createTransaction = async (data: TransactionData) => {
	// Your implementation
};

# Node.js Usage

Both libraries work seamlessly in Node.js environments:

// Web3.js in Node.js
const Web3 = require('web3');
const web3 = new Web3('YOUR_ETHEREUM_NODE_URL');

// Ethers.js in Node.js
const { ethers } = require('ethers');
const provider = new ethers.JsonRpcProvider('YOUR_ETHEREUM_NODE_URL');

# Browser Usage

For browser environments, you can use either library with bundlers like webpack or vite:

// Using ES modules
import { Web3 } from 'web3';
// or
import { ethers } from 'ethers';

// Using window.ethereum (MetaMask)
const provider = new ethers.BrowserProvider(window.ethereum);

See also

Giúp chúng tôi trở nên tốt hơn!
Chia sẻ trang này và giúp chúng tôi tạo ra sản phẩm tốt hơn cho bạn.