[Ethereum] TypeError: Cannot read property ‘abiDefinition’ of undefined

solidityweb3js

When I run my code ,it told me that

“TypeError: Cannot read property 'abiDefinition' of undefined”

The error code is:

console.log(calcCompiled["info"]["abiDefinition"]);

The code that I am using is:


let source = "
pragma solidity ^0.4.0;

contract Calc { 
  uint count; 

  function add(uint a, uint b) returns(uint){ count++; return a + b; } 
  function getCount() constant returns (uint){ return count; }}

"; 

let calcCompiled = web3.eth.compile.solidity(source); console.log(calcCompiled); 
console.log("ABI definition:"); 
console.log(calcCompiled["info"]["abiDefinition"]); 

What should I do?

Best Answer

The method eth_compileSolidity which web3.eth.compile.solidity relies on is deprecated and does not exist nor is available anymore.

An alternative is to compile using solc:

solc --abi Calc.sol -o ./build

which generates the ABI file:

├── Calc.sol
├── build
│   └── Calc.abi

Or compiling with solc in node.js:

const fs = require('fs')
const solc = require('solc')

const input = fs.readFileSync('Calc.sol')
const output = solc.compile(input.toString(), 1)
const abi = output.contracts[':Calc'].interface

console.log(abi)
Related Topic