[Ethereum] Can’t compile contract – pragma error and Cannot read property ‘interface’ of undefined

abiblockchaincompilersolc

I am trying to compile this simple contract:

pragma solidity ^0.4.18;

contract Tasks
{

    struct TaskOwner {
      address TaskOwnerAddress;
      bytes32 TaskOwnerName;
    }
    mapping (address => TaskOwner) public theTaskOwner;

    //Task owner information
    address TaskOwnerAddress;
    bytes32 TaskOwnerName;

    modifier onlyOwner(){
      if(msg.sender != TaskOwnerAddress) revert();
      _;
    }

    event NewTaskStarted(bytes32 TaskOwnerName, address TaskOwnerAddress);


    function Tasks(bytes32 TaskOwnerName) {

        //establish the owner
        TaskOwnerAddress = msg.sender;
        TaskOwnerName    = TaskOwnerName;

        //fire a new Task started event
        NewTaskStarted(TaskOwnerName, TaskOwnerAddress);
    }
}

using this script in the contracts directory after starting rpc in another Mac terminal tab:

node

Web3 = require('web3')
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));

code = fs.readFileSync('Tasks.sol').toString()
solc = require('solc')
compileCode = solc.compile(code)

abiDefinition = JSON.parse(compileCode.contracts[':Tasks'].interface)

but the solc.compile produces this warning:

compileCode = solc.compile(code)
{ contracts: {},
errors: [ ':1:1: Warning: Source file does not specify required compiler version!Consider adding "pragma solidity ^0.4.18\n\n^\n' ],
sourceList: [ '' ],
sources: { '': { AST: [Object] } } }

and then when I use this command next:

abiDefinition = JSON.parse(compileCode.contracts[':Tasks'].interface)

produces this error:

TypeError: Cannot read property 'interface' of undefined
    at repl:1:59
    at sigintHandlersWrap (vm.js:22:35)
    at sigintHandlersWrap (vm.js:73:12)
    at ContextifyScript.Script.runInThisContext (vm.js:21:12)
    at REPLServer.defaultEval (repl.js:340:29)
    at bound (domain.js:280:14)
    at REPLServer.runBound [as eval] (domain.js:293:12)
    at REPLServer.<anonymous> (repl.js:538:10)
    at emitOne (events.js:101:20)
    at REPLServer.emit (events.js:188:7)
    at REPLServer.Interface._onLine (readline.js:233:10)
    at REPLServer.Interface._line (readline.js:584:8)
    at REPLServer.Interface._ttyWrite (readline.js:861:14)
    at REPLServer.self._ttyWrite (repl.js:611:7)
    at ReadStream.onkeypress (readline.js:120:10)
    at emitTwo (events.js:106:13)
    at ReadStream.emit (events.js:191:7)
    at emitKeys (internal/readline.js:389:14)
    at next (native)
    at ReadStream.onData (readline.js:971:36)
    at emitOne (events.js:96:13)
    at ReadStream.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at ReadStream.Readable.push (_stream_readable.js:134:10)
    at TTY.onread (net.js:547:20)

I'm trying to understand why it is saying that there is no pragma comment when there is. And I'm trying to understand the meaning of the TypeError: Cannot read property as well.

I am running this in the contracts directory of this truffle unbox pet-shop file structure.

Any comments to improve the code would be appreciated as well as I am learning solidity.

Thank you

Best Answer

The class name must be the file name. Try with that and everything will work.

Related Topic