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

Error: Number can only safely store up to 53 bits #5761

Closed
1 task done
tpalaz opened this issue Jan 16, 2023 · 6 comments · Fixed by #5845
Closed
1 task done

Error: Number can only safely store up to 53 bits #5761

tpalaz opened this issue Jan 16, 2023 · 6 comments · Fixed by #5845
Assignees
Labels
1.x 1.0 related issues Bug Addressing a bug

Comments

@tpalaz
Copy link

tpalaz commented Jan 16, 2023

Is there an existing issue for this?

  • 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 tpalaz added the Bug Addressing a bug label Jan 16, 2023
@tpalaz tpalaz changed the title UnhandledPromiseRejectionWarning: Error: Number can only safely store up to 53 bits Error: Number can only safely store up to 53 bits Jan 16, 2023
@nikoulai nikoulai added the 1.x 1.0 related issues label Jan 17, 2023
@Muhammad-Altabba
Copy link
Contributor

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 Muhammad-Altabba added the Needs Clarification Requires additional input label Jan 18, 2023
@tpalaz
Copy link
Author

tpalaz commented Jan 24, 2023

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.

@Muhammad-Altabba
Copy link
Contributor

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.

@ix-hwpark
Copy link

@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 ?

@Muhammad-Altabba
Copy link
Contributor

Muhammad-Altabba commented Jan 25, 2023

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:

return numberToBN.apply(null, arguments);

And the error would not come if the line at

return toBN(value).toNumber();
would be changed return BigInt(value);. However, more tests need to be performed before updating this 6-year-old line 😄 .

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 Muhammad-Altabba added Discussion and removed Needs Clarification Requires additional input labels Jan 25, 2023
@addo47
Copy link

addo47 commented Feb 6, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
1.x 1.0 related issues Bug Addressing a bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants