4

I am trying to do some gas transaction cost calculations in a karma test to assert the final balance and I can not understand why the output of this two code snippets is different

Values for variables in order are:

59916559960000000000 3000000000000000000 394980000000000

And the snippets are:

let currentBalance =  web3.utils.fromWei(customerBalance.toString(), 'ether')  
         + web3.utils.fromWei(customerRefundableEther.toString(), 'ether') 
         - web3.utils.fromWei(transactionFee.toString(), 'ether');

let currentBalance = (customerBalance / 1e18)
                     +(customerRefundableEther / 1e18) 
                     - (transactionFee / 1e18);

The second snippet is the correct balance at the user account and the assert is successful. Is not the conversion from wei to ether: value / 1e18?. I can't understand why but the difference between this snippets are more than 3 ether units.

I am using web3 version 1.0.0-beta26.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Carlos Landeras
  • 11,025
  • 11
  • 56
  • 82

1 Answers1

4

I believe the issue is that web3.utils.fromWei returns a string, and + for strings performs concatenation.

Maybe just do web3.utils.fromWei(customerBalance + customerRefundableEther - transactionFee, 'ether')?

EDIT

It appears maybe customerBalance et al. are BigNumber instances. In that case:

web3.utils.fromWei(customerBalance.add(customerRefundableEther)
  .sub(transactionFee).toString(), 'ether')

EDIT 2

Working code with numbers:

> const customerBalance = 59916559960000000000;
> const customerRefundableEther = 3000000000000000000;
> const transactionFee = 394980000000000;
> web3.utils.fromWei((customerBalance + customerRefundableEther - transactionFee).toString(), 'ether');
62.91616498000001

Working code with strings, just in case the issue is that they start off as strings:

> const customerBalance = '59916559960000000000';
> const customerRefundableEther = '3000000000000000000';
> const transactionFee = '394980000000000';
> web3.utils.fromWei(web3.utils.toBN(customerBalance).add(web3.utils.toBN(customerRefundableEther)).sub(web3.utils.toBN(transactionFee)), 'ether')
'62.91616498'
user94559
  • 59,196
  • 6
  • 103
  • 103
  • Doing that I receive an error: allows to buy a flight and redeem loyalty points: Error: [number-to-bn] while converting number "5.991655996e+38" to BN.js instance, error: invalid number value. Value must be an integer, hex string, BN or BigNumber instance. Note, decimals are not supported. Gonna try converting to hex – Carlos Landeras Mar 22 '18 at 08:15
  • Oh, sorry, are `customerBalance` etc. `BigNumber` instances? – user94559 Mar 22 '18 at 08:17
  • If so, `web3.utils.fromWei(customerBalance.add(customerRefundableEther).sub(transactionFee).toString(), 'ether')`. – user94559 Mar 22 '18 at 08:18
  • But the 1.0.0 api is giving balances as numbers, they have no methods. – Carlos Landeras Mar 22 '18 at 08:20
  • So what type are `customerRefundableEther`, etc.? – user94559 Mar 22 '18 at 08:21
  • Also where did `5.991655996e+38` come from? All the values are integers, right? Is this an error you're getting *after* the conversion from wei to ether? – user94559 Mar 22 '18 at 08:23
  • Yeah the sum in console is 62916954940000000000, when using let currentBalance = web3.utils.fromWei( (customerBalance + customerRefundableEther - transactionFee).toString(), 'ether'); it results in "5.991655996e+38" and the error. :/ – Carlos Landeras Mar 22 '18 at 08:26
  • It gives you a result _and_ an error? What is "the sum in console"? Could you share the full code you're running and what line results in the error? – user94559 Mar 22 '18 at 08:27
  • In fact, this.... console.log( (customerBalance + customerRefundableEther - transactionFee).toString()); is adding the decimal point – Carlos Landeras Mar 22 '18 at 08:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/167319/discussion-between-carlos-landeras-and-smarx). – Carlos Landeras Mar 22 '18 at 08:29
  • You haven't answered my question of what type the values are. I've added a couple edits to show what to do if they're `BigNumber`s, numbers, or strings. If none of those work, you'll really have to tell me what type they are. :-) – user94559 Mar 22 '18 at 08:30