0

Im using web3.js to retrieve the balance for the main account on my geth server

I set:

  let provider = new web3.providers.HttpProvider(ethereumNode)
  web3.setProvider(provider)

  if (!web3.isConnected()) {
    console.error('Ethereum - no conection to RPC server')
  } else {
    // is connected
    console.log('Ethereum - connected to RPC server')
  }


web3.eth.defaultAccount = from // from is my account on geth

// When goes to this line I get  Error: invalid address 
console.log("balance ", web3.eth.getBalance()) 

Besides this, I m trying to broadcast a raw transaction where it gets the same error

Error: invalid address

however If I run the same raw transaction on https://ropsten.etherscan.io/pushTx it works fine.

any ideas what is happening? I have the feeling is a problem on my geth server.

error stack on getBalance()

 Error: invalid address
     at inputAddressFormatter (.../node_modules/web3/lib/web3/formatters.js:271:11)
    at .../node_modules/web3/lib/web3/method.js:89:28
    at Array.map (native)
    at Method.formatInput (.../node_modules/web3/lib/web3/method.js:88:32)
    at Method.toPayload (.../node_modules/web3/lib/web3/method.js:114:23)
    at Eth.send [as getBalance] (.../node_modules/web3/lib/web3/method.js:139:30)
    at .../server/src/batches/export2contract.js:80:40
    at writeContract (.../server/src/batches/export2contract.js:61:10)
    at .../server/src/batches/export2contract.js:190:5
    at Array.forEach (native)

Error stack on sendTransaction:

   Error: invalid address
    at inputAddressFormatter (.../node_modules/web3/lib/web3/formatters.js:271:11)
    at inputTransactionFormatter (.../node_modules/web3/lib/web3/formatters.js:97:20)
    at .../node_modules/web3/lib/web3/method.js:89:28
    at Array.map (native)
    at Method.formatInput (.../node_modules/web3/lib/web3/method.js:88:32)
    at Method.toPayload (.../node_modules/web3/lib/web3/method.js:114:23)
    at Eth.send [as sendTransaction] (.../node_modules/web3/lib/web3/method.js:139:30)
    at .../server/src/batches/export2contract.js:135:32
    at writeContract (.../server/src/batches/export2contract.js:61:10)
    at .../server/src/batches/export2contract.js:190:5
12
  • Well , did you try like this web3.eth.getBalance(from) ?
    – Rangesh
    Sep 14, 2017 at 7:14
  • yes and it works Sep 14, 2017 at 7:15
  • Ok...great ! Is this what you are looking for ?
    – Rangesh
    Sep 14, 2017 at 7:16
  • nope, I updated the question with more details. Sep 14, 2017 at 7:17
  • and what is from ? I assume it is an valid account address and also make sure geth is running .
    – Rangesh
    Sep 14, 2017 at 7:20

1 Answer 1

1

It looks like from is not a valid address.

Looking at the stacktrace, in web3's formatters.js


var inputAddressFormatter = function (address) {
    var iban = new Iban(address);
    if (iban.isValid() && iban.isDirect()) {
        return '0x' + iban.address();
    } else if (utils.isStrictAddress(address)) {
        return address;
    } else if (utils.isAddress(address)) {
        return '0x' + address;
    }
    throw new Error('invalid address');
};

line 271 is "utils.isAddress(address)" and utils can be found here.

You might want to try calling web3.isAddress(from) and seeing if it validates.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.