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. Nov 18 '17 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
    Nov 18 '17 at 13:18
  • Do you have a proper web3 object?
    – gisdev_p
    Nov 19 '17 at 8:35
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
New contributor
James Harrison is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.