24

result is a BigNumber object that is stringified to something like { [String: '5'] s: 1, e: 0, c: [ 5 ] }. You can use BigNumber methods, like result.toNumber() to see it better. When integers are involved, web3.js uses BigNumber because native Javascript numbers are not large enough. It's out of scope here to go into the internals of a general external ...


23

If your token have 18 decimals like an ether you can use then(function(balance) { console.log(web3.fromWei(balance.toNumber(), "ether" ) ); }) Use fromWei to convert your bigNumber to ether and then convert it to Number. Be aware that unnecessary 0 after the floating point will not be displayed. In web3 ^1.0 it may change to web3.utils.fromWei ...


14

We get these occasionally when we make a change to MyEtherWallet, and we don't use Solidity or web3. It's a JS thing in the BigNumber.js library that web3 also uses. Somewhere you have something that you think is a number but it's actually a string, null, array, object, or something else. This was some code for sending a signed TX via the offline tab. The ...


10

Due to the limitation imposed by Number.MAX_SAFE_INTEGER, try to stick to the following rules: Rule #1: Avoid using toNumber() on any value returned from a contract function whose return-value type is one of the following: uint64 uint128 uint256 This also applies for public variables (since the compiler generates implicit getter functions for them). ...


6

You can use exponential number syntax in javascript to shorten you code. Example (18 decimals): const balance = getBalance().toNumber() / 1E18 PS. Sorry, i can not make comment due to low reputation.


6

Summary 10 finney is equals to 0.01 ethers or 10000000000000000 wei. Refer to http://ether.fund/tool/converter. The native units in geth, solidity and the web3 interface is in wei. Floating point numbers are currently unsupported. Your code is trying to perform that following conditional check if (rate % 10000000000000000 != 0 || rate == 0) The condition ...


6

Note that I believe this is fixed in the latest v3.0.0 of web3 which isn't out yet. This issue is now occuring for Binance Smart Chain. In the meantime, this fix worked for me: https://github.com/ChainSafe/web3.js/pull/3948#issuecomment-821779691


5

You could estimate the gas by using web3.eth.estimateGas to get an estimate of gas used by your transactions and then you can subtract the estimated gas from total balance to get you valueTosend. transactionObject = { from: sender, to: receiver, value: totalBalance } var estimatedGas = web3.eth.estimateGas(transactionObject); web3.eth.getGasPrice(...


5

If you are sending to an address that is not a contract the gas is always 21000. For a contract you can estimate with web3.eth.estimateGas. It calculates the gas executing the contract locally, so when the transaction is mined the result can be different, usually you add an extra to make sure it will work. But I think Ethereum developers are aware of the ...


4

Sending To A Regular Account This solution is for sending the entire balance of an account to another regular account (not a contract account), where the gas is always 21,000. The important part in the answer below is to use BigNumber mathematics, or you will sometimes end up with incorrect numbers and your transaction will be rejected, or you will be left ...


4

I would try to recreate the operations with an array of uint128. To make it simpler, let's say we want to do it for 512-bit integers: // With the large number = num[0] + 2^128 * num[1] + 2^256 * num[2] + 2^384 * num[3] function add(uint128[4] num1, uint128[4] num2) public pure returns(uint128[4] sum) { uint256[4] memory intermediaries; ...


4

The signature of the toWei method is (can be found in the docs. web3.utils.toWei(number [, unit]) number - String|BN: The value. unit - String (optional, defaults to "ether"): The ether to convert from. So you should provide the number as either a String or a BigNumber (Web3 0.2x) / BN (Web3 1.x). Your code should then be: let result = await instance....


4

The ethers.js library uses BN.js internally for its maths, but the BigNumber class that is exposed serializes all values as immutable strings, and uses Object.defineProperty to ensure the resulting object is completely immutable. So, what you are seeing neither BN.js or bignumber.js. A custom implementation would probably be the best way to describe it, but ...


4

I was able to use ethers.utils.formatEther( value ) ⇒ string to convert back to readable.


3

Contract function calls in Truffle return promises so you have to add a callback to get the response. If getAge() is declared as either constant, pure or view in the Solidity contract you can do instance.getAge().then(age => age.toNumber()) if it doesn't have any of those modifiers then you need to do instance.getAge.call().then(age => age.toNumber())


3

"1ee8ce2ce2ba93bc60" in hexadecimal is 570177717225592700000 in decimal. ETH has 18 "decimal places", so your number is 570177717225592700000 / 10^18. In the geth console, you can type the following to get the number: > new BigNumber("1ee8ce2ce2ba93bc60", 16) 570177717225592700000 And you can shift the "decimal places" 18 places using the following ...


3

Are you using Quorum? If you are, this is probably because you are using RAFT consensus algorithm, you have to amend web3 or change to IBFT


3

Web3.js uses the BigNumber library since JavaScript does not do a good job handling large numbers correctly. Take a look at the following examples on how you can recover the value you expect: var val; web3.eth.getBalance('0x90b0137bf6ddb130ed8e3f28cc9f929b0b609ed2', function (error, wei) { if (!error) { val = wei } }) val > Object { s: 1, e: 17, c: (2) ...


3

I also faced the similar issue, the possible reason could be, you are entering any parameter wrong. Please check if you are providing correct values of: Contract ABI Contract Address from (address) to (address) amount Take care of any typo. As in my case, I wrote form instead of from.


3

It depends what you mean by real number. You can check out here the divisions used in Ethereum. For wei: let bigNo = await this.crowdsale.rate(); console.log(bigNo.toNumber()); For ether add this: console.log(web3.fromWei(bigNo.toNumber())); If you're using version ^1.0.0 of web3, use web3.utils.fromWei instead of web3.fromWei.


3

The limitation of 2^256 storage spaces is a hard limit. It's the limit that the EVM allows, the opcodes for storage simply can't accept parameters larger than it. That aside, I can't think of any reason why you would need even close to 2^256 storage slots. 2^256 is 3.7 × 10^66 terabytes of storage. Even if you had a use for all that storage it would take 2....


3

I don't fully understand the question but I think this might help. JavaScript has no type for a 256-bit integer so web3.js includes the bignumber.js library. Web3.js 1.0: https://web3js.readthedocs.io/en/1.0/web3-utils.html?highlight=bignumber#bn Web3.js 0.x: https://github.com/ethereum/wiki/wiki/JavaScript-API#a-note-on-big-numbers-in-web3js To avoid ...


3

To do this in pure chai, you can use these two approaches: const expected = web3.utils.toBN('123.052'); const actual = await meta.getBalance.call(account_two); expect(actual).to.eql(expected); // compare to BigNumber or use strings: const actual = (await meta.getBalance.call(account_two)).toString(); expect(actual).to.equal('123.052'); Note that you have ...


3

To generate a number in the entire uint256 range ethers.BigNumber.from(ethers.utils.randomBytes(32)) You can add function wrappers around that if you need it in a certain range such as function randomBN(max) { return ethers.BigNumber.from(ethers.utils.randomBytes(32)).mod(max); }


2

You should call thus: myContractInstance.send( "0xed7a39b8cd4c217f99af29b5e5d7cbfd3c53030d", // Better prefix with 0x 100, // Just a number can work { from: tokenOwner }); // Rest of the transaction elements, if necessary.


2

The problem is that both requireJS and web3 declare variables define and require in global scope. Naming collision is the cause. I've managed to make my code to work, but it's an ugly solution and I will post something better here later. If you want to learn about renaming these variables check out RequireJS docs


2

Calls in web3 always return BigNumber instances for every number returning method. The fix is to avoid the check and treat all returned values as BigNumbers. Edit: Here's an example of how to create an instance of BigNumber: var nr = new BigNumber("10"); and all available methods: nr.abs nr.decimalPlaces nr.equals ...


2

instead of asking others first you could have tried yourself. Anyway, I have tried it out for you and the answer is: Yes Yes Here is the test: As you write out the numbers on MyEtherWallet the text-box stroke kindly becomes green thus hinting that the operation will be successful. As you generate the transaction, you can clearly read the number 1E-17 ...


2

I upvoted the above answer but it will soon be out of date because web3.js in v1.0 exposes the padding functions. It's better not to reinvent the wheel. So my code looks like this: topic = '0x' + web3.utils.padLeft(new BigNumber(someNumber).toString(16), 64);


2

Personally, I like to work with big numbers and not parse them into "normal" numbers. I guess I'm afraid some overflow may happen if I don't. Therefore, if a contract returns a uint, then I parse the return value to a BigNumber (from bignumber.js), like so: const balanceBefore = new BigNumber(await contract.getBalance(...)); and I like to use the ...


Only top voted, non community-wiki answers of a minimum length are eligible