Add custom RPC methods
Introductionβ
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.
In Web3.js 1.x, web3.extend()
function could be used to add new JSON-RPC methods. web3.extend()
is also available in Web3 v4.0.4+ with some breaking changes. However it is recommended to use Web3 Plugin feature for extending web3 functionality if you are developing new feature. Read the "Extending Web3.js" guide to learn more about the legacy web3.extend()
method.
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.
Creating new RPC methods Plugin in 4 Stepsβ
Step 1: Setting Up Web3.js as a Peer Dependency and Creating a TypeScript Classβ
- First add web3.js as peer dependency in projectΒ΄s
package.json
and create a TypeScript class for your plugin. This class should extend theWeb3Plugin
class provided by web3.js.
This will give your plugin access to requestManager and accountProvider.
- JavaScript
- TypeScript
const { Web3PluginBase } = require('web3');
class CustomRpcMethodsPlugin extends Web3PluginBase {
// step 1
// ...
}
module.exports = CustomRpcMethodsPlugin;
import { Web3PluginBase } from 'web3';
export default class CustomRpcMethodsPlugin extends Web3PluginBase {
// step 1
// ...
}
Step 2: Adding a Public pluginNamespace
Property to the Plugin Classβ
- After that add public
pluginNamespace
property. This will be used to access your plugin, as mentioned in step number 5 code example.
- JavaScript
- TypeScript
const { Web3PluginBase } = require('web3');
class CustomRpcMethodsPlugin extends Web3PluginBase {
pluginNamespace = 'customRpcMethods'; // step 2
}
module.exports = CustomRpcMethodsPlugin;
import { Web3PluginBase } from 'web3';
export default class CustomRpcMethodsPlugin extends Web3PluginBase {
public pluginNamespace = 'customRpcMethods'; // step 2
}
Step 3: Creating Custom RPC Methods in the Plugin Classβ
- Once plugin class is created using above mentioned steps, its very easy to add new RPC methods like:
- JavaScript
- TypeScript
const { Web3PluginBase } = require('web3');
class CustomRpcMethodsPlugin extends Web3PluginBase {
pluginNamespace = 'customRpcMethods';
async customRpcMethod() {
// step 3
return this.requestManager.send({
// plugin has access to web3.js internal features like request manager
method: 'custom_rpc_method',
params: [],
});
}
}
module.exports = CustomRpcMethodsPlugin;
import { Web3PluginBase } from 'web3';
export default class CustomRpcMethodsPlugin extends Web3PluginBase {
public pluginNamespace = 'customRpcMethods';
public async customRpcMethod() {
// step 3
return this.requestManager.send({
// plugin has access to web3.js internal features like request manager
method: 'custom_rpc_method',
params: [],
});
}
}
Step 4: Enabling Access to the Plugin on the Web3 Objectβ
- (For TypeScript) Final step is setting up module augmentation, this will allow you to access plugin on web3 object.
- JavaScript
- TypeScript
const { Web3PluginBase } = require('web3');
class CustomRpcMethodsPlugin extends Web3PluginBase {
pluginNamespace = 'customRpcMethods';
async customRpcMethod() {
return this.requestManager.send({
// plugin has access to web3.js internal features like request manager
method: 'custom_rpc_method',
params: [],
});
}
}
module.exports = CustomRpcMethodsPlugin;
import { Web3PluginBase } from 'web3';
export default class CustomRpcMethodsPlugin extends Web3PluginBase {
public pluginNamespace = 'customRpcMethods';
public async customRpcMethod() {
return this.requestManager.send({
// plugin has access to web3.js internal features like request manager
method: 'custom_rpc_method',
params: [],
});
}
}
// Module Augmentation
declare module 'web3' {
// step 4
interface Web3Context {
customRpcMethods: CustomRpcMethodsPlugin;
}
}
After the plugin is ready, it is recommended to publish it on the NPM registry.
Step 5: Using the Web3 CustomRPCPlugin
with a Web3 Instanceβ
- 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).
Once plugin is registered its custom methods will be available to use.
- JavaScript
- TypeScript
const { Web3 } = require('web3');
const CustomRpcMethodsPlugin = require('web3-plugin-example');
const web3 = new Web3('http://127.0.0.1:8545');
web3.registerPlugin(new CustomRpcMethodsPlugin()); // step 5
web3.customRpcMethods.customRpcMethod(); //usage
import { Web3 } from 'web3';
import CustomRpcMethodsPlugin from 'web3-plugin-example';
const web3 = new Web3('http://127.0.0.1:8545');
web3.registerPlugin(new CustomRpcMethodsPlugin()); // step 5
web3.customRpcMethods.customRpcMethod(); //usage
More Details of Plugin Featureβ
For more details follow :