以太坊节点部署智能合约(centos 7)

主要着重智能合约部署,ETH节点安装先略过了。

1、centos部署好ETH节点之后,进入安装目录运行节点开发调试终端:

setsid ./geth --rpc --rpcaddr 127.0.0.1 --rpcport 8545 --dev --datadir privchain --allow-insecure-unlock 2>>privchain.log

--allow-insecure-unlock用于解锁权限,不然解锁地址会报错;
--datadir 指定存放数据目录,这里是“privchain” 

2、进入调试终端
./geth attach ipc:./privchain/geth.ipc

 3、查看所有地址

> eth.accounts
["0x448d8936637811d466cc4f4d08b8e1f2863d76b6", "0xed185e7a649c535d482d98db1f69adce58d7e135", "0x4245ac351bcbb1b78d6512c34e8fe4023c5c1a5c"]

  开发调试会有一个默认地址,这个地址密码为空

 4、查看地址余额

eth.getBalance(eth.accounts[0])
1.15792089237316195423570985008687907853269984665640564039457584007913129639927e+77

  一般eth.accounts[0]为默认地址,也有相应的默认余额。

5、新建地址,由于前面我已经新建了两个地址,这里直接上命令

>personal.newAccount("123456")//内容是密码
0xed185e7a649c535d482d98db1f69adce58d7e135

6、解锁地址,用于转出地址

personal.unlockAccount(eth.accounts[1])
Unlock account 0x448d8936637811d466cc4f4d08b8e1f2863d76b6
Password: //如果为空直接回车即可
true

 7、ETH转账

>eth.sendTransaction({from: eth.accounts[0],to: eth.accounts[1],value: web3.toWei(3,"ether")})
"0xc7b20871deffe5bbaf7aa2f3029adbbaecca111a8b15e7b7d8cd5084eb7376ea"

> eth.getBalance(eth.accounts[1])
3000000000000000000

>eth.getTransaction("0xc7b20871deffe5bbaf7aa2f3029adbbaecca111a8b15e7b7d8cd5084eb7376ea");
{
  blockHash: "0xb2b5d06689b7285a5b8f08534fdb9eb5bafd3a20337a88a43d1e01306877e44c",
  blockNumber: 1,
  from: "0x448d8936637811d466cc4f4d08b8e1f2863d76b6",
  gas: 21000,
  gasPrice: 1,
  hash: "0xc7b20871deffe5bbaf7aa2f3029adbbaecca111a8b15e7b7d8cd5084eb7376ea",
  input: "0x",
  nonce: 0,
  r: "0xdb9f25a13b542114e750740bd36b26874815525aeb650aa6d8a707429271da70",
  s: "0x4000e961321b4e3fe863a4b5e923631fb6e543f626df95826853975c32a1cfe8",
  to: "0xed185e7a649c535d482d98db1f69adce58d7e135",
  transactionIndex: 0,
  v: "0xa96",
  value: 3000000000000000000
}

  

 8,solidity网上的案例脚本,可以使用https://remix.ethereum.org/编译器进行编译,

pragma solidity ^0.4.10;

contract Storage {
    uint256 storedData;

    function set(uint256 data) public {
    storedData = data;
    }

    function get() public constant returns (uint256) {
    return storedData;
    }
}

  编译取得如下代码

var _myString = /* var of type string here */ ;
var dataContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"getMyString","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_myString","type":"string"}],"name":"setMyString","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"inputs":[{"name":"_myString","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);
var data = dataContract.new(
   _myString,
   {
     from: web3.eth.accounts[0], 
     data: '0x608060405234801561001057600080fd5b506040516103ce3803806103ce833981018060405281019080805182019291905050508060009080519060200190610049929190610050565b50506100f5565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009157805160ff19168380011785556100bf565b828001600101855582156100bf579182015b828111156100be5782518255916020019190600101906100a3565b5b5090506100cc91906100d0565b5090565b6100f291905b808211156100ee5760008160009055506001016100d6565b5090565b90565b6102ca806101046000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680637e7b8a961461005157806382180568146100e1575b600080fd5b34801561005d57600080fd5b5061006661013d565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100a657808201518184015260208101905061008b565b50505050905090810190601f1680156100d35780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61013b600480360381019080803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192905050506101df565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101d55780601f106101aa576101008083540402835291602001916101d5565b820191906000526020600020905b8154815290600101906020018083116101b857829003601f168201915b5050505050905090565b80600090805190602001906101f59291906101f9565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061023a57805160ff1916838001178555610268565b82800160010185558215610268579182015b8281111561026757825182559160200191906001019061024c565b5b5090506102759190610279565b5090565b61029b91905b8082111561029757600081600090555060010161027f565b5090565b905600a165627a7a72305820936443736d7ac9e024fdad5068218e151bb401a60a0a5ed266749decb910b7510029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

  

9、发布智能合约,出现交易哈希说明发布成功了,到这里就完成了。

>var _myString ="hello word"

>var dataContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"getMyString","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_myString","type":"string"}],"name":"setMyString","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"inputs":[{"name":"_myString","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);

undefined


> personal.unlockAccount(eth.accounts[1])//解锁地址
Unlock account 0xed185e7a649c535d482d98db1f69adce58d7e135
Password: 
true

> dataContract.new(_myString,{from: web3.eth.accounts[1],data: '0x608060405234801561001057600080fd5b506040516103ce3803806103ce833981018060405281019080805182019291905050508060009080519060200190610049929190610050565b50506100f5565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009157805160ff19168380011785556100bf565b828001600101855582156100bf579182015b828111156100be5782518255916020019190600101906100a3565b5b5090506100cc91906100d0565b5090565b6100f291905b808211156100ee5760008160009055506001016100d6565b5090565b90565b6102ca806101046000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680637e7b8a961461005157806382180568146100e1575b600080fd5b34801561005d57600080fd5b5061006661013d565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100a657808201518184015260208101905061008b565b50505050905090810190601f1680156100d35780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61013b600480360381019080803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192905050506101df565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101d55780601f106101aa576101008083540402835291602001916101d5565b820191906000526020600020905b8154815290600101906020018083116101b857829003601f168201915b5050505050905090565b80600090805190602001906101f59291906101f9565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061023a57805160ff1916838001178555610268565b82800160010185558215610268579182015b8281111561026757825182559160200191906001019061024c565b5b5090506102759190610279565b5090565b61029b91905b8082111561029757600081600090555060010161027f565b5090565b905600a165627a7a72305820936443736d7ac9e024fdad5068218e151bb401a60a0a5ed266749decb910b7510029', gas: '4700000'}, function (e, contract){console.log(e, contract);if (typeof contract.address !== 'undefined') {console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);}})
null [object Object]
{
  abi: [{
      constant: true,
      inputs: [],
      name: "getMyString",
      outputs: [{...}],
      payable: false,
      stateMutability: "view",
      type: "function"
  }, {
      constant: false,
      inputs: [{...}],
      name: "setMyString",
      outputs: [],
      payable: true,
      stateMutability: "payable",
      type: "function"
  }, {
      inputs: [{...}],
      payable: false,
      stateMutability: "nonpayable",
      type: "constructor"
  }],
  address: undefined,
  transactionHash: "0x1c44cd556e8af630c7a7bf2f50c336061a8762405299e506919676e42ded2103"
}
> null [object Object]
Contract mined! address: 0x9c2804b1251fcd84f3d985bd880c26c89b8882fe transactionHash: 0x1c44cd556e8af630c7a7bf2f50c336061a8762405299e506919676e42ded2103

 10 最后也可以根据交易哈希查询合约地址:

>eth.getTransactionReceipt("0x085b66b2591ee31c3ad58a66ca485bd19bea6c1fc8ca7550a896853ab52855a6")

  

 

posted @ 2020-03-04 17:15  _ylsn  阅读(1051)  评论(0编辑  收藏  举报