1

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?

3
  • What you should do is format your post, provide more code so that we know what you are doing. Commented Nov 18, 2017 at 10:04
  • here is my code: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"]);
    – XFL
    Commented Nov 18, 2017 at 13:18
  • Do you have a proper web3 object?
    – gisdev_p
    Commented Nov 19, 2017 at 8:35

2 Answers 2

1

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)
0

if you are using the latest version of solidity, Use the Following code syntax

const path = require('path'); //used for getting the path of the file


const fs = require('fs');//used for interacting with the test network

const solc = require('solc');//solc is a compiler for solidity

const CalcPath = path.resolve(__dirname, 'contracts', 'Calc.sol'); //path to the contract

const CalcSource = fs.readFileSync(CalcPath, 'utf8'); //read the file



 const input = {
 language: "Solidity",
 sources: {
  "Calc.sol": { //name of the file
  content: LottrySource,
  },
 },
 settings: {
   outputSelection: {
     "*": {
       "*": ["*"],
     },
   },
  },
 };
 module.exports = JSON.parse(solc.compile(JSON.stringify(input))).contracts[
    "Calc.sol"
  ].Calc; //compile the contract and return the abi and evm

Your Answer

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

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