Learn how to interact with Ethereum using Ethers.js through these simplified and modular examples.
- ES Modules: Modern JavaScript syntax for improved readability and maintainability.
- Ethers.js: A powerful library for blockchain interaction.
- Node.js: For running scripts and managing dependencies.
- Infura: A node provider for Ethereum blockchain access.
$ git clone https://github.com/zuyux/ethers_calls
$ cd ethers_calls$ npm installCreate a .env file in the root directory and include the following details:
INFURA_ID=your_infura_project_id
ACCOUNT1=your_account1_address
ACCOUNT2=your_account2_address
PRIVATE_KEY1=your_account1_private_key
ERC20_CONTRACT=your_erc20_contract_address- Ensures readability by formatting Ether balance in human-readable units.
- Uses ES Modules to load dependencies.
$ node calls/getAccount.js- Extracts detailed transaction data for inspection and logging.
$ node calls/getBlocks.js3. getContract.js: Fetches the balance of a wallet address from a smart contract (e.g., DAI contract).
- Demonstrates how to interact with ERC20 contracts using Ethers.js.
$ node calls/getContract.js- Streams real-time blockchain data using Ethers.js’s event querying API.
$ node calls/getStream.js- Shows how to connect a wallet to an ERC20 contract and send tokens securely.
$ node calls/postContract.js- Includes detailed logging of sender and receiver balances before and after the transaction.
- Uses
.envfor securely managing private keys.
$ node calls/postTX.js