Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4975/net documentation #5258

Merged
merged 9 commits into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/docs/guides/accounts_migration_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ sidebar_position: 5
sidebar_label: Web3.eth.accounts
---

# Web3.eth.accounts Migration Guide
# Web3 eth accounts Migration Guide

## Breaking changes

Expand Down
5 changes: 5 additions & 0 deletions docs/docs/guides/net_migration_guide.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
---
sidebar_position: 8
sidebar_label: Web3.*.net
---

# Web3 Net Migration guide

## Breaking Changes
Expand Down
30 changes: 30 additions & 0 deletions packages/web3-net/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,36 @@ You should have received a copy of the GNU Lesser General Public License
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/

/**
* The web3-net package allows you to interact with an Ethereum node’s network properties.
*
* ```ts
* import Net from 'web3-net';
*
* const net = new Net(Net.givenProvider || 'ws://some.local-or-remote.node:8546');
* // or using the web3 umbrella package
* import Web3 from 'web3';
* const web3 = new Web3(Web3.givenProvider || 'ws://some.local-or-remote.node:8546');
*
* // -> web3.eth.net
*
* // get the ID of the network
* web3.eth.net.getId();
* > 5777n
*
* // get the peer count
* web3.eth.net.getPeerCount();
* > 0n
*
* // Check if the node is listening for peers
* web3.eth.net.isListening();
* > true
* ```
*/

/**
*
*/
import { Net } from './net';

export * from './net';
Expand Down
30 changes: 30 additions & 0 deletions packages/web3-net/src/net.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,48 @@ import * as rpcMethodsWrappers from './rpc_method_wrappers';
import { Web3NetAPI } from './web3_net_api';

export class Net extends Web3Context<Web3NetAPI> {
/**
* Gets the current network ID
* @returns A Promise of the network ID.
* @example
* ```ts
* const net = new Net(Net.givenProvider || 'ws://some.local-or-remote.node:8546');
* net.getId();
* > 1
* ```
*/
public async getId<ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT>(
returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat,
) {
return rpcMethodsWrappers.getId(this, returnFormat);
}

/**
* Get the number of peers connected to.
* @returns A promise of the number of the peers connected to.
* @example
* ```ts
* const net = new Net(Net.givenProvider || 'ws://some.local-or-remote.node:8546');
* net.getPeerCount();
* > 0
* ```
*/
public async getPeerCount<ReturnFormat extends DataFormat = typeof DEFAULT_RETURN_FORMAT>(
returnFormat: ReturnFormat = DEFAULT_RETURN_FORMAT as ReturnFormat,
) {
return rpcMethodsWrappers.getPeerCount(this, returnFormat);
}

/**
* Check if the node is listening for peers
* @returns A promise of a boolean if the node is listening to peers
* @example
* ```ts
* const net = new Net(Net.givenProvider || 'ws://some.local-or-remote.node:8546');
* net.isListening();
* > true
* ```
*/
public async isListening() {
return rpcMethodsWrappers.isListening(this);
}
Expand Down