0

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

9
  • 1
    Because compile filed, compileCode.contracts[':Tasks'] is undefined. So that explains the second error. As for the first, main error, seems like it's not reading your file properly. Have you tried to console.log code? And obviously Tasks.sol in the same folder as the script? I just tried this, and it worked for me.
    – carlolm
    Commented Oct 22, 2017 at 11:51
  • Hi carlolm, where would i place console.log code ? and is that the exact syntax? I usually see something like console.log(somecommand). Also, I'm not running the script in the file but pasting it in at the web3.js prompt (>). It's good to know that it is worked for you, I agree that it doesn't seem to be reading my file correctly.
    – Jazzmine
    Commented Oct 22, 2017 at 12:09
  • (just noticed, previous message: compile "failed"). You can run console.log(code) after the code = ... line. Just to check if it's reading the file
    – carlolm
    Commented Oct 22, 2017 at 12:31
  • I tried compiling it in remix and it did not report the pragma error but I did get this: Tasks errored: Error encoding arguments: SyntaxError: Unexpected token m in JSON at position 1
    – Jazzmine
    Commented Oct 22, 2017 at 12:32
  • OK carlolm - you are on to something. I get an empty string for code when I do this even though I'm in the directory with the contract file (Tasks.sol) : > code = fs.readFileSync('Tasks.sol').toString() '' (my note this is an empty string so it's not finding the string) > (To exit, press ^C again or type .exit) > MacBook-Pro:contracts user1$ ls Migrations.sol Tasks.sol .
    – Jazzmine
    Commented Oct 22, 2017 at 12:37

1 Answer 1

-1

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

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.