geth編譯部署智能合約

一、安裝solc,Ubuntu 16.04下

sudo add-apt-repository ppa:ethereum/ethereum
sudo apt-get update
sudo apt-get install solc

裝完以後執行 wheris solc 會返回solc的可執行文件路徑 /usr/bin/solcweb

二、添加編譯器

一、啓動geth, 命令以下,樓主把參數換成你本身的json

geth  --identity "newEth" --rpc --rpccorsdomain "*" --datadir "cdata" --port 30303 --rpcapi "personal,db,eth,net,web3" --networkid 999  --rpcport 8080  console

二、添加編譯器api

> admin.setSolc("/usr/bin/solc")
"solc, the solidity compiler commandline interface\nVersion: 0.4.10+commit.f0d539ae.Linux.g++\n"
> eth.getCompilers()
["Solidity"]

三、編譯合約代碼

> source="contract test { function multiply(uint a) returns(uint d) { return a * 7; }}"
"contract test { function multiply(uint a) returns(uint d) { return a * 7; }}"
> contractCompiled = eth.compile.solidity(source)
{
  <stdin>:test: {
    code: "0x60606040523415600b57fe5b5b60788061001a6000396000f300606060405263ffffffff60e060020a600035041663c6888fa181146020575bfe5b3415602757fe5b60306004356042565b60408051918252519081900360200190f35b600781025b9190505600a165627a7a72305820e5878550146c3d0f5d5030fd4b2b033475091b9eabea79f5026d14bc0d103db60029",
    info: {
      abiDefinition: [{...}],
      compilerOptions: "--combined-json bin,abi,userdoc,devdoc --add-std --optimize",
      compilerVersion: "0.4.10",
      developerDoc: {
        methods: {}
      },
      language: "Solidity",
      languageVersion: "0.4.10",
      source: "contract test { function multiply(uint a) returns(uint d) { return a * 7; }}",
      userDoc: {
        methods: {}
      }
    }
  }
}

四、部署合約

不少人都是按照相似官方文檔 相似的步驟,因此卡在這一步。部署合約最重要的是拿到合約編譯後的字節碼abi, 對此不清楚的能夠看社區其它的帖cors

> code=contractCompiled["<stdin>:test"].code
"0x60606040523415600b57fe5b5b60788061001a6000396000f300606060405263ffffffff60e060020a600035041663c6888fa181146020575bfe5b3415602757fe5b60306004356042565b60408051918252519081900360200190f35b600781025b9190505600a165627a7a72305820e5878550146c3d0f5d5030fd4b2b033475091b9eabea79f5026d14bc0d103db60029"
>
> abi=contractCompiled["<stdin>:test"].info.abiDefinition
[{
    constant: false,
    inputs: [{
        name: "a",
        type: "uint256"
    }],
    name: "multiply",
    outputs: [{
        name: "d",
        type: "uint256"
    }],
    payable: false,
    type: "function"
}]
>
> var contract=eth.contract(abi)
undefined
> personal.unlockAccount(eth.accounts[0])
Unlock account 0xa122277be213f56221b6140998c03d860a60e1f8
Passphrase:    // 此處輸入密碼,解鎖帳戶
> var contractInstance=contract.new({from:eth.accounts[0], data:code, gas:300000})
I0401 08:15:43.260996 internal/ethapi/api.go:1141] Tx(0x2bb5b5a407bf4885a90dbd9d3b44b00b32ee64c3cbd1c9c1d5bee40cc3b0d95f) created: 0x3f915fa924f4ab4e25feda28f147fd483e29ea77
undefined

而後開啓挖礦miner.start() , 等待打包(部署合約是一筆交易)後,合約就成功部署到了私有鏈上,來查看一下dom

> contractInstance
{
  abi: [{
      constant: false,
      inputs: [{...}],
      name: "multiply",
      outputs: [{...}],
      payable: false,
      type: "function"
  }],
  address: "0x3f915fa924f4ab4e25feda28f147fd483e29ea77",
  transactionHash: "0x2bb5b5a407bf4885a90dbd9d3b44b00b32ee64c3cbd1c9c1d5bee40cc3b0d95f",
  allEvents: function(),
  multiply: function()
}

能夠看到,上面的address即合約帳戶的地址 細心的同窗可能發現,上面在獲取合約類 contractcontractInstance的時候,我使用了var 關鍵字,而後出現了undefined,不少同窗在QQ羣裏問,其實這並非錯誤,只是JS中兩種不一樣的定義變量的方法,有興趣的同窗能夠google it, 可是在這裏有沒有var關鍵字並不影響ide

五、調用合約方法

上述演示的合約代碼沿用的是樓主的,其實並很差,由於它並不會修改合約的狀態變量,直接call操做便可,而不用發起一筆交易。關於調用合約方法的三種方式 請戳這裏ui

> contractInstance.multiply.call(6)
相關文章
相關標籤/搜索