3

I'm building a Dapp where, whenever a user creates an account, it automatically creates an Ethereum address for the user (I'm exposing the personal module over RPC for now, but it's just for playing purposes). I now want to automatically deploy a contract when a user creates an account, ideally to make that user the only address capable of interacting with that contract.

Following some tutorials (this), I installed solc and changed my code to

var Web3 = require('web3');
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
const solc = require('solc');
var fs = require('fs');
const input = fs.readFileSync('contracts/helloWorld.sol');
const output = solc.compile(input.toString(), 1);
const bytecode = output.contracts['helloWorld'].bytecode;
const abi = JSON.parse(output.contracts[':helloWorld'].interface);

This however, throws an error TypeError: Cannot read property ':helloWorld' of undefined. After some investigation, I realised the output is returning { errors: [ ':1:1: Error: Expected import directive or contract definition.\npragma solidity ^0.4.11;\n^\n' ] }

Finally, even after seeing this answer, I still can't figure out why. I paste my contract code on Remix IDE and it does not throw any error.

My contract code (the test one I'm using now) is

pragma solidity ^0.4.11;

contract helloWorld {
    /* Constructor */
    address owner;

    function helloWorld() {
        owner = msg.sender;    
    }

    function remove() {
        if (msg.sender == owner) {
            // Suicide and send funds to owner
            selfdestruct(owner);
        }
    }

    function greet() constant returns (bytes32, address) {
        if (msg.sender == owner) {
            bytes32 hello = "hello";
            return (hello, msg.sender);
        }
    }

}

Any help would be much appreciated, been stuck for a while :)

3
  • Check compilation results for errors if (output['errors']) { console.log(JSON.stringify(output['errors']))} Commented Jul 18, 2017 at 22:25
  • Is it necessary to read the contract from a file? if contract is not changing you can avoid onfly compilation. Commented Jul 19, 2017 at 3:39
  • It would be more convenient because that way I could have contracts directory as part of the node app with all contracts in it. But what would you propose as the alternative?
    – mcansado
    Commented Jul 19, 2017 at 7:35

3 Answers 3

0

Try just cutting and pasting your code into the nodejs terminal (note the grave ticks) so...

var source = `pragma solidity ^0.4.11;

contract helloWorld {
    /* Constructor */
    address owner;

    function helloWorld() {
        owner = msg.sender;    
    }

    function remove() {
        if (msg.sender == owner) {
            // Suicide and send funds to owner
            selfdestruct(owner);
        }
    }

    function greet() constant returns (bytes32, address) {
        if (msg.sender == owner) {
            bytes32 hello = "hello";
            return (hello, msg.sender);
        }
    }

}`

var output = solc.compile(source);

The import stuff is all sorts of weird still for me too

1
  • This didn't seem to work for me, throws the same error.
    – mcansado
    Commented Jul 18, 2017 at 23:32
0

I think it's an encoding issue. https://github.com/ethereum/solc-js/issues/90

Your error message comes from this line: \npragma solidity ^0.4.11;\n^\n'

When I ran it in my console, the toString resulted in \npragma solidity ^0.4.11;\n\n, so I think the \n^ literal is causing solc to mess up.

8
  • The error is coming from that line but I didn't quite get how to solve it from your answer, could you clarify please?
    – mcansado
    Commented Jul 18, 2017 at 23:23
  • Also, I've tried using npm utf8 package to convert to UTF-8 but it returned an error as well.
    – mcansado
    Commented Jul 19, 2017 at 14:58
  • The person in the link I sent fixed his issue by adjusting the encoding in the editor itself, as importing with utf8 encoding using fs didn't work. I think the issue might be with your editor, because your code worked for me perfectly. Commented Jul 19, 2017 at 15:11
  • I'm using Sublime 2 and have tried File > Save with Encoding > UTF-8 and it still gives me the same error. Any other ideas? Thanks for the help.
    – mcansado
    Commented Jul 19, 2017 at 15:17
  • @thefett's answer worked for me as well... maybe update solc? Commented Jul 19, 2017 at 15:24
0

using solc version 0.4.19, Contract object can be accesible by adding : prefix to the contract name,

const output = solc.compile(input.toString(), 1); const bytecode = '0x' + output.contracts[':helloWorld'].bytecode; const abi = JSON.parse(output.contracts[':helloWorld'].interface);

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.