Solidity: First Iteration - msatkinson/ethereum GitHub Wiki

##Introduction The first iteration is not expected to express the most efficient ways to get things done. It's a journal of exploration, describing of parts of the Ethereum toolset with a bias towards understanding the command line interfaces. ###Running Solc Download the latest nodejs tarball and install solc: npm install solc then start node to access the REPL. ##Write the 'greeter' Contract Use the Solc browser app to write your contract, the app provides syntax highlighting and automatically creates the JavaScript needed to deploy a contract. The JavaScript is available from the Web3 Deploy text box available from the right hand pane. ##Load the 'greeter' Contract Source Create a nodejs module for importing the contract source:

//node.js only exposes module.exports as part of it's require behavior
module.exports = {
  /*
    usage:
    npm install solc
    var solc = require('solc')
    var greeter = require('./greeter.compile.js')
    var compiledGreeter = solc.compile(greeter.source())
    var fs = require('fs')
    var writer = fs.createWriteStream('./greeter.object.js')
    writer.write(JSON.stringify(comiledGreeter))
    writer.close()
  */
  source: function () { 
    return 'contract mortal { address owner; function mortal() {owner = msg.sender;} function kill() { if(msg.sender == owner) suicide(owner); } } contract greeter is mortal { string message; function greeter(string _message) public { message = _message;} function greet() constant returns (string) {return message;} }';
  }
};

Load the source into the REPL var greeter = require('./greeter.js')

##Compile the 'greeter' Source

var greeter = require('./greeter.js')
var compiledGreeter = solc.compile(greeter.source())

##Navigating the compiledGreeter object The compiledGreeter contains 2 contracts:

  1. compiledGreeter.contracts.mortal
  2. compiledGreeter.contracts.greeter ###Deploying a Contract The compiledGreeter provides all the variables needed to deploy the contract using the web3 API. Unfortunately, the web3 API is not provided as part of the solc npm package. The solc browser helps bridge the gap but it would be useful to be able to deploy contracts from solc: s ####Update: Combining the solc and web3 APIs in a single session The Solidity: Second Iteration shows how nodejs can be used to work with the solc and web3 apis. The following geth notes also require exploring: https://github.com/ethereum/go-ethereum/wiki/Contract-Tutorial#linking-your-compiler-in-geth

var mortalContract = web3.eth.contract(compiledGreeter.contracts.mortal.interface())