Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Docs] Guides & Tutorials improvement #6676

Merged
merged 56 commits into from
Jan 7, 2024
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
827e8f8
restructure the folders and add category.yml to all of them for clear…
web3js-org Dec 23, 2023
6d8b2c9
created introduction file withshort packages explanation
web3js-org Dec 23, 2023
5a12855
finished quickstart guide with basic examples
web3js-org Dec 23, 2023
2723ded
finished introduction
web3js-org Dec 23, 2023
13e2bc5
delete features file (added this in introduction)
web3js-org Dec 23, 2023
7f1dbbd
fixed typo
web3js-org Dec 24, 2023
d493826
metamask (react and html) connection
web3js-org Dec 24, 2023
e3efa24
added emojis to the categories
web3js-org Dec 24, 2023
df7c5aa
separated the examples into different files
web3js-org Dec 24, 2023
b82c2da
removed transactions sections and put them into wallet sections
web3js-org Dec 28, 2023
d3fd65f
rewrote mastering wallet and accounts
web3js-org Dec 28, 2023
ea1190d
reorder and changed name of promievents
web3js-org Dec 28, 2023
bdaf7c4
rewrote local wallet tutorial
web3js-org Dec 28, 2023
07460a8
rewrote/reorganized signing tutorial
web3js-org Dec 28, 2023
d843760
added diagram of wallet and accounts for the index
web3js-org Dec 28, 2023
ff879d8
rewrote/reorganized transactions section
web3js-org Dec 28, 2023
e987cbb
added little warning to node_wallet and changed title
web3js-org Dec 28, 2023
6ef631d
restructure the examples into several tutorials
web3js-org Dec 28, 2023
74574e6
renamed file
web3js-org Dec 28, 2023
2431b06
removed CJS imports
web3js-org Dec 28, 2023
87db969
rename file and reorganized folder structure of infer guide
web3js-org Dec 28, 2023
bd75c28
removed CJS imports
web3js-org Dec 28, 2023
be739d6
renamed label
web3js-org Dec 28, 2023
c9de06d
created different file to show all the explained web3-eth methods
web3js-org Dec 28, 2023
7c1e889
fixed some typos
web3js-org Dec 28, 2023
729edc3
removed step 7 which is in another file
web3js-org Dec 28, 2023
0172bf0
removed folder and renamed the file
web3js-org Dec 28, 2023
41f3e5a
removed folder and renamed the file
web3js-org Dec 28, 2023
44afd14
fixed typos and reorganized structure of the file
web3js-org Dec 28, 2023
07674b9
fixed format of tree shaking guide
web3js-org Dec 28, 2023
8462b22
organized typos/structure but didn't change the code at all
web3js-org Dec 29, 2023
671bd49
added showcase section and rephrase the intro
web3js-org Dec 29, 2023
344b31d
reorganized the spaces in the code samples
web3js-org Dec 29, 2023
53bf9f9
reorganized spaces in the codesamples
web3js-org Dec 29, 2023
6eb8834
reorganized spaces in the codesamples
web3js-org Dec 29, 2023
5d1cf14
changed headings
web3js-org Dec 29, 2023
b2bc267
removed glossary in the sidebar and added it to guides and tutorials
web3js-org Dec 29, 2023
99ea8e4
Revert "removed glossary in the sidebar and added it to guides and tu…
web3js-org Jan 2, 2024
3c63118
fixed broken links
web3js-org Jan 2, 2024
5259eff
fixed broken links x2
web3js-org Jan 2, 2024
46b6958
shorten name to have it only in 1 line
web3js-org Jan 3, 2024
bbc89ab
add info about personal api is deprecated
web3js-org Jan 3, 2024
191ff37
renamed web3eth without dash to be consistent
web3js-org Jan 3, 2024
c933273
added parenthesis
web3js-org Jan 3, 2024
7309bb5
added link to web3js/plugins
web3js-org Jan 3, 2024
2801bd0
deleted glossary tab since it was added to the guides section
web3js-org Jan 4, 2024
3a85fa5
Revert "deleted glossary tab since it was added to the guides section"
web3js-org Jan 4, 2024
cb6c434
changed all double-quotes to single-quotes
web3js-org Jan 4, 2024
09d41f6
organized pre-requisites for each section
web3js-org Jan 7, 2024
7a6cde1
separated injected provider from truffle file
web3js-org Jan 7, 2024
f74cd34
changed title from truffle to third party provider
web3js-org Jan 7, 2024
53a62ed
replaced double quotes for single quotes
web3js-org Jan 7, 2024
73d2630
Update docs/docs/guides/wallet/signing.md
Jan 7, 2024
fcf45e9
Update docs/docs/guides/wallet/transactions.md
Jan 7, 2024
31e14a0
Merge branch 'guides_improvement' of github.com:web3/web3.js into gui…
web3js-org Jan 7, 2024
f06bbce
changed main header in for `Truffle`
web3js-org Jan 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/docs/guides/advanced/_category_.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
label: 'Advanced'
label: '🧠 Advanced'
collapsible: true
collapsed: true
link: null
position: 5
position: 11
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
---
sidebar_position: 0
sidebar_position: 1
sidebar_label: Add custom RPC methods
---

Expand All @@ -8,7 +8,7 @@ import TabItem from '@theme/TabItem';

# Add custom RPC methods

#### Introduction
## 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.

Expand All @@ -18,27 +18,30 @@ In Web3.js 1.x, `web3.extend()` function could be used to add new JSON-RPC metho

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
## Creating new RPC methods Plugin in 4 Steps

### Step 1: Setting Up Web3.js as a Peer Dependency and Creating a TypeScript Class

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.

:::info
This will give your plugin access to [requestManager](/api/web3-core/class/Web3Context#requestManager) and [accountProvider](/api/web3-core/class/Web3Context#accountProvider).
:::caution

:::

<Tabs groupId="prog-lang" queryString>

<TabItem value="javascript" label="JavaScript"
attributes={{className: "javascript-tab"}}>

```javascript
const { Web3PluginBase } = require('web3');
const { Web3PluginBase } = require("web3");
SantiagoDevRel marked this conversation as resolved.
Show resolved Hide resolved

//highlight-start
class CustomRpcMethodsPlugin extends Web3PluginBase {
// step 1
// ...
// step 1
// ...
}
//highlight-end

module.exports = CustomRpcMethodsPlugin;
```
Expand All @@ -49,17 +52,21 @@ module.exports = CustomRpcMethodsPlugin;
attributes={{className: "typescript-tab"}}>

```typescript
import { Web3PluginBase } from 'web3';
import { Web3PluginBase } from "web3";

//highlight-start
export default class CustomRpcMethodsPlugin extends Web3PluginBase {
// step 1
// ...
// step 1
// ...
}
//highlight-end
```

</TabItem>
</Tabs>

### Step 2: Adding a Public `pluginNamespace` Property to the Plugin Class

2. After that add public `pluginNamespace` property. This will be used to access your plugin, as mentioned in step number 5 code example.


Expand All @@ -69,10 +76,12 @@ export default class CustomRpcMethodsPlugin extends Web3PluginBase {
attributes={{className: "javascript-tab"}}>

```javascript
const { Web3PluginBase } = require('web3');
const { Web3PluginBase } = require("web3");

class CustomRpcMethodsPlugin extends Web3PluginBase {
pluginNamespace = 'customRpcMethods'; // step 2
//highlight-start
pluginNamespace = "customRpcMethods"; // step 2
//highlight-end
}

module.exports = CustomRpcMethodsPlugin;
Expand All @@ -84,16 +93,21 @@ module.exports = CustomRpcMethodsPlugin;
attributes={{className: "typescript-tab"}}>

```typescript
import { Web3PluginBase } from 'web3';
import { Web3PluginBase } from "web3";

export default class CustomRpcMethodsPlugin extends Web3PluginBase {
public pluginNamespace = 'customRpcMethods'; // step 2
//highlight-start
public pluginNamespace = "customRpcMethods"; // step 2
//highlight-end
}
```

</TabItem>
</Tabs>


### Step 3: Creating Custom RPC Methods in the Plugin Class

3. Once plugin class is created using above mentioned steps, its very easy to add new RPC methods like:

<Tabs groupId="prog-lang" queryString>
Expand All @@ -102,19 +116,21 @@ export default class CustomRpcMethodsPlugin extends Web3PluginBase {
attributes={{className: "javascript-tab"}}>

```javascript
const { Web3PluginBase } = require('web3');
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: [],
});
}
pluginNamespace = "customRpcMethods";

//highlight-start
async customRpcMethod() {
// step 3
return this.requestManager.send({
// plugin has access to web3.js internal features like request manager
method: "custom_rpc_method",
params: [],
});
}
//highlight-end
}

module.exports = CustomRpcMethodsPlugin;
Expand All @@ -126,45 +142,49 @@ module.exports = CustomRpcMethodsPlugin;
attributes={{className: "typescript-tab"}}>

```typescript
import { Web3PluginBase } from 'web3';
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: [],
});
}
public pluginNamespace = "customRpcMethods";

//highlight-start
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: [],
});
}
//highlight-end
}
```

</TabItem>
</Tabs>

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.
### Step 4: Enabling Access to the Plugin on the Web3 Object

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.

<Tabs groupId="prog-lang" queryString>

<TabItem value="javascript" label="JavaScript"
attributes={{className: "javascript-tab"}}>

```javascript
const { Web3PluginBase } = require('web3');
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: [],
});
}
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;
Expand All @@ -176,28 +196,30 @@ module.exports = CustomRpcMethodsPlugin;
attributes={{className: "typescript-tab"}}>

```typescript
import { Web3PluginBase } from 'web3';
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: [],
});
}
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: [],
});
}
}

//highlight-start
// Module Augmentation
declare module 'web3' {
// step 4
declare module "web3" {
// step 4

interface Web3Context {
customRpcMethods: CustomRpcMethodsPlugin;
}
interface Web3Context {
customRpcMethods: CustomRpcMethodsPlugin;
}
}
//highlight-end
```

</TabItem>
Expand All @@ -207,7 +229,7 @@ declare module 'web3' {
After the plugin is ready, it is recommended to publish it on the NPM registry.
:::

#### Using Web3 Custom PRC Plugin (with web3 instance)
### Step 5: Using the Web3 `CustomRPCPlugin` with a Web3 Instance

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).

Expand All @@ -219,14 +241,16 @@ Once plugin is registered its custom methods will be available to use.
attributes={{className: "javascript-tab"}}>

```javascript
const { Web3 } = require('web3');
const CustomRpcMethodsPlugin = require('web3-plugin-example');
const { Web3 } = require("web3");
const CustomRpcMethodsPlugin = require("web3-plugin-example");

const web3 = new Web3('http://127.0.0.1:8545');
const web3 = new Web3("http://127.0.0.1:8545");

//highlight-start
web3.registerPlugin(new CustomRpcMethodsPlugin()); // step 5

web3.customRpcMethods.customRpcMethod();
// ...
web3.customRpcMethods.customRpcMethod(); //usage
//highlight-end
```

</TabItem>
Expand All @@ -235,19 +259,22 @@ web3.customRpcMethods.customRpcMethod();
attributes={{className: "typescript-tab"}}>

```typescript
import { Web3 } from 'web3';
import CustomRpcMethodsPlugin from 'web3-plugin-example';
import { Web3 } from "web3";
import CustomRpcMethodsPlugin from "web3-plugin-example";

const web3 = new Web3("http://127.0.0.1:8545");

const web3 = new Web3('http://127.0.0.1:8545');
//highlight-start
web3.registerPlugin(new CustomRpcMethodsPlugin()); // step 5

web3.customRpcMethods.customRpcMethod();
web3.customRpcMethods.customRpcMethod(); //usage
//highlight-end
```

</TabItem>
</Tabs>

#### More Details of Plugin Feature
## More Details of Plugin Feature

For more details follow :

Expand Down
Loading
Loading