You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Mar 5, 2025. It is now read-only.
@@ -8,7 +8,7 @@ import TabItem from '@theme/TabItem';
8
8
9
9
# Add custom RPC methods
10
10
11
-
####Introduction
11
+
## Introduction
12
12
13
13
Web3.js is a popular library for interacting with the Ethereum blockchain. It provides a set of APIs to interact with Ethereum nodes via JSON-RPC calls. For adding new JSON-RPC function calls to the library, you can do so using the plugin feature in web3.js 4.x. This allows you to extend the functionality of Web3.js and add support for new JSON-RPC methods.
14
14
@@ -18,186 +18,208 @@ In Web3.js 1.x, `web3.extend()` function could be used to add new JSON-RPC metho
18
18
19
19
Following tutorial will guide you through the process of creating a custom plugin to extend the functionality of web3.js 4.x and add support for new RPC methods.
20
20
21
-
#### Creating new RPC methods Plugin in 4 Steps
21
+
## Creating new RPC methods Plugin in 4 Steps
22
+
23
+
### Step 1: Setting Up Web3.js as a Peer Dependency and Creating a TypeScript Class
22
24
23
25
1. First add web3.js as peer dependency in project´s `package.json` and create a TypeScript class for your plugin. This class should extend the `Web3Plugin` class provided by web3.js.
24
26
25
27
:::info
26
28
This will give your plugin access to [requestManager](/api/web3-core/class/Web3Context#requestManager) and [accountProvider](/api/web3-core/class/Web3Context#accountProvider).
// plugin has access to web3.js internal features like request manager
138
-
method: 'custom_rpc_method',
139
-
params: [],
140
-
});
141
-
}
148
+
public pluginNamespace ='customRpcMethods';
149
+
150
+
//highlight-start
151
+
publicasync customRpcMethod() {
152
+
// step 3
153
+
returnthis.requestManager.send({
154
+
// plugin has access to web3.js internal features like request manager
155
+
method: 'custom_rpc_method',
156
+
params: [],
157
+
});
158
+
}
159
+
//highlight-end
142
160
}
143
161
```
144
162
145
163
</TabItem>
146
164
</Tabs>
147
165
148
-
4. Final step is setting up module [augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation), this will allow you to access plugin on web3 object.
166
+
### Step 4: Enabling Access to the Plugin on the Web3 Object
167
+
168
+
4. (For TypeScript) Final step is setting up module [augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation), this will allow you to access plugin on web3 object.
// plugin has access to web3.js internal features like request manager
187
-
method: 'custom_rpc_method',
188
-
params: [],
189
-
});
190
-
}
202
+
public pluginNamespace ='customRpcMethods';
203
+
204
+
publicasync customRpcMethod() {
205
+
returnthis.requestManager.send({
206
+
// plugin has access to web3.js internal features like request manager
207
+
method: 'custom_rpc_method',
208
+
params: [],
209
+
});
210
+
}
191
211
}
192
212
213
+
//highlight-start
193
214
// Module Augmentation
194
215
declaremodule'web3' {
195
-
// step 4
216
+
// step 4
196
217
197
-
interfaceWeb3Context {
198
-
customRpcMethods:CustomRpcMethodsPlugin;
199
-
}
218
+
interfaceWeb3Context {
219
+
customRpcMethods:CustomRpcMethodsPlugin;
220
+
}
200
221
}
222
+
//highlight-end
201
223
```
202
224
203
225
</TabItem>
@@ -207,47 +229,52 @@ declare module 'web3' {
207
229
After the plugin is ready, it is recommended to publish it on the NPM registry.
208
230
:::
209
231
210
-
#### Using Web3 Custom PRC Plugin (with web3 instance)
232
+
###Step 5: Using the Web3 `CustomRPCPlugin`with a Web3 Instance
211
233
212
234
5. First add plugin in your plugin consumer project's `package.json`, create web3 and plugin instances, and after that use `.registerPlugin` method with some web3.js module (in following example its registered with main web3).
213
235
214
236
Once plugin is registered its custom methods will be available to use.
0 commit comments