Skip to content
This repository was archived by the owner on Mar 5, 2025. It is now read-only.

Commit 16a12b1

Browse files
authored
Merge pull request #2799 from ethereum/documentation/web3-module-example
Module API documentation improved
2 parents cf5a8d8 + cecca9f commit 16a12b1

File tree

2 files changed

+95
-0
lines changed

2 files changed

+95
-0
lines changed

docs/web3-core.rst

+22
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,28 @@ The ``AbstractWeb3Module`` does have the following constructor parameters:
2222
- ``methodFactory`` - ``AbstractMethodFactory`` The :ref:`AbstractMethodFactory <web3-abstract-method-factory>` will be used in the module proxy for the JSON-RPC method calls. (optional)
2323
- ``net`` - ``net.Socket`` The ``net.Socket`` object of the NodeJS net module. (optional)
2424

25+
-------
26+
Example
27+
-------
28+
29+
.. code-block:: javascript
30+
31+
import {AbstractWeb3Module} from 'web3-core';
32+
33+
class Example extends AbstractWeb3Module {
34+
/**
35+
* @param {AbstractSocketProvider|HttpProvider|CustomProvider|String} provider
36+
* @param {AbstractMethodFactory} methodFactory
37+
* @param {Web3ModuleOptions} options
38+
* @param {Net.Socket} nodeNet
39+
*
40+
* @constructor
41+
*/
42+
constructor(provider, net, methodFactory, options) {
43+
super(provider, net, methodFactory, options;
44+
}
45+
}
46+
2547
2648
Interface of the ``AbstractWeb3Module`` class:
2749

docs/web3-module.rst

+73
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,76 @@ These are the core modules which are providing all the classes for the Web3 Modu
2020
- :ref:`web3-core-subscriptions <web3-core-subscriptions>`
2121
- :ref:`Contract <web3-module-contract>`
2222

23+
-------
24+
Example
25+
-------
26+
27+
.. code-block:: javascript
28+
29+
import * as Utils from 'web3-utils';
30+
import {formatters} from 'web3-core-formatters';
31+
import {AbstractWeb3Module} from 'web3-core';
32+
import {AbstractMethodFactory, GetBlockByNumberMethod, AbstractMethod} from 'web3-core-method';
33+
34+
class MethodFactory extends AbstractMethodFactory {
35+
/**
36+
* @param {Utils} utils
37+
* @param {Object} formatters
38+
*
39+
* @constructor
40+
*/
41+
constructor(utils, formatters) {
42+
super(utils, formatters);
43+
44+
this.methods = {
45+
getBlockByNumber: GetBlockByNumberMethod
46+
};
47+
}
48+
}
49+
50+
class Example extends AbstractWeb3Module {
51+
/**
52+
* @param {AbstractSocketProvider|HttpProvider|CustomProvider|String} provider
53+
* @param {Web3ModuleOptions} options
54+
* @param {Net.Socket} nodeNet
55+
*
56+
* @constructor
57+
*/
58+
constructor(provider, net, options) {
59+
super(provider, net, new MethodFactory(Utils, formatters), options;
60+
}
61+
62+
sign() {
63+
const method = new AbstractMethod('eth_sign', 2, utils, formatters, this);
64+
method.setArguments(arguments)
65+
66+
return method.execute();
67+
}
68+
69+
logs(options) {
70+
return new LogSubscription(
71+
options,
72+
Utils,
73+
formatters,
74+
this,
75+
new GetPastLogsMethod(Utils, formatters, this)
76+
);
77+
}
78+
}
79+
80+
const example = new Example(provider, net, options);
81+
82+
example.sign('0x0', 'message').then(console.log);
83+
// > "response"
84+
85+
example.sign('0x0', 'message', (error, response) => {
86+
console.log(response);
87+
};
88+
// > "response"
89+
90+
const block = example.getBlockByNumber(1).then(console.log);
91+
// > {}
92+
93+
example.logs(options).subscribe(console.log);
94+
> {}
95+

0 commit comments

Comments
 (0)