Skip to content

Commit

Permalink
4975/net documentation (#5258)
Browse files Browse the repository at this point in the history
* adding docs for net

* update net docs

* adding migration guide changes for consistent

* adding then methods

* await instead of then

Co-authored-by: Oleksii Kosynskyi <oleksii.kosynskyi@gmail.com>
  • Loading branch information
Alex and avkos authored Jul 19, 2022
1 parent a571baa commit 5662af6
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
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
* await web3.eth.net.getId();
* > 5777n
*
* // get the peer count
* await web3.eth.net.getPeerCount();
* > 0n
*
* // Check if the node is listening for peers
* await 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');
* await 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');
* await 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');
* await net.isListening();
* > true
* ```
*/
public async isListening() {
return rpcMethodsWrappers.isListening(this);
}
Expand Down

0 comments on commit 5662af6

Please sign in to comment.