Ethereum-Solidity


Install:

# dev
$ apk --update add --no-cache boost-dev build-base cmake git
$ apk --update add --no-cache boost-dev g++ make cmake git

# Solidity
$ git clone --recursive https://github.com/ethereum/solidity.git
$ mkdir solidity/build
$ cd solidity/build
$ cmake ..
$ make
$ make install

Quick Start:

$ solc --optimize --combined-json abi,bin /home/src/HelloWorld.sol
$ solc --optimize --combined-json abi,bin HelloWorld.sol

res: |
  {
    "contracts":{
      "Simple.sol:Simple":{
        "abi":"[
          {\"constant\":true,\"inputs\":[],\"name\":\"text\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},
          {\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"}
        ]",
        "bin":"606060405260408051...........c80111b21bd958165b00029"
      }
    },
    "version":"0.4.21-develop.2018.2.21+commit.2f862b47.Linux.g++"
  }

> personal.unlockAccount(eth.accounts[0], "password1");

> var abi = "[{\"constant\":true,\"inputs\":[],\"name\":\"get\",\"outputs\":[{\"name\":\"retVal\",\"type\":\"string\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"}]"

> var testContract = web3.eth.contract(JSON.parse(abi));

shel: |
  > var test = testContract.new({ from: eth.accounts[0], data: "0x" + "6060604052341561000f57600080fd5b61014e8061001e6000396000f3006060604052600436106100405763ffffffff7c01000000000000000000000000000000000000000000000000000000006000350416636d4ce63c8114610045575b600080fd5b341561005057600080fd5b6100586100cf565b60405160208082528190810183818151815260200191508051906020019080838360005b8381101561009457808201518382015260200161007c565b50505050905090810190601f1680156100c15780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100d7610110565b60408051908101604052600c81527f48656c6c6f576f726c64212100000000000000000000000000000000000000006020820152905090565b602060405190810160405260008152905600a165627a7a723058207ca1c6550c760cf41bf3239a05a88012376a66270bc8af3564510e17fa1e702d0029", gas: 4700000},
    function (e, contract) {
      console.log(e, contract);
      if (typeof contract.address !== 'undefined') {
           console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
      }
    }
  );

res: |
  Contract mined! address: 0xb7e69ae41a591eedb4448b3f5b65c99d7ab570b5 transactionHash: 0xca2b2abb8dde20170ec6918950857a691baaa27ad596a7963583205f7c21e5f7

  > test
  > test.get()
    "HelloWorld!!"


ABI: Application Binary Interface

.>  aaaa

:

基本型
  bool
  int
  uint
  address # 20bytes
参照型
  Array
  string
  struct
  mapping

演算子: |
  +-*/
  %
  **
  <<
  >>
  &|^~
  !
  &&
  ||
  ==
  !=
  <
  >
  <=
  >=
  +=
  -=

uint a = 3;
uint b = 2;
uint c = a/b*10 # 10
uint d = 3/2*10 # 15


  pragma solidity ^0.4.16;

  contract <MyContract> {
    string public text;
    function <MyContract>() { # constructor
      text = "text";
    }

    function <myMethod>(string _text) {
      text = _text;
    }

    function <myMethod>() constant returns (string) {
      return text;
    }

    struct <Struct> {
      string text;
      uint  a;
    }
    <Struct>[] public list;
    list.length
    list.push()

    mapping(<KEY>=><VALUE>) public <MAP>;
    <MAP>[KEY]


     //
     /* */
     uint  a;
     int   b = 10;    # 256bit
     int16 c = -10;   # 16bit

     uint[2] x;
     x[0] = 100;
     x[1] = 200;
     uint[2] y = x; # 参照型

     string[2] stArray;
     stArray[0] = "Apple";
     stArray[1] = "Orange";
  }