From 622daced7fac0651db4b3045b233a94aea14fd85 Mon Sep 17 00:00:00 2001 From: Emmanuel Yusuf Date: Wed, 24 Jul 2024 05:36:16 +0100 Subject: [PATCH 1/4] updated glossary --- docs/docs/glossary/index.md | 82 +++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) diff --git a/docs/docs/glossary/index.md b/docs/docs/glossary/index.md index 39294de39ed..5749b4b48cd 100644 --- a/docs/docs/glossary/index.md +++ b/docs/docs/glossary/index.md @@ -108,3 +108,85 @@ contract Test { } ] ``` + +## Proxy + +A `proxy` in Web3.js serves as an intermediary between your application and an Ethereum node, **facilitating communication** by **forwarding requests and responses**. Configuring a proxy can help overcome network restrictions, enhance security, and improve load balancing. Understanding how to set up and use proxies with `HttpProvider` and `WebsocketProvider` in Web3.js is crucial for building robust and secure blockchain applications. + +## HttpProvider +`HttpProvider` in Web3.js connects an application to an Ethereum node over HTTP. It allows for sending transactions, reading blockchain data, and interacting with smart contracts. You create a Web3 instance with the node’s URL to establish the connection. It’s essential for DApps needing blockchain interaction but can block the event loop, so alternatives like `WebSocketProvider` might be used for better performance. Proper security measures are crucial when exposing HTTP endpoints. + +```javascript title='HttpProvider example' +const Web3 = require('web3'); +const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')); +``` + +## WebSocketProvider +`WebSocketProvider` in Web3.js connects your application to an Ethereum node via WebSocket, enabling real-time and asynchronous communication. This provider is ideal for applications needing real-time updates, such as new blocks or smart contract events. It offers better performance for high-throughput applications compared to `HttpProvider`. Ensure secure connections with `wss://` for exposed endpoints. Handle reconnections gracefully for reliable operation. + +```javascript title='WebSocketProvider example' +const Web3 = require('web3'); +const web3 = new Web3(new Web3.providers.WebsocketProvider('ws://localhost:8546')); +``` + +## Events + +The `Events` class in Web3.js is a crucial part of the library that enables developers to interact with and listen for events emitted by smart contracts on the Ethereum network. Events in **smart contracts** are similar to `logs` or `messages` that the **contract emits to notify** external applications about specific actions or state changes. Web3.js provides a comprehensive set of tools to handle these events, making it possible to build responsive and interactive decentralized applications (dApps). + +#### Example + +```solidity title='Event in solidity' +contract MyContract { + event Transfer(address indexed from, address indexed to, uint value); + + function transfer(address recipient, uint amount) public { + // ... transfer logic ... + emit Transfer(msg.sender, recipient, amount); + } +} +``` + +```javascript title='Event in web3.js' +const Web3 = require('web3'); +const MyContract = require('./MyContract.json'); // Assuming ABI is loaded + +const web3 = new Web3('wss://mainnet.infura.io/v3/YOUR_INFURA_ID'); // Replace with your provider URL +const contractAddress = '0x...'; // Replace with your contract address + +const myContract = new web3.eth.Contract(MyContract.abi, contractAddress); + +const transferEvent = myContract.events.Transfer(); // Access the Transfer event + +transferEvent.on('data', (event) => { + console.log('Transfer Event:', event); + // Process the event data (from, to, value) +}); +``` + +## Event logs + +`Logs` in Web3.js are a part of **Ethereum transactions** that contain **information about events triggered** within smart contracts. They provide a way to record and retrieve significant occurrences within the blockchain. `Event logs` are particularly useful for tracking changes, and debugging. + +#### Example + +```javascript title='Event Log in web3.js' +const Web3 = require('web3'); +const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID'); + +const options = { + fromBlock: 0, + toBlock: 'latest', + address: '0xYourContractAddress', + topics: [ + web3.utils.sha3('Transfer(address,address,uint256)') + ] +}; + +web3.eth.getPastLogs(options) + .then((logs) => { + console.log(logs); + }) + .catch((error) => { + console.error('Error retrieving logs:', error); + }); +` \ No newline at end of file From 0a970468f6f4847792d0102d31062366fac9b81b Mon Sep 17 00:00:00 2001 From: Emmanuel Yusuf Date: Wed, 24 Jul 2024 06:37:26 +0100 Subject: [PATCH 2/4] chore: change import to ES import --- docs/docs/glossary/index.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/docs/glossary/index.md b/docs/docs/glossary/index.md index 5749b4b48cd..f1947624208 100644 --- a/docs/docs/glossary/index.md +++ b/docs/docs/glossary/index.md @@ -117,7 +117,7 @@ A `proxy` in Web3.js serves as an intermediary between your application and an E `HttpProvider` in Web3.js connects an application to an Ethereum node over HTTP. It allows for sending transactions, reading blockchain data, and interacting with smart contracts. You create a Web3 instance with the node’s URL to establish the connection. It’s essential for DApps needing blockchain interaction but can block the event loop, so alternatives like `WebSocketProvider` might be used for better performance. Proper security measures are crucial when exposing HTTP endpoints. ```javascript title='HttpProvider example' -const Web3 = require('web3'); +import { Web3 } from 'web3'; const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')); ``` @@ -125,7 +125,7 @@ const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')); `WebSocketProvider` in Web3.js connects your application to an Ethereum node via WebSocket, enabling real-time and asynchronous communication. This provider is ideal for applications needing real-time updates, such as new blocks or smart contract events. It offers better performance for high-throughput applications compared to `HttpProvider`. Ensure secure connections with `wss://` for exposed endpoints. Handle reconnections gracefully for reliable operation. ```javascript title='WebSocketProvider example' -const Web3 = require('web3'); +import { Web3 } from 'web3'; const web3 = new Web3(new Web3.providers.WebsocketProvider('ws://localhost:8546')); ``` @@ -147,7 +147,7 @@ contract MyContract { ``` ```javascript title='Event in web3.js' -const Web3 = require('web3'); +import { Web3 } from 'web3'; const MyContract = require('./MyContract.json'); // Assuming ABI is loaded const web3 = new Web3('wss://mainnet.infura.io/v3/YOUR_INFURA_ID'); // Replace with your provider URL @@ -170,7 +170,7 @@ transferEvent.on('data', (event) => { #### Example ```javascript title='Event Log in web3.js' -const Web3 = require('web3'); +import { Web3 } from 'web3'; const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID'); const options = { From ec60406dea05162c4d0912de756d421e08233b13 Mon Sep 17 00:00:00 2001 From: Emmanuel Yusuf Date: Wed, 24 Jul 2024 14:51:21 +0100 Subject: [PATCH 3/4] change javascript to typescript --- docs/docs/glossary/index.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/docs/glossary/index.md b/docs/docs/glossary/index.md index f1947624208..b6cf4cd7d1b 100644 --- a/docs/docs/glossary/index.md +++ b/docs/docs/glossary/index.md @@ -116,7 +116,7 @@ A `proxy` in Web3.js serves as an intermediary between your application and an E ## HttpProvider `HttpProvider` in Web3.js connects an application to an Ethereum node over HTTP. It allows for sending transactions, reading blockchain data, and interacting with smart contracts. You create a Web3 instance with the node’s URL to establish the connection. It’s essential for DApps needing blockchain interaction but can block the event loop, so alternatives like `WebSocketProvider` might be used for better performance. Proper security measures are crucial when exposing HTTP endpoints. -```javascript title='HttpProvider example' +```typescript import { Web3 } from 'web3'; const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')); ``` @@ -169,7 +169,7 @@ transferEvent.on('data', (event) => { #### Example -```javascript title='Event Log in web3.js' +```typescript import { Web3 } from 'web3'; const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID'); From a5cb2d05c56b17ede626c47a291fd3c690b06068 Mon Sep 17 00:00:00 2001 From: Emmanuel Yusuf Date: Thu, 25 Jul 2024 09:49:27 +0100 Subject: [PATCH 4/4] update content based on feedback --- docs/docs/glossary/index.md | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/docs/docs/glossary/index.md b/docs/docs/glossary/index.md index b6cf4cd7d1b..1af9691a85e 100644 --- a/docs/docs/glossary/index.md +++ b/docs/docs/glossary/index.md @@ -111,10 +111,11 @@ contract Test { ## Proxy -A `proxy` in Web3.js serves as an intermediary between your application and an Ethereum node, **facilitating communication** by **forwarding requests and responses**. Configuring a proxy can help overcome network restrictions, enhance security, and improve load balancing. Understanding how to set up and use proxies with `HttpProvider` and `WebsocketProvider` in Web3.js is crucial for building robust and secure blockchain applications. +A `proxy` in Web3.js serves as an intermediary between your application and an Ethereum node, **facilitating communication** by **forwarding requests and responses**. Configuring a proxy can help overcome network restrictions, enhance security, and improve load balancing. You can set up a proxy using either HttpProvider or WebSocketProvider in Web3.js. ## HttpProvider -`HttpProvider` in Web3.js connects an application to an Ethereum node over HTTP. It allows for sending transactions, reading blockchain data, and interacting with smart contracts. You create a Web3 instance with the node’s URL to establish the connection. It’s essential for DApps needing blockchain interaction but can block the event loop, so alternatives like `WebSocketProvider` might be used for better performance. Proper security measures are crucial when exposing HTTP endpoints. + +[HttpProvider](https://docs.web3js.org/guides/web3_providers_guide/#http-provider) in Web3.js connects an application to an Ethereum node over HTTP. It allows for sending transactions, reading blockchain data, and interacting with smart contracts. You create a Web3 instance with the node’s URL to establish the connection. It’s essential for DApps needing blockchain interaction but can block the event loop, so alternatives like `WebSocketProvider` might be used for better performance when real-time communication is needed. ```typescript import { Web3 } from 'web3'; @@ -122,7 +123,7 @@ const web3 = new Web3(new Web3.providers.HttpProvider('http://localhost:8545')); ``` ## WebSocketProvider -`WebSocketProvider` in Web3.js connects your application to an Ethereum node via WebSocket, enabling real-time and asynchronous communication. This provider is ideal for applications needing real-time updates, such as new blocks or smart contract events. It offers better performance for high-throughput applications compared to `HttpProvider`. Ensure secure connections with `wss://` for exposed endpoints. Handle reconnections gracefully for reliable operation. +[WebSocketProvider](https://docs.web3js.org/guides/web3_providers_guide/#websocket-provider) in Web3.js connects your application to an Ethereum node via WebSocket, enabling real-time and asynchronous communication. This provider is ideal for applications needing real-time updates, such as new blocks or smart contract events. It offers better performance for high-throughput applications compared to `HttpProvider`. Ensure secure connections with `wss://` for exposed endpoints. Handle reconnections gracefully for reliable operation. ```javascript title='WebSocketProvider example' import { Web3 } from 'web3';