0

I am interacting with a contract on the Rinkeby testnet using web3.js (version 0.20.7) with Metamask and the chrome console.

I setup a contract object called mycontract for a given ABI and address. When I set the web3 provider for this contract using mycontract.setProvider(web3.givenProvider) I get an error saying that mycontract.setProvider is not a function.

See below the js code deployed in the chrome console:

> const mycontract = web3.eth.contract(ABI, Address);
undefined
> mycontract.setProvider(web3.givenProvider);
< VM212:1 Uncaught TypeError: simpleStorage.setProvider is not a function
    at <anonymous>:1:15

I suppose this may be due to the fact that the mycontract.setProvider(web3.givenProvider) command is for web3.js version 1.0 and above. However, if it is the case, I could not find info about what command I should use with web3.js 0.20.7.

Any help would be great.

Thank you

  • 1
    Please console.log(web3.version) inside your code and report back with the result. – goodvibration Jan 6 at 12:12
  • Here is what I am getting: > console.log(web3.version) > {api: "0.20.7", getNode: ƒ, getNetwork: ƒ, …} VM288:1 So as expected it seems to be the version 0.20.7 of web3.js. Do you know what should I do to execute the code described in my post above? Thanks – Weisel2095 Jan 7 at 11:18
0

This:

> const mycontract = web3.eth.contract(ABI, Address);
undefined

Indicates a problem in web3.eth.contract(ABI, Address), right?

You've printed web3.version and got 0.20.7, right?

So now you need to open the documentation of web3.js v0.x, and check again what it says about web3.eth.contract (right?).

If you open it (here), then you can understand from the coding example that instead of doing this:

const mycontract = web3.eth.contract(ABI, Address);

You should be doing this:

const mycontract = web3.eth.contract(ABI).at(Address);
  • Thank you for your help and the link to the example. I followed the example closely and could resolve my problems.FYI to set the web3 provider I did web3.setProvider(web3.currentProvider) (instead of mycontract.setProvider(web3.givenProvider)) and it worked. – Weisel2095 Jan 9 at 11:43
  • @Weisel2095: You're welcome. Please accept the answer by clicking on the V. – goodvibration Jan 9 at 11:44

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.