-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Documentation for `Web3Context.extend` method Closes #6768
- Loading branch information
Showing
3 changed files
with
40 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
--- | ||
sidebar_position: 2 | ||
sidebar_label: Extending Web3.js | ||
--- | ||
|
||
# Extending Web3.js | ||
|
||
Although the preferred way to add custom RPC methods to Web3.js is to [create a plugin](/guides/advanced/custom_RPC), Web3.js also exposes a [legacy `extend` method](/api/web3/class/Web3Context#extend) that can be used for the same purpose. Keep reading to learn how to use the legacy `extend` method to add a custom RPC method to an instance of Web3.js. | ||
|
||
## `ExtensionObject` | ||
|
||
The legacy `extend` method accepts a single parameter that should implement the [`ExtensionObject` interface](/api/web3/namespace/core/#ExtensionObject). An `ExtensionObject` consists of two properties: an optional `string` property named `property` and a required property named `methods` that is an array of objects that implement the [`Method` interface](/api/web3/namespace/core/#Method). The `Method` interface specifies two properties, both of which are required and both of which are strings: `name` and `call`. The `property` property of an `Extension` object can be used to specify the name of the Web3.js member property that will expose the custom RPC methods (if this parameter is omitted, the new RPC methods will be exposed by the "root" Web3.js object). Each element of the `methods` array from the `ExtensionObject` specifies a new custom RPC method - the `name` property is the name of the new function that will be used to call the custom RPC method and the `call` property is the actual RPC endpoint that should be invoked. The new function will accept parameters that will be passed along when invoking the RPC endpoint. | ||
|
||
Here is a complete example of using the legacy `extend` method: | ||
|
||
```js | ||
import { Web3 } from "web3"; | ||
|
||
const web3 = new Web3("https://eth.llamarpc.com"); | ||
|
||
async function main() { | ||
web3.extend({ | ||
property: "BlockReceipts", | ||
methods: [ | ||
{ | ||
name: "getBlockReceipts", | ||
// https://www.quicknode.com/docs/ethereum/eth_getBlockReceipts | ||
call: "eth_getBlockReceipts", | ||
}, | ||
], | ||
}); | ||
|
||
const receipts = await web3.BlockReceipts.getBlockReceipts("latest"); | ||
console.log(receipts); | ||
} | ||
|
||
main(); | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
--- | ||
sidebar_position: 2 | ||
sidebar_position: 3 | ||
sidebar_label: Tree Shaking Guide | ||
--- | ||
|
||
|
973ee80
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Benchmark
processingTx
23117
ops/sec (±6.26%
)23756
ops/sec (±7.52%
)1.03
processingContractDeploy
40079
ops/sec (±7.66%
)40022
ops/sec (±7.39%
)1.00
processingContractMethodSend
15960
ops/sec (±9.63%
)16888
ops/sec (±7.21%
)1.06
processingContractMethodCall
28404
ops/sec (±6.43%
)27258
ops/sec (±8.42%
)0.96
abiEncode
43768
ops/sec (±7.10%
)44502
ops/sec (±6.85%
)1.02
abiDecode
30387
ops/sec (±8.42%
)31804
ops/sec (±6.33%
)1.05
sign
1499
ops/sec (±3.12%
)1546
ops/sec (±4.08%
)1.03
verify
368
ops/sec (±0.59%
)372
ops/sec (±0.57%
)1.01
This comment was automatically generated by workflow using github-action-benchmark.