以太坊(三)MAC以太坊私有链创建第一个智能合约

转载自:https://www.jianshu.com/p/d699c82cedde
前天搭建了以太坊的私有链环境,今天本来想建立一个基于以太坊的智能合约Demo,发现很多过去的文档都已经过时了(包括github官网),折腾了半天,终于搞定了,现记录如下。

安装智能合约编译器

brew tap ethereum/ethereum
brew install solidity

创建智能合约

新建一个contract文件夹,在文件夹中创建一个demo.sol智能合约文件

pragma solidity ^0.4.20;
contract Demo {
  function print(string str) returns (string content) {
    content = str;
  }
}

编译,会多出两个文件,abi文件就是智能合约相关的接口,bin文件就是智能合约编译代码。

> solc -o . --bin --abi demo.sol
> ls
Demo.abi    Demo.bin    demo.sol

在geth中加载这些文件很复杂,这里我们修改下刚生成的文件

Demo.abi 文件内容修改成

var demoContract = eth.contract([{"constant":false,"inputs":[{"name":"str","type":"string"}],"name":"print","outputs":[{"name":"content","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"function"}])

Demo.bin 文件内容修改成

personal.unlockAccount(eth.accounts[0])
var demo = demoContract.new({
  from: eth.accounts[0],
  data: "0x6060604052341561000f57600080fd5b61016c8061001e6000396000f300606060405260043610610041576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806311114af114610046575b600080fd5b341561005157600080fd5b6100a1600480803590602001908201803590602001908080601f0160208091040260200160405190810160405280939291908181526020018383808284378201915050505050509190505061011c565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100e15780820151818401526020810190506100c6565b50505050905090810190601f16801561010e5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61012461012c565b819050919050565b6020604051908101604052806000815250905600a165627a7a72305820e95348d1ac4b8934c8fcf9f894565bd48f2452fd4a62d0048a7d82c4b788e4a00029",
  gas: 500000
})

注意:别忘了data必须0x开头,New合约就得解锁自己的账户,这里解锁也写在这里了

回到geth,加载刚修改的文件,加载bin文件需要输入账户密码

文件夹contract就运行在geth命令的目录

> loadScript("contract/Demo.abi")
true
> loadScript("contract/Demo.bin")
Unlock account 0x0416f04c403099184689990674f5b4259dc46bd8
Passphrase:
true

现在智能合约已经部署到区块链上了,但是要挖矿才能生效,挖完就可以调用合约方法了

> demo
{
  abi: [{
      constant: false,
      inputs: [{...}],
      name: "print",
      outputs: [{...}],
      payable: false,
      stateMutability: "nonpayable",
      type: "function"
  }],
  address: undefined,
  transactionHash: "0xbe51755e1901b037dfd3e9a597a99111ce58d42da2f9fadf18e0909a5fb688fc"
}
> demo.print
undefined
> miner.start(1);admin.sleepBlocks(1);miner.stop();
INFO [03-01|11:01:52] Updated mining threads                   threads=1
INFO [03-01|11:01:52] Transaction pool price threshold updated price=18000000000
INFO [03-01|11:01:52] Starting mining operation 
INFO [03-01|11:01:52] Commit new mining work                   number=73 txs=1 uncles=0 elapsed=2.428ms
INFO [03-01|11:02:53] Successfully sealed new block            number=73 hash=4b0335…049ed7
INFO [03-01|11:02:53]  mined potential block                  number=73 hash=4b0335…049ed7
INFO [03-01|11:02:53] Commit new mining work                   number=74 txs=0 uncles=0 elapsed=602.175µs
true
> demo.print
function()
true
> demo.print.call("hello world")
"hello world"

猜你喜欢

转载自blog.csdn.net/qq1263292336/article/details/79866165