My Image
CoursesQuizzesProblemsContestsSmartBooks
Contest!

No results found

LOGINREGISTER
My ProgressCoursesQuizzesProblemsContestsSmartbooks
Published on 13 Aug 2021
Extend Web3 module
Web3 lets developers to add required RPC methods in their working web3 module
img
Aniket Savji
0
Like
304

Introduction

Extension object from web3 will add rpc methods into current web3 module of the dapp.  There are many rpc calls that are not added as a method in web3 but they provide extension object with which we can add required rpc methods/calls into our working web3 module or instance.

 

Web3.extend

1. Parameters

  • property  -  [ String ] (optional) : The name of the property to add to the module. If no property is set it will be added to the module directly.
  • methods  -  It must be an array of method descriptions as follows :
    • name [ String ]  :  Name of the method to add.
    • call [ String ]  :  The RPC method name.
    • params [ Number ] (optional)  :  The number of parameters for that function. Default is always set to 0.
    • inputFormatter [ Array ] (optional)  :  Array of inputformatter functions. Each array item responds to a function parameter, so if you want some parameters not to be formatted, add a null instead.
    • outputFormatter [ Function ] (optional)  :  Used to format the output of the method.

2. Code Example

Let's add get balance and gas price super  RPC methods in our web3 module

const Web3 = require("web3");

Web3.extend({
    property: 'myModule',
    methods: [{
        name: 'getBalance',
        call: 'eth_getBalance',
        params: 2,
        inputFormatter: [web3.extend.formatters.inputAddressFormatter,
                          web3.extend.formatters.inputDefaultBlockNumberFormatter]
    },{
        name: 'getGasPriceSuperFunction',
        call: 'eth_gasPriceSuper',
        params: 2,
        inputFormatter: [null, web3.utils.numberToHex]
    }]
});

 

 

 

Enjoyed the SmartBook?
Like
logo
contact@dapp-world.com
Katraj, Pune, Maharashtra, India - 411048

Follow Us

linkedintwitteryoutubediscordinstagram

Products

  • SmartBooks
  • Courses
  • Quizzes
  • Assessments

Support

  • Contact Us
  • FAQ
  • Privacy Policy
  • T&C

Backed By

ah! ventures

Copyright 2023 - All Rights Reserved.

Recommended from DAppWorld
img
1 May 2021
How to connect Ganache with Metamask and deploy Smart contracts on remix without
Set up your development environment with (Metamask + Ganache + Remix) and skip truffle :)
3 min read
11494
5
img
8 Jul 2021
How to interact with smart contarct from backend node js
call and send functions from backend server side using nodejs
3 min read
8069
2
img
18 Aug 2021
Send transaction with web3 using python
Introduction to web3.py and sending transaction on testnet
3 min read
6215
5
img
5 Aug 2021
Deploy Smart Contract on Polygon POS using Hardhat
how to deploy smart contracts on polygon pos chain using hardhat both mainnet and testnet ?
3 min read
5533
3