13

Is it possible to load Solidity source file .sol file to go-ethereum.

I am using the 'loadScript' method by saving a .js file like the following and loads it.

source = "contract test {\n" +
"   /// @notice will multiply `a` by 7.\n" +
"   function multiply(uint a) returns(uint d) {\n" +
"      return a * 7;\n" +
"   }\n" +
"} ";

Is there any better way? I don't want to use browser-solidity

2 Answers 2

9

I found this Solidity Compile Helper ruby wrapper

Usage

solc_helper contract.sol

This will generate a javascript file and print out a statement like the following.

loadScript('/path/contract.js')

Paste this statement into the geth console. Your code will be loaded and your contract will be deployed automatically.

There are also a few helper functions to easily deploy the contracts.

5

Update Jan 3 2017

Note that Solidity 4.7 compiler makes different code for the same contract when you have different whitespacing. This will be important when you try to add verified sources in the blockchain explorers as the compiled source will have to match the deployed code. You may want to consider deploying your contracts on the Mainnet or Testnet blockchain using Browser Solidity or Ethereum Wallet / Mist, instead of using the geth command line.

From the link above:

pragma solidity ^0.4.7; contract Test { }  

produces:

6060604052346000575b60358060166000396000f30060606040525b60005600a165627a7a7230582083d1e44aa5fc6707310b1b2f2f853c4fe7a55c7d9d7f5fd00699d97527c4fe4a0029

And

pragma solidity ^0.4.7; contract Test {}  

produces:

6060604052346000575b60358060166000396000f30060606040525b60005600a165627a7a723058204687f46477038e9c93d271aa664f966bf84275743951a45d1f7b801daafe15ae0029

This is due to:

I believe these extra 32 bytes are the Swarm hash of the metadata file that Solidity generates. See the release notes for 0.4.7. The code comments explain what is going on.

I agree that it is unexpected behaviour for whitespace changes, but note that it is intended to help with source code verification, not hinder it (see comments in code under usage for source code verification).



Using Linux (will also work in Cygwin), I've created a simple Perl script to remove the CRLF and compress spaces which I save as $HOME/bin/stripCrLf :

#!/usr/bin/perl
while (<>) {
  chomp;           # Remove newline
  s#/\*.*?\*/##sg; # Remove multiline comments
  s/\/\/.*$//go;   # Remove // comments
  s/\s+/ /go;      # Collapse space
  print;
}
print "\n";

You will need to run the following command to make the command executable:

chmod 700 $HOME/bin/stripCrLf

I've saved the following code that I borrowed from https://gist.github.com/alexvandesande/259b4ffb581493ec0a1c into random.sol .

contract random {
    /* Generates a random number from 0 to 100 based on the last block hash */
    function randomGen(uint seed) constant returns (uint randomNumber) {
        return(uint(sha3(block.blockhash(block.number-1), seed ))%100);
    }

    /* generates a number from 0 to 2^n based on the last n blocks */
    function multiBlockRandomGen(uint seed, uint size) constant returns (uint randomNumber) {
        uint n = 0;
        for (uint i = 0; i < size; i++){
            if (uint(sha3(block.blockhash(block.number-i-1), seed ))%2==0)
                n += 2**i;
        }
        return n;
    }
}

I can then run the following command in a Linux terminal:

beefee@Kumquat:~$ echo "var randomSource='`stripCrLf random.sol`'"
var randomSource='contract random { /* Generates a random number from 0 to 100 based on the last block hash */ function randomGen(uint seed) constant returns (uint randomNumber) { return(uint(sha3(block.blockhash(block.number-1), seed ))%100); }  /* generates a number from 0 to 2^n based on the last n blocks */ function multiBlockRandomGen(uint seed, uint size) constant returns (uint randomNumber) { uint n = 0; for (uint i = 0; i < size; i++){ if (uint(sha3(block.blockhash(block.number-i-1), seed ))%2==0) n += 2**i; } return n; }}'

I then triple-click on the output line and paste it into the geth command line:

> var randomSource='contract random { /* Generates a random number from 0 to 100 based on the last block hash */ function randomGen(uint seed) constant returns (uint randomNumber) { return(uint(sha3(block.blockhash(block.number-1), seed ))%100); }  /* generates a number from 0 to 2^n based on the last n blocks */ function multiBlockRandomGen(uint seed, uint size) constant returns (uint randomNumber) { uint n = 0; for (uint i = 0; i < size; i++){ if (uint(sha3(block.blockhash(block.number-i-1), seed ))%2==0) n += 2**i; } return n; }}'
undefined
> randomSource
"contract random { /* Generates a random number from 0 to 100 based on the last block hash */ function randomGen(uint seed) constant returns (uint randomNumber) { return(uint(sha3(block.blockhash(block.number-1), seed ))%100); }  /* generates a number from 0 to 2^n based on the last n blocks */ function multiBlockRandomGen(uint seed, uint size) constant returns (uint randomNumber) { uint n = 0; for (uint i = 0; i < size; i++){ if (uint(sha3(block.blockhash(block.number-i-1), seed ))%2==0) n += 2**i; } return n; }}"
> I0309 08:26:32.300483 miner/worker.go:349] 🔨 Mined block (#53 / 7892a72e). Wait 5 blocks for confirmation
> var randomCompiled = web3.eth.compile.solidity(randomSource)
I0309 08:31:53.168772 miner/worker.go:570] commit new work on block 76 with 0 txs & 0 uncles. Took 262.261µs
...
> randomCompiled
{
  random: {
    code: "0x606060405260978060106000396000f3606060405260e060020a600035046341fa487681146024578063434b14e714606b575b005b6085600435602435600080805b83811015608f574381900360001901406060908152608086905260409020600290068314156064578060020a8201915081505b6001016031565b436000190140606090815260043560805260409020606490065b6060908152602090f35b50939250505056",
    info: {
      abiDefinition: [{...}, {...}],
      compilerOptions: "--bin --abi --userdoc --devdoc --add-std --optimize -o /tmp/solc030529307",
      compilerVersion: "0.2.2",
      developerDoc: {
        methods: {}
      },
      language: "Solidity",
      languageVersion: "0.2.2",
      source: "contract random { /* Generates a random number from 0 to 100 based on the last block hash */ function randomGen(uint seed) constant returns (uint randomNumber) { return(uint(sha3(block.blockhash(block.number-1), seed ))%100); }  /* generates a number from 0 to 2^n based on the last n blocks */ function multiBlockRandomGen(uint seed, uint size) constant returns (uint randomNumber) { uint n = 0; for (uint i = 0; i < size; i++){ if (uint(sha3(block.blockhash(block.number-i-1), seed ))%2==0) n += 2**i; } return n; }}",
      userDoc: {
        methods: {}
      }
    }
  }
}

Using this method, I can edit the source code in one terminal, stripCrLf and copy the resulting information in another terminal and paste and run in geth in a third terminal.

2
  • 1
    Sorry to say, but this is complicated than my current workaround.
    – niksmac
    Commented Apr 3, 2016 at 9:03
  • 1
    If there is newline in between /**/ seems like it does not remove comments. We have to do something like that: stackoverflow.com/a/911583/2402577@BokkyPooBah
    – alper
    Commented Apr 8, 2017 at 16:26

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.