Skip to content

Commit 3abd133

Browse files
committed
Module 32 Complete
1 parent 90f0393 commit 3abd133

7 files changed

+181
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# What is Ethers?
2+
3+
**Ethers.js** is a lightweight and secure JavaScript library that enables you to interact with the Ethereum blockchain and ecosystem. It provides a simple interface to connect to the Ethereum network, send transactions, interact with smart contracts, manage wallets, and handle cryptographic operations such as signing messages.
4+
5+
Ethers.js is designed to be used in both the browser and Node.js environments. It supports various Ethereum providers, such as MetaMask, Infura, and Alchemy, making it flexible for a range of Ethereum-based applications, from decentralized applications (dApps) to interacting with smart contracts.
6+
7+
## Key Features:
8+
9+
- **Wallet management**: Create and manage wallets and private keys.
10+
- **Transaction handling**: Send Ether, deploy contracts, and interact with smart contracts.
11+
- **ABI encoding/decoding**: Easily encode and decode data for transactions and contracts.
12+
- **BigNumber support**: Efficiently handle large integers used in Ethereum transactions (like token amounts and gas prices).
13+
14+
Overall, Ethers.js is popular for its simplicity, small size, and focus on security, making it a great choice for Ethereum developers.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
## Ethers.js Features
2+
3+
- **Lightweight**: Ethers.js is a small library, making it ideal for use in client-side applications without bloating the project.
4+
5+
- **Cross-platform**: Works seamlessly across both Node.js and browser environments, offering flexibility in development.
6+
7+
- **Secure**: Ethers.js prioritizes security by providing safe ways to handle sensitive information like private keys and transactions. It supports wallet encryption and signing with care.
8+
9+
- **Provider Agnostic**: It can work with various Ethereum providers, including MetaMask, Infura, and Alchemy, allowing developers to connect to different Ethereum networks.
10+
11+
- **Smart Contract Support**: It simplifies interactions with smart contracts by using ABI (Application Binary Interface) encoding and decoding. This allows you to call contract methods and listen for events.
12+
13+
- **BigNumber Support**: Ethers.js handles large numbers, which are common in Ethereum transactions, such as token amounts and gas fees. It uses its own BigNumber implementation to avoid issues with JavaScript’s number limitations.
14+
15+
- **Simple API**: It offers a clean and simple API for interacting with the Ethereum blockchain, making it easy for developers to integrate Ethereum functionalities into their applications.
16+
17+
- **HD Wallet Support**: Ethers.js supports Hierarchical Deterministic (HD) wallets, allowing you to generate multiple wallet addresses from a single seed phrase.
18+
19+
- **TypeScript Support**: Ethers.js provides first-class TypeScript support for enhanced code quality and easier development.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
## Installing Ethers.js using NPM
2+
3+
To install Ethers.js in your project, you can use the following command with **npm**:
4+
5+
```bash
6+
npm install ethers
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
## Connecting to Ethereum: MetaMask
2+
3+
MetaMask is a popular browser extension wallet that allows users to interact with the Ethereum network. To connect to Ethereum using MetaMask with Ethers.js, follow these steps:
4+
5+
### Steps:
6+
7+
1. **Install MetaMask**:
8+
- Install the MetaMask extension in your browser (Chrome, Firefox, etc.) if you haven’t already.
9+
- Set up your MetaMask wallet and connect it to the desired Ethereum network (e.g., Mainnet, Rinkeby).
10+
11+
2. **Access the Ethereum Provider**:
12+
- MetaMask injects the `ethereum` object into the browser window, which Ethers.js uses as a provider.
13+
14+
3. **Request Account Access**:
15+
- Request the user to connect their MetaMask account by calling `eth_requestAccounts`.
16+
17+
4. **Create a Provider and Signer**:
18+
- Use Ethers.js to create a provider from MetaMask and obtain the signer for sending transactions.
19+
20+
### Example Code:
21+
22+
```javascript
23+
// Check if MetaMask is installed
24+
if (typeof window.ethereum !== 'undefined') {
25+
// Create a provider
26+
const provider = new ethers.providers.Web3Provider(window.ethereum);
27+
28+
// Request account access (MetaMask will prompt the user)
29+
await window.ethereum.request({ method: "eth_requestAccounts" });
30+
31+
// Get the signer (user's wallet)
32+
const signer = provider.getSigner();
33+
34+
// Now you can interact with the Ethereum network
35+
const userAddress = await signer.getAddress();
36+
console.log(`Connected address: ${userAddress}`);
37+
} else {
38+
console.log("MetaMask is not installed.");
39+
}
40+
```
41+
42+
## Explanation:
43+
- window.ethereum.request({ method: "eth_requestAccounts" }): Prompts the user to connect their MetaMask account.
44+
- ethers.providers.Web3Provider(window.ethereum): Creates a new provider to interact with Ethereum.
45+
- provider.getSigner(): Retrieves the signer (the connected wallet) to send transactions and interact with smart contracts.
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
## Connecting to Ethereum: RPC
2+
3+
You can also connect to Ethereum using a remote provider through **RPC** (Remote Procedure Call). Services like **Infura**, **Alchemy**, or your own Ethereum node provide RPC endpoints that allow you to interact with the Ethereum blockchain without needing to run a full node yourself.
4+
5+
### Steps:
6+
7+
1. **Sign up for an RPC provider**:
8+
- **Infura**: [Sign up here](https://infura.io/) and get a project ID for accessing the Ethereum network.
9+
- **Alchemy**: [Sign up here](https://www.alchemy.com/) to obtain an API URL.
10+
- You can also set up your own Ethereum node and use its RPC URL.
11+
12+
2. **Connect using the RPC URL**:
13+
- Once you have your RPC URL from Infura or Alchemy, you can use it to connect to the Ethereum network.
14+
15+
### Example Code:
16+
17+
```javascript
18+
// Replace with your own RPC URL from Infura, Alchemy, or your Ethereum node
19+
const rpcUrl = "https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID";
20+
21+
// Create a new provider
22+
const provider = new ethers.providers.JsonRpcProvider(rpcUrl);
23+
24+
// Now you can interact with the Ethereum network
25+
const blockNumber = await provider.getBlockNumber();
26+
console.log(`Current Block Number: ${blockNumber}`);
27+
```
28+
29+
## Explanation:
30+
- ethers.providers.JsonRpcProvider(rpcUrl): Creates a provider using the RPC URL to connect to the Ethereum network.
31+
- You can query blockchain data, like block numbers, account balances, and send transactions, using this provider.
32+
- This method doesn't require any user interaction, unlike MetaMask, making it ideal for server-side applications or automated scripts.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
## Building blocks of Ethers.js - Signers, Providers, and Contracts
2+
3+
Ethers.js is built around three core components: **Signers**, **Providers**, and **Contracts**. Each plays a crucial role in interacting with the Ethereum blockchain.
4+
5+
### 1. Signers
6+
7+
A **Signer** is an abstraction for an Ethereum account capable of signing transactions and messages. A signer is needed for sending transactions, deploying contracts, or interacting with smart contracts that require state changes.
8+
9+
- **Signer** is connected to a specific Ethereum account.
10+
- It allows sending transactions and interacting with contracts by signing them.
11+
- A signer can be created from a wallet or a MetaMask provider.
12+
13+
#### Example:
14+
15+
```javascript
16+
const provider = new ethers.providers.Web3Provider(window.ethereum);
17+
await window.ethereum.request({ method: "eth_requestAccounts" });
18+
19+
const signer = provider.getSigner();
20+
```
21+
- getSigner(): Returns the signer for the current account.
22+
- signer.getAddress(): Retrieves the address of the signer.
23+
24+
### 2. Providers
25+
A Provider allows interaction with the Ethereum blockchain by querying data from the network (e.g., balances, block information). Providers cannot sign transactions; they are used for reading information.
26+
27+
There are different types of providers:
28+
29+
- Web3Provider: Used with MetaMask and other injected wallets.
30+
- JsonRpcProvider: Used for connecting to remote Ethereum nodes or services like Infura and Alchemy.
31+
- EtherscanProvider: A provider that fetches data from Etherscan.
32+
33+
Example:
34+
```javascript
35+
// Using a JsonRpcProvider with an Infura URL
36+
const provider = new ethers.providers.JsonRpcProvider("https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID");
37+
38+
// Querying the current block number
39+
const blockNumber = await provider.getBlockNumber();
40+
console.log(`Current Block Number: ${blockNumber}`);
41+
42+
```
43+
44+
### 3. Contracts
45+
A Contract in Ethers.js represents an Ethereum smart contract deployed on the blockchain. You can interact with contracts by calling their methods, sending transactions, and listening to events.
46+
47+
To interact with a contract, you need the contract's ABI (Application Binary Interface) and its address.
48+
49+
Example:
50+
```javascript
51+
const contractAddress = "0x..."; // Address of the deployed contract
52+
const abi = [ /* ABI Array */ ]; // ABI of the contract
53+
const contract = new ethers.Contract(contractAddress, abi, provider);
54+
55+
// Interacting with a contract method
56+
const result = await contract.someMethod();
57+
console.log(result);
58+
```
59+
- new ethers.Contract(): Creates a contract instance.
60+
- contract.someMethod(): Calls a contract method. You can also pass parameters and retrieve return values.
61+
62+
### Summary
63+
- Signers: Used for signing transactions and interacting with state-changing methods in contracts.
64+
- Providers: Used for reading data from the Ethereum blockchain.
65+
- Contracts: Enable interaction with deployed smart contracts by calling methods and listening to events.

Module 32 - Ethers.js/text.md

Whitespace-only changes.

0 commit comments

Comments
 (0)