web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

Error: Number can only safely store up to 53 bits

Open tpalaz opened this issue 3 years ago • 6 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Current Behavior

When trying to retreive a block, I am getting the error "Number can only safely store up to 53 bits".

The full call stack:

  Error: Number can only safely store up to 53 bits
    at assert (node_modules\number-to-bn\node_modules\bn.js\lib\bn.js:6:21)
    at BN.toNumber (node_modules\number-to-bn\node_modules\bn.js\lib\bn.js:506:7)
    at Object.hexToNumber (node_modules\web3-utils\lib\utils.js:207:24)
    at outputTransactionFormatter (node_modules\web3-core-helpers\lib\formatters.js:206:20)
    at node_modules\web3-core-helpers\lib\formatters.js:282:24
    at Array.forEach (<anonymous>)
    at Method.outputBlockFormatter [as outputFormatter] (node_modules\web3-core-helpers\lib\formatters.js:280:28)
    at Method.formatOutput (node_modules\web3-core-method\lib\index.js:148:54)
    at sendTxCallback (node_modules\web3-core-method\lib\index.js:536:33)
    at node_modules\web3-core-requestmanager\lib\index.js:305:9
    at node_modules\web3-providers-http\lib\index.js:124:13
    at processTicksAndRejections (internal/process/task_queues.js:95:5)

Expected Behavior

The block should resolve and not error when trying to retreive. The error comes from:

"returnTransactionObjects" parameter on the "getBlock" function.

When set to True, it errors.

When set to False or not defined, it works.

Steps to Reproduce

const block = await web3.eth
  .getBlock(5000000, true)
  .then((block) => {
    console.log(block);
  }).catch(err) => {
     console.log(err);
  });

Web3.js Version

1.8.1

Environment

  • Operating System: win10
  • Browser: N/A
  • Node.js Version: 14.18.0
  • NPM Version: 7.5.2

Anything Else?

No response

tpalaz avatar Jan 16 '23 21:01 tpalaz

Thanks @tpalaz for opening the issue, The error seems to come from an edge case for numbers. So, could you please tell if this error came always with all blocks or only with some block(s)? And could you please manage to share with us the full data of the transactions at that block? However, to get the data, you may try using the latest version of 4.x (4.0.1-alpha.5). And/or you may try with an older version for web3 like 1.7.0, 1.5.0 or even far old like 1.2.0. Because sharing the data would let us be able to investigate this issue further. Thanks in advance,

Muhammad-Altabba avatar Jan 18 '23 15:01 Muhammad-Altabba

Thanks @tpalaz for opening the issue, The error seems to come from an edge case for numbers. So, could you please tell if this error came always with all blocks or only with some block(s)? And could you please manage to share with us the full data of the transactions at that block? However, to get the data, you may try using the latest version of 4.x (4.0.1-alpha.5). And/or you may try with an older version for web3 like 1.7.0, 1.5.0 or even far old like 1.2.0. Because sharing the data would let us be able to investigate this issue further. Thanks in advance,

Tested 1.7.0, 1.5.0, 1.2.0 and of course 1.8.1 all of which encounter the same issue.

With 4.0.1-alpha.5 the issue is not present at all.

tpalaz avatar Jan 24 '23 18:01 tpalaz

Tested 1.7.0, 1.5.0, 1.2.0 and of course 1.8.1 all of which encounter the same issue.

With 4.0.1-alpha.5 the issue is not present at all.

Thanks @tpalaz The error seems to come from an edge case for numbers. So... First question: Could you please tell if this error came always with all blocks or only with some block(s)? Second question: Could you please manage to share with us the full data of the transactions at that block? You may try using version 4.0.1-alpha.5 to get the data for the transactions at that block.

Muhammad-Altabba avatar Jan 25 '23 10:01 Muhammad-Altabba

@Muhammad-Altabba same issue v1.7.4 and v.1.8.1 too getTransactionReceipt function has occur this error https://polygonscan.com/tx/0x8a9df67dd623a50806c9bed399783bccd6234a59c3379696542eae125d115998

is the only solution is change version 1.x to 4.x ?

ix-hwpark avatar Jan 25 '23 11:01 ix-hwpark

Thanks @ix-hwpark

I am able to see the error with this code:

const Web3 = require('web3');
const web3 = new Web3('https://polygon-mainnet.infura.io/v3/[INFURA_KEA]')
const ts = web3.eth.getTransactionReceipt('0x8a9df67dd623a50806c9bed399783bccd6234a59c3379696542eae125d115998') 
ts.then(console.log);

And the error that causes the issue inside web3js is: https://github.com/web3/web3.js/blob/b995b9e7cb58572c4dcc455d7b02714b8bd305ae/packages/web3-utils/src/utils.js#L60

And the error would not come if the line at https://github.com/web3/web3.js/blob/b995b9e7cb58572c4dcc455d7b02714b8bd305ae/packages/web3-utils/src/utils.js#L236 would be changed return BigInt(value);. However, more tests need to be performed before updating this 6-year-old line :smile: .

And the only fast solution, that I can think of is that you may move to version 4 if this is possible for you. However, it is still in alpha now.

Muhammad-Altabba avatar Jan 25 '23 13:01 Muhammad-Altabba

Here's how we fixed it as a workaround. It's not clean, but it keeps things moving till you transition to 4.x or it's fixed on 1.x.

Use patch-package. Create a folder named patches and copy paste the following into a file named number-to-bn++bn.js+4.11.6.patch inside the patches folder.

diff --git a/node_modules/number-to-bn/node_modules/bn.js/lib/bn.js b/node_modules/number-to-bn/node_modules/bn.js/lib/bn.js
index 29a4c51..9964e7e 100644
--- a/node_modules/number-to-bn/node_modules/bn.js/lib/bn.js
+++ b/node_modules/number-to-bn/node_modules/bn.js/lib/bn.js
@@ -503,7 +503,8 @@
       // NOTE: at this stage it is known that the top bit is set
       ret += 0x10000000000000 + (this.words[1] * 0x4000000);
     } else if (this.length > 2) {
-      assert(false, 'Number can only safely store up to 53 bits');
+      // assert(false, 'Number can only safely store up to 53 bits');
+      ret = Number.MAX_SAFE_INTEGER;
     }
     return (this.negative !== 0) ? -ret : ret;
   };

In your package.json scripts, add: "postinstall": "patch-package"

npm install and run your code.

addo47 avatar Feb 06 '23 08:02 addo47