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

Commit 51a59f9

Browse files
Few touches on the Plugin Guid (#6182)
* exported few more basic classes from web3 package * update plugin developer guid * update plugin users guid * updated CHANGELOG.md of the web3 package
1 parent a2a232f commit 51a59f9

File tree

2 files changed

+27
-19
lines changed

2 files changed

+27
-19
lines changed

docs/docs/guides/web3_plugin_guide/plugin_authors.md

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,18 @@ To provide type safety and IntelliSense for your plugin users, please refer to t
1313

1414
## Plugin Dependencies
1515

16-
At the minimum, your plugin should depend on the `4.x` version of `web3`. This will allow your plugin class to extend the provided `Web3PluginBase` abstract class. However, `web3` shouldn't be listed as a regular dependency, instead it should be listed in your plugin's `package.json` as a [peer dependency](https://nodejs.org/en/blog/npm/peer-dependencies/):
16+
At the minimum, your plugin should depend on `web3` package version `4.0.2`. This will allow your plugin class to extend the provided `Web3PluginBase` abstract class. However, `web3` shouldn't be listed as a regular dependency, instead it should be listed in your plugin's `package.json` as a [peer dependency](https://nodejs.org/en/blog/npm/peer-dependencies/).
17+
18+
:::important
19+
If the version `[email protected]`, was not available yet. You can use the version `[email protected]`.
20+
:::
1721

1822
```json
1923
{
2024
"name": "web3-plugin-custom-rpc-methods",
2125
"version": "0.1.0",
2226
"peerDependencies": {
23-
"web3": ">= 4.0.1 < 5"
27+
"web3": ">= 4.0.2 < 5"
2428
}
2529
}
2630
```
@@ -210,9 +214,9 @@ public link(parentContext: Web3Context) {
210214

211215
## Setting Up Module Augmentation
212216

213-
To ensure type safety and enable IntelliSense for your plugin (which still needs to be registered by the user), you must augment the `Web3Context` class inside the `web3` module. In simpler terms, this is to modify the `Web3Context` class, and any inheriting classes, to make your plugin's functionality accessible from within. As a result, your plugin object will be accessible within a namespace of your choice, which will be available within any `Web3Context` object.
217+
In order to provide type safety and IntelliSense for your plugin when it's registered by the user, you must [augment](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation) the `Web3Context` module. In simpler terms, you will be making TypeScript aware that you are modifying the interface of the class `Web3Context`, and any class that extends it, to include the interface of your plugin (i.e. your plugin's added methods, properties, etc.). As a result, your plugin object will be accessible within a namespace of your choice, which will be available within any `Web3Context` object.
214218

215-
For a general understanding of Module Augmentation, you can refer to [this tutorial](https://www.digitalocean.com/community/tutorials/typescript-module-augmentation).
219+
A good tutorial that further explains Module Augmentation, in general, can be found [here](https://www.digitalocean.com/community/tutorials/typescript-module-augmentation).
216220

217221
### Module Augmentation
218222

@@ -245,16 +249,16 @@ declare module 'web3' {
245249
1. By augmenting `Web3Context` (and, by extension, all the classes that extend it), your plugin's interface will show up in things like IntelliSense for **all** Web3 modules that extend `Web3Context`, even if your plugin isn't registered.
246250
This is something worth making your users aware of, as they'll only be able to use your plugin if they register it with a Web3 class instance using `.registerPlugin`.
247251

248-
:::warning
252+
:::danger
249253

250-
The following represent what your **plugin users** would see, when they use the plugin `CustomRpcMethodsPlugin`, without calling `.registerPlugin`:
254+
The following represent what your **plugin users** would see, when they use the plugin `CustomRpcMethodsPlugin`, without calling `.registerPlugin`:
251255

252-
![web3 context augmentation](./assets/web3_context_augmentation.png 'Web3Context augmentation')
256+
![web3 context augmentation](./assets/web3_context_augmentation.png 'Web3Context augmentation')
253257

254-
The above screenshot shows IntelliSense thinking `.customRpcMethods.someMethod` is available to call on the instance of `Web3`, regardless if the plugin user registered or did not register `CustomRpcMethodsPlugin`.
255-
But, the user who does not call `.registerPlugin`, before accessing your plugin, would face an error. And you need to make it clear for them that they need to call `.registerPlugin`, before they can access any plugin functionality.
258+
The above screenshot shows IntelliSense thinking `.customRpcMethods.someMethod` is available to call on the instance of `Web3`, regardless if the plugin user registered or did not register `CustomRpcMethodsPlugin`.
259+
But, the user who does not call `.registerPlugin`, before accessing your plugin, would face an error. And you need to make it clear for them that they need to call `.registerPlugin`, before they can access any plugin functionality.
256260

257-
:::
261+
:::
258262

259263
2. The `registerPlugin` method exists on the `Web3Context` class, so any class that `extends Web3Context` has the ability to add your plugin's additional functionality to its interface. So, by augmenting `Web3Context` to include your plugin's interface, you're essentially providing a blanket augmentation that adds your plugin's interface to **all** Web3 modules that extend `Web3Context` (i.e. `web3`, `web3-eth`, `web3-eth-contract`, etc.).
260264

@@ -298,3 +302,7 @@ declare module 'web3' {
298302
// on the instance of Web3
299303
web3.customRpcMethods;
300304
```
305+
306+
## Complete Example
307+
308+
You may find it helpful to reference a complete example for developing and using a web3 plugin. The [Web3.js Chainlink Plugin](https://github.com/ChainSafe/web3.js-plugin-chainlink/) repository provides an excellent example which you can check out.

docs/docs/guides/web3_plugin_guide/plugin_users.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@ This guide intends to provide the necessary context for registering plugins with
99

1010
## Installing the Plugin
1111

12-
Unless otherwise mentioned by the plugin author, installing a plugin should be as simple as `yarn add web3-sample-plugin`. This should add the plugin as a dependency within your `package.json` and the plugin should be available to import within your code.
12+
Unless otherwise mentioned by the plugin author, installing a plugin should be as simple as `yarn add web3-plugin-example`. This should add the plugin as a dependency within your `package.json` and the plugin should be available to import within your code.
1313

1414
```json
1515
# package.json
1616
{
1717
...
1818
"dependencies": {
19-
"web3-sample-plugin": "0.1.0"
19+
"web3-plugin-example": "0.1.0"
2020
}
2121
}
2222
```
@@ -32,8 +32,8 @@ For illustration purposes, let's assume a plugin developer has the following cod
3232

3333
import { Web3PluginBase } from 'web3';
3434

35-
export class SamplePlugin extends Web3PluginBase {
36-
public pluginNamespace = 'samplePlugin';
35+
export class PluginExample extends Web3PluginBase {
36+
public pluginNamespace = 'pluginExample';
3737

3838
public sampleMethod() {
3939
return 'simpleValue';
@@ -43,21 +43,21 @@ export class SamplePlugin extends Web3PluginBase {
4343
// Module Augmentation
4444
declare module 'web3' {
4545
interface Web3Context {
46-
samplePlugin: SamplePlugin;
46+
pluginExample: PluginExample;
4747
}
4848
}
4949
```
5050

51-
Here is an example of how to register the `SamplePlugin` onto an instance of `Web3`:
51+
Here is an example of how to register the `PluginExample` onto an instance of `Web3`:
5252

5353
```typescript
5454
// code written by the plugin **user**
5555

5656
import Web3 from 'web3';
57-
import SamplePlugin from 'web3-sample-plugin';
57+
import PluginExample from 'web3-plugin-example';
5858

5959
const web3 = new Web3('http://127.0.0.1:8545');
60-
web3.registerPlugin(new SamplePlugin(any_parameters, if_needed));
60+
web3.registerPlugin(new PluginExample(any_parameters, if_needed));
6161

62-
web3.samplePlugin.sampleMethod();
62+
web3.pluginExample.sampleMethod();
6363
```

0 commit comments

Comments
 (0)