Ethereum_4_合约部署

2018/03/06 Ethereum

参考 https://luren5.gitbooks.io/dapp-develop/content/33-%E7%BB%8F%E5%85%B8%E7%9A%84hello-world.html

  1. 简单合约 ``` pragma solidity ^0.4.7;

contract HelloWorld {

function greet() constant returns(string){
    return "Hello World!";
} } ```
  1. Remix

https://ethereum.github.io/browser-solidity/

  • Create编译
  • Contract details 找到Web3 deploy
var browser_helloworld_sol_helloworldContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"}]);
var browser_helloworld_sol_helloworld = browser_helloworld_sol_helloworldContract.new(
   {
     from: web3.eth.accounts[0], 
     data: '0x6060604052341561000f57600080fd5b5b6101518061001f6000396000f30060606040526000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063cfae32171461003e575b600080fd5b341561004957600080fd5b6100516100cd565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100925780820151818401525b602081019050610076565b50505050905090810190601f1680156100bf5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6100d5610111565b6040805190810160405280600c81526020017f48656c6c6f20576f726c6421000000000000000000000000000000000000000081525090505b90565b6020604051908101604052806000815250905600a165627a7a723058209f03e6199c19e8e55962f2d18cde2ca6f1e61651aae64345a18946c281bc74640029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

修改var browser_helloworld_sol_helloworld 变量名 为 helloworld

  1. 部署

部署合约也是一笔交易,需要挖矿,让节点打包这笔交易。(中间需要解锁账户)

可以开启挖矿

miner.start(1) //一个线程执行

再拷贝上述代码到geth console,执行完成会显示

Contract mined! address: 0x8021741b6375b80b6135b9ab2bb4747524cc4a09 transactionHash: 0xdef9ae339d40a6f4397043e17503d919ed79f8dfc514afdb9d141a2276ddfe4f

在日志文件中也可以查看

tail -1000f file_to_log_output

I0706 17:34:22.292965 internal/ethapi/api.go:1045] Tx(0xdef9ae339d40a6f4397043e17503d919ed79f8dfc514afdb9d141a2276ddfe4f) created: 0x8021741b6375b80b6135b9ab2bb4747524cc4a09
I0706 17:35:10.098890 miner/unconfirmed.go:105] 🔗  mined block #49 [82962fe2…] reached canonical chain
I0706 17:35:10.098926 miner/unconfirmed.go:83] 🔨  mined potential block #54 [adf783c8…], waiting for 5 blocks to confirm
I0706 17:35:10.101199 miner/worker.go:516] commit new work on block 55 with 1 txs & 0 uncles. Took 2.249628ms


  1. 调用合约
> helloworld.greet()
"Hello World!"

Search

    Table of Contents