4

I'm trying to create a contract object with web3.

      var contract_abi = result['contracts']['Test']["interface"];

//[{"constant":false,"inputs":[],"name":"inc","outputs":[],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"odd","type":"bool"},{"indexed":false,"name":"x","type":"uint256"}],"name":"Incremented","type":"event"}];

var contract = new web3.eth.Contract(contract_abi);

However I keep getting the error:

Uncaught Error: You must provide the json interface of the contract when instantiating a contract object.

However if I copy my contract_abi console output and directly create a variable like so:

var contract_abi =
[{"constant":false,"inputs":[],"name":"inc","outputs":[],"payable":false,"type":"function"},{"inputs":[],"payable":false,"type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"name":"odd","type":"bool"},{"indexed":false,"name":"x","type":"uint256"}],"name":"Incremented","type":"event"}];

It works. Whats going on? Thanks

6

I am not sure if your abi is correct.But I am writing the way of interacting with smart contract and generating abi from source code.

Generating abi from Source code:

  • Paste your code in remix-online solidity compiler.
  • Click on start to compile to compile your code.
  • If your code is compiled successfully. CLick on Details tab on upper right corner
  • You will a list of properties including abi, copy the abi from there.

Accessing contract using web3:

  • Initialise web3 instance

  • Use following code to get an instance of contract.

//For web3 versions 0.20.x
var contract = web3.eth.contract(contractAbi).at(contractAddress);
//For web3 1.0.x
var contract = new web3.eth.Contract(contractAbi,contractAddress);

Hope this helps!

  • 2
    Please, which version of web3? The method contract doesn't exist in the latest at least, it isn't listed in the API docs. There's only the Contract class, so the new keyword is needed, just as @user339946 used in the question description. – Mateus Pires Feb 18 '18 at 20:32
2

I just ran into this issue and was able to resolve it by running JSON.parse() on the interface result, so in your case it would be something like:

var contract_abi = JSON.parse(result['contracts']['Test']["interface"])

the reason that copying the output of your command line and setting it to the variable is working is because when you copy it, you technically change it from a string to an object, which is what JSON.parse would do :)

  • This should be the accepted answer. – Eddy Aug 27 '18 at 6:34
1

For me I was not retrieving the abi object from the compiled json file.

this worked:

var erc20ABI = JSON.parse(fs.readFileSync(file+"DummyERC20.json"));
var abi = erc20ABI.abi #essential
ERC20DummyContract = new web3.eth.Contract(erc20ABI, erc20Address);
  • Adding .abi fixed it for me on 1.0.0-beta.37 – edzillion Feb 11 at 10:30
0

I have had the same issue. If you are using web3@1.0 and JavaScript, you probably need to run your ABI with JSON.parse().

Check this link

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.