0

i try to get the contract balance and im using this

var Web3 = require('web3')
var web3 = new Web3('ropsten.infura.io/v3/123456789')
var abi = abi is here ;

var contractAddress = '0x31111.....';
var contract = new web3.eth.Contract(abi, contractAddress);
//var address = '0x31111......';

web3.eth.getBalance(contract.address, function (e, r) {
   $('contractBalanceAgi').text(convertWeiToEth(r).toFixed(4))
 })

and when running this i get following error in my console web3.eth.Contract is not a constructor

when i googled for it some said its because of the new web3 js version but the new web3 version requires this var contract = new web3.eth.Contract(abi, contractAddress) new and capital C

1
  • can you try console.log("Web3 version", web3.version);? I have a hunch you are running a different Web3 version than you think. Jan 12, 2020 at 23:02

1 Answer 1

1

I'm not sure if this is the cause of your issue but I am fairly sure that you need to include the protocol prefix in the URL provided to the web3 constructor:

var web3 = new Web3('ropsten.infura.io/v3/123456789')

Should include the protocol:

var web3 = new Web3('https://ropsten.infura.io/v3/123456789')

(P.S. please don't use var, const is fairly appropriate)

7
  • didnt work mate
    – yappy twan
    Jan 8, 2020 at 22:43
  • updated my answer @yappytwan, please let me know if that sorts it out :)
    – Jay Welsh
    Jan 8, 2020 at 22:49
  • no is not working and additional i get this error now Uncaught TypeError: Cannot read property 'getAccounts' of undefined
    – yappy twan
    Jan 8, 2020 at 22:56
  • That seems like progress to me @yappytwan, please share the code making use of "getAccounts".
    – Jay Welsh
    Jan 8, 2020 at 23:36
  • Uncaught TypeError: this.provider.sendAsync is not a function at s.sendAsync (web3.js:1) at r.e [as getAccounts] (web3.js:1) at (index):70
    – yappy twan
    Jan 9, 2020 at 14:02

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.