1

Here's my main.js file:

require.config({
    paths: {
        angular: '//ajax.googleapis.com/ajax/libs/angularjs/1.5.5/angular.min',
        web3: 'lib/web3',
        MainController: 'controllers/MainController',
        mainService: 'services/mainService'

    },
    shim: {
        "angular": {
            exports: "angular"
        }
    }
});

require(['web3', 'angular', 'utils/init'],
    function() {    
        web3 = new Web3();
        web3.setProvider(new web3.providers.HttpProvider('http://localhost:8545'));
        angular.bootstrap(document, ['app']);       
    }
);

So, I've succesfully connected to Geth, because I can obtain my account address using web3.eth.getAccounts(...).

The problems arise when I try to check my balance:

var getBalance = function(address){ 
    web3.eth.getBalance(address, gotBalance);   
    function gotBalance(error, wei){
        console.log(wei);               
    }
}

I get the following error:

enter image description here

I've tried calling some other methods that use BigNumber library under the hood and I get the same error.

Any ideas how to fix this issue?

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               nr.isInteger            nr.lte                  nr.pow                  nr.sub                  nr.toNumber             
nr.absoluteValue        nr.div                  nr.floor                nr.isNaN                nr.minus                nr.precision            nr.times                nr.toPower              
nr.add                  nr.divToInt             nr.greaterThan          nr.isNeg                nr.mod                  nr.round                nr.toDigits             nr.toPrecision          
nr.c                    nr.dividedBy            nr.greaterThanOrEqualTo nr.isNegative           nr.modulo               nr.s                    nr.toExponential        nr.toString             
nr.ceil                 nr.dividedToIntegerBy   nr.gt                   nr.isZero               nr.mul                  nr.sd                   nr.toFixed              nr.trunc                
nr.cmp                  nr.dp                   nr.gte                  nr.lessThan             nr.neg                  nr.shift                nr.toFormat             nr.truncated            
nr.comparedTo           nr.e                    nr.isFinite             nr.lessThanOrEqualTo    nr.negated              nr.sqrt                 nr.toFraction           nr.valueOf              
nr.constructor          nr.eq                   nr.isInt                nr.lt                   nr.plus                 nr.squareRoot           nr.toJSON 

To import BigNumbers in your code install package bignumbers.js:

# npm install -g bignumber.js

and then include it in your source file:

var BigNumber = require('bignumber.js');
  • Thank you very much, good sir!!! How do I avoid the check? – manidos Aug 10 '16 at 8:33
  • Treat all returned values as BigNumbers and use their methods to work with their value (see the edited answer). – Sebi Aug 10 '16 at 8:34
  • I don't have access to BigNumber function. To be precise, I don't know how to import it from web3.js file. It's not in global scope – manidos Aug 10 '16 at 8:37
  • var BigNumber = require('bignumber.js'); – Sebi Aug 10 '16 at 8:38
  • I've added BigNumber = require('bignumber.js'); console.log(BigNumber); after web3.setProvider(...). In my logs I now have an empty object. And the error persists if I query my balance. Note, that the error doesn't come from my code, it's an error inside the web3. It's as if BigNumber wasn't initiated in the web3 itself for some reason. – manidos Aug 10 '16 at 8:47
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

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.