进阶 docs.web3js.org 2026-10-19 02:32:30 · 17 阅读

第17章 进阶——为 Web3.js 添加自定义 RPC 方法

添加自定义 RPC 方法 简介 Web3.js 是一个流行的以太坊区块链交互库,提供了一整套 API,通过 JSON-RPC 调用与以太坊节点通信。如果要为库添加新的 JSON-RPC 方法调用,可以使用 web3.js 4.x 的插件功能。这让你能够扩展 Web3.js 的功能,增加对新 JSON-RPC 方法的支持。 注意:在 Web3.js 1.x 中,可以使用 web3.extend() 函数来添加新的 JSON-RPC 方法。Web3 v4.0.4+ 仍然提供 web3.extend(),但有一些破坏性变更。不过,如果你要开发新功能,建议使用 Web3 插件功能来扩展 web3 的能力。如需了解旧版 web3.extend() 方法的更多内容,请阅读"Extending Web3.js"指南。 接下来的教程将引导你创建一个自定义插件,扩展 web3.js 4.x 的功能并支持新的 RPC 方法。 四步创建新的 RPC 方法插件 第一步:将 Web3.js 配置为 Peer Dependency 并创建 TypeScript 类 首先在项目的 package.json 中把 web3.js 添加为 peer dependency,然后为插件创建一个 TypeScript 类。这个类需要继承 web3.js 提供的 Web3Plugin 类。 提示:这样你的插件就能访问 requestManager 和 accountProvider。 JavaScript const { Web3PluginBase } = require('web3');

class CustomRpcMethodsPlugin extends Web3PluginBase {
// step 1
// ...
}

module.exports = CustomRpcMethodsPlugin;
TypeScript import { Web3PluginBase } from 'web3';

export default class CustomRpcMethodsPlugin extends Web3PluginBase {
// step 1
// ...
}
第二步:为插件类添加公开的 pluginNamespace 属性 接下来添加公开的 pluginNamespace 属性,它将用于访问你的插件,具体用法见第五步的代码示例。 JavaScript const { Web3PluginBase } = require('web3');

class CustomRpcMethodsPlugin extends Web3PluginBase {
pluginNamespace = 'customRpcMethods'; // step 2
}

module.exports = CustomRpcMethodsPlugin;
TypeScript import { Web3PluginBase } from 'web3';

export default class CustomRpcMethodsPlugin extends Web3PluginBase {
public pluginNamespace = 'customRpcMethods'; // step 2
}
步骤 3:在插件类中创建自定义 RPC 方法 按照上述步骤创建好插件类后,添加新的 RPC 方法非常简单,示例如下: JavaScriptTypeScriptconst { Web3PluginBase } = require('web3');

class CustomRpcMethodsPlugin extends Web3PluginBase {
pluginNamespace = 'customRpcMethods';

async customRpcMethod() {
// step 3
return this.requestManager.send({
// 插件可以访问 web3.js 的内部功能,例如请求管理器
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({
// 插件可以访问 web3.js 的内部功能,例如请求管理器
method: 'custom_rpc_method',
params: [],
});
}
}
步骤 4:在 Web3 对象上启用插件访问权限 (针对 TypeScript)最后一步是配置模块扩展,这允许你在 web3 对象上访问该插件。 JavaScriptTypeScriptconst { Web3PluginBase } = require('web3');

class CustomRpcMethodsPlugin extends Web3PluginBase {
pluginNamespace = 'customRpcMethods';

async customRpcMethod() {
return this.requestManager.send({
// 插件可以访问 web3.js 的内部功能,例如请求管理器
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({
// 插件可以访问 web3.js 的内部功能,例如请求管理器
method: 'custom_rpc_method',
params: [],
});
}
}

// 模块扩展
declare module 'web3' {
// step 4

interface Web3Context {
customRpcMethods: CustomRpcMethodsPlugin;
}
}
提示:插件开发完成后,建议将其发布到 NPM 注册表。 步骤 5:在 Web3 实例中使用 Web3 CustomRPCPlugin 首先,在您的插件使用者项目的 package.json 中添加该插件,创建 web3 和插件实例,然后使用 .registerPlugin 方法将其与某个 web3.js 模块绑定(在以下示例中,是注册到主 web3 实例上)。 一旦插件注册完成,其自定义方法即可投入使用。 JavaScript ```javascript const { Web3 } = require('web3'); const CustomRpcMethodsPlugin = require('web3-plugin-example'); const web3 = new Web3('http://127.0.0.1:8545'); web3.registerPlugin(new CustomRpcMethodsPlugin()); // 步骤 5 web3.customRpcMethods.customRpcMethod(); // 使用示例 ``` TypeScript ```typescript import { Web3 } from 'web3'; import CustomRpcMethodsPlugin from 'web3-plugin-example'; const web3 = new Web3('http://127.0.0.1:8545'); web3.registerPlugin(new CustomRpcMethodsPlugin()); // 步骤 5 web3.customRpcMethods.customRpcMethod(); // 使用示例 ``` 插件功能详解 如需了解更多详情,请参考: * 示例插件代码 * Web3 插件开发者指南 * Web3 插件使用者指南

评论 (0)