0

I am trying to get a the gas price in terms of integer.

I tried the following

// Get references to the modules (or the module content) that we need
var Web3 = require('web3');
const apiKey ='https://rinkeby.infura.io/v3/'+Project_ID_Infura
const web3 = new Web3(new Web3.providers.HttpProvider(apiKey));

// Get the gas price
a=web3.eth.getGasPrice().then(x => {return Number.parseInt(x)*1.4});

typeof(a)

the type of a is an "object" when I expect it to be an integer. Can you please help me understand how to store the gasprice as integer rather than object?

9
  • I think the gas price is BigNumber, you can convert it to string or hex using BigNumber libraries
    – Majd TL
    Sep 27 at 11:13
  • js has no integer. it has "number" and the returned value could be bigger than the biggest value in that can stored in "number". and try to use "await" instead of .then
    – Majd TL
    Sep 27 at 11:14
  • a is not a big number! as it says false to whether it is a BN! web3.utils.isBN(a) gives false! even if you write web3.utils.isBN(web3.eth.getGasPrice())
    – rsc05
    Sep 27 at 11:19
  • @MajdTL regarding your await comment the outcome is web3.eth.getGasPrice(...).await is not a function
    – rsc05
    Sep 27 at 11:20
  • do console.log(a), i think it is undefinded
    – Majd TL
    Sep 27 at 13:39
1

As KonyTech sais "web3.eth.getGasPrice() returns a Promise so you must treat it as such. In your example a is the Promise, not the gas price" (so you need to understand what are promises in JS before learning web3js :) )

When you connect to Ethereum you need to use BigNumber most of the time because js cannot handle those big numbers used in Ethereum and Solidity.

this script works for me using nodejs and web3js v1.6.0

create package.json file

{
  "name": "testApp",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "start": "node main.js",
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "web3": "^1.6.0"
  }
}

then create this file: main.js

// Get references to the modules (or the module content) that we need
var Web3 = require('web3');
const apiKey ='https://rinkeby.infura.io/v3/' + <your-key>
const web3 = new Web3(new Web3.providers.HttpProvider(apiKey));

async function main() {
     const gasPriceAsString = await web3.eth.getGasPrice();
     console.log(gasPriceAsString); // "1000000011" in WEI
     console.log(typeof gasPriceAsString); // string

     var BN = web3.utils.BN;
     const gasPriceAsBigNumber = new BN(gasPriceAsString);
     console.log(gasPriceAsBigNumber); // BN { negative: 0, words: [ 60475915, 14 ], length: 2, red: null }
     console.log(typeof gasPriceAsBigNumber); // object
     console.log(gasPriceAsBigNumber.toString()); // "1000000011"

     const MAX_NUMBER_IN_JS = Number.MAX_SAFE_INTEGER;
     console.log(MAX_NUMBER_IN_JS); // 9007199254740991
     const maxNumberInJsAsBigNumber = new BN(MAX_NUMBER_IN_JS);
     console.log(maxNumberInJsAsBigNumber); // BN { negative: 0, words: [ 67108863, 67108863, 1 ], length: 3, red: null }
     console.log(typeof maxNumberInJsAsBigNumber); // object
     console.log(maxNumberInJsAsBigNumber.toString()); // "9007199254740991"

     //compare to make sure that your big number can be convert to number (the max number in js is greate or equal to your bignumber )
     const isSafeToConvert = maxNumberInJsAsBigNumber.gte(gasPriceAsBigNumber);
     if (isSafeToConvert) {
         const gasPriceAsNormalNumber = gasPriceAsBigNumber.toNumber();
         console.log(gasPriceAsNormalNumber); // 1000000011
         console.log(typeof gasPriceAsNormalNumber) // number
     }
}

// call the function
main();

then

npm install

then

npm run start

1
  • @Majid you are a genius thanks!
    – rsc05
    Oct 11 at 13:44
0

First, web3.eth.getGasPrice() returns a Promise so you must treat it as such. In your example a is the Promise, not the gas price.

The cleanest way to achieve this is to await on the Promise. Since it is asynchronous, by awaiting it you automatically get the value returned by the Promise:

// Get the gas price
a = await web3.eth.getGasPrice();

However, we're not done yet since from the web3 documentation, the Promise returns the gas price as a String. Sure, you can do a = Number.parseInt(a) to get the value as a number but this is dangerous.

If the value exceeds the range of safe values to use in JavaScript (here Number.MAX_SAFE_INTEGER) this will most likely create problems in your applications. For this purpose it's better to use one of the existing BigNumber JS libraries that exist and parse the String to a BigNumber instead.

Example using web3.utils:

a = web3.utils.toBN(a)
1
  • @KonyTeck Thanks for this as I did what you told me it give me this error "Error: Error: [number-to-bn] while converting number {} to BN.js instance, error: invalid number value."
    – rsc05
    Oct 9 at 10:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.