以太坊 ethereum

命令行部署以太坊合约 deploy contract soli

2021-08-28  本文已影响0人  walker_1992

命令行部署合约

启动环境:用geth启动一个以太坊节点:

进入到.../ethereuem/build/bin:

启动geth:

./geth --datadir walkertest/ --miner.etherbase  0x106cdbb5029d186ae6f659b52fb83179fc153e6a --mine --miner.threads 1   --rpc --rpcvhosts "*" --rpcaddr 0.0.0.0 --rpcport 8545 --rpccorsdomain "*" --rpcapi "db,eth,net,web3,personal,debug"

为了便于查看交互信息,再开一个窗口,启动console:

 ./geth attach /Users/.../walkertest/geth.ipc 

编写合约代码:

pragma solidity >=0.4.21 < 0.6.0;

contract greeting {
    string myStr;
    
    constructor(string _myStr) public{
        myStr = _myStr;
    }
    
    function getMystring() constant public returns (string){
        return myStr;
    }
    
    function setMyString(string _myStr)public payable{
        myStr =_myStr;
    }
}  

定义了一个名为greeting的智能合约,在智能合约初始化时保存了一个字符串,每次调用getMyString返回字符串,每次调用setMyString改变字符串。

编译合约

这里选择线上编译器将上面的合约编译,打开remix,将写好的合约代码粘贴进去。

点击Compile data.sol后,再点击Compilation Details,在弹出的对话框中找到WEB3DEPLOY部分,点拷贝。粘贴到sublime或者其他编辑器中:

var _myStr = "hello dataqin!";
var greetingContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"getMystring","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_myStr","type":"string"}],"name":"setMyString","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"inputs":[{"name":"_myStr","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]);
var greeting = greetingContract.new(
   _myStr,
   {
     from: web3.eth.accounts[0], 
     data: '0x608060405234801561001057600080fd5b506040516103ce3803806103ce833981018060405281019080805182019291905050508060009080519060200190610049929190610050565b50506100f5565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061009157805160ff19168380011785556100bf565b828001600101855582156100bf579182015b828111156100be5782518255916020019190600101906100a3565b5b5090506100cc91906100d0565b5090565b6100f291905b808211156100ee5760008160009055506001016100d6565b5090565b90565b6102ca806101046000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680637307a3491461005157806382180568146100e1575b600080fd5b34801561005d57600080fd5b5061006661013d565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156100a657808201518184015260208101905061008b565b50505050905090810190601f1680156100d35780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61013b600480360381019080803590602001908201803590602001908080601f01602080910402602001604051908101604052809392919081815260200183838082843782019150505050505091929192905050506101df565b005b606060008054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101d55780601f106101aa576101008083540402835291602001916101d5565b820191906000526020600020905b8154815290600101906020018083116101b857829003601f168201915b5050505050905090565b80600090805190602001906101f59291906101f9565b5050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061023a57805160ff1916838001178555610268565b82800160010185558215610268579182015b8281111561026757825182559160200191906001019061024c565b5b5090506102759190610279565b5090565b61029b91905b8082111561029757600081600090555060010161027f565b5090565b905600a165627a7a723058200391d0360b2537e075bda78dd0853794913d525f8a8fade4c094b87630c8e6e20029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
    }
 })

代码内容解释:
第1行:修改过输入字符串为Hello dataqin!。
第2行:用eth.contract来定义一个合约类(ABI接口)。
第3行:部署创建合约实例,并修改合约实例名greetinng,之后可以直接用实例调用函数。
第4行:创建实例时的入参。
第6行:部署合约的账户,该账户需先解锁,不然会报错,这里用新账户部署。
第7行:data里为合约的编译后的二进制代码。
第8行:准备付的gas费用,IDE已经帮我们预估好了。
第9行:设置部署回调函数。

解锁账号,并保证改账号有足够的eth

> eth.accounts[0]
"0x106cdbb5029d186ae6f659b52fb83179fc153e6a"
> personal.unlockAccount("0x106cdbb5029d186ae6f659b52fb83179fc153e6a","walker")
true
> eth.getBalance("0x106cdbb5029d186ae6f659b52fb83179fc153e6a")
3.7287000066644e+22
> 

执行部署合约

拷贝编辑好的代码到console,回车,看到返回结果:

Contract mined! address: 0x4b1d5efc2579689e5e935fb5ad842f9d186afca7 transactionHash: 0xafcbefe7b81da35cc5d55fe788cd38bc6f5624a2ed3f10bb0c97d70631452e09

运行合约

> greeting.getMystring()
"hello dataqin!"
>  greeting.setMyString.sendTransaction('hello boy',{from:"0x106cdbb5029d186ae6f659b52fb83179fc153e6a", gas:3000000})
"0x08dcbd4d45487b8392586b1534416be4d26488c2f6b8764d716d515b4cf91edd"
> greeting.getMystring()
"hello dataqin!"

合约部署成功

在console中如何调用已经部署的合约?

从新打开一个console窗口

> abi = [{"constant":true,"inputs":[],"name":"getMystring","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_myStr","type":"string"}],"name":"setMyString","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"inputs":[{"name":"_myStr","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]
[{
    constant: true,
    inputs: [],
    name: "getMystring",
    outputs: [{
        name: "",
        type: "string"
    }],
    payable: false,
    stateMutability: "view",
    type: "function"
}, {
    constant: false,
    inputs: [{
        name: "_myStr",
        type: "string"
    }],
    name: "setMyString",
    outputs: [],
    payable: true,
    stateMutability: "payable",
    type: "function"
}, {
    inputs: [{
        name: "_myStr",
        type: "string"
    }],
    payable: false,
    stateMutability: "nonpayable",
    type: "constructor"
}]

定义一个合约类

需要使用eth.contract来定义一个合约类

> data = eth.contract(abi)
{
  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"
  }],
  eth: {
    accounts: ["0x106cdbb5029d186ae6f659b52fb83179fc153e6a", "0x9de47bfdf3ccac22349ec090942692ed0a4b3bdb", "0xc378658c4410578a699d11f64ad69df50108e9e6", "0x21290df9f8424a15bfdd2daa04347a60e2b4d2a2", "0x3b7bfcffd7a8c1047055edc58e2efa6eb589184f"],
    blockNumber: 5346,
    coinbase: "0x106cdbb5029d186ae6f659b52fb83179fc153e6a",
    compile: {
      lll: function(),
      serpent: function(),
      solidity: function()
    },
    defaultAccount: undefined,
    defaultBlock: "latest",
    gasPrice: 1000000000,
    hashrate: 1070,
    mining: true,
    pendingTransactions: [],
    protocolVersion: "0x3f",
    syncing: false,
    call: function(),
    chainId: function(),
    contract: function(abi),
    estimateGas: function(),
    filter: function(options, callback, filterCreationErrorCallback),
    getAccounts: function(callback),
    getBalance: function(),
    getBlock: function(),
    getBlockNumber: function(callback),
    getBlockTransactionCount: function(),
    getBlockUncleCount: function(),
    getCode: function(),
    getCoinbase: function(callback),
    getCompilers: function(),
    getGasPrice: function(callback),
    getHashrate: function(callback),
    getMining: function(callback),
    getPendingTransactions: function(callback),
    getProof: function(),
    getProtocolVersion: function(callback),
    getRawTransaction: function(),
    getRawTransactionFromBlock: function(),
    getStorageAt: function(),
    getSyncing: function(callback),
    getTransaction: function(),
    getTransactionCount: function(),
    getTransactionFromBlock: function(),
    getTransactionReceipt: function(),
    getUncle: function(),
    getWork: function(),
    iban: function(iban),
    icapNamereg: function(),
    isSyncing: function(callback),
    namereg: function(),
    resend: function(),
    sendIBANTransaction: function(),
    sendRawTransaction: function(),
    sendTransaction: function(),
    sign: function(),
    signTransaction: function(),
    submitTransaction: function(),
    submitWork: function()
  },
  at: function(address, callback),
  getData: function(),
  new: function()
}

赋值合约地址

> newContract = data.at("0x4b1d5efc2579689e5e935fb5ad842f9d186afca7")
{
  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: "0x4b1d5efc2579689e5e935fb5ad842f9d186afca7",
  transactionHash: null,
  allEvents: function(),
  getMystring: function(),
  setMyString: function()
}

使用合约

> newContract.getMystring()
"hello dataqin!"
> newContract.setMyString.sendTransaction("you are the best man",{from:"0x106cdbb5029d186ae6f659b52fb83179fc153e6a",gas:3000000})
"0xf93fcce33f48015976b6beb2fb1809eba84e03abada5a69d5a74371d1e30bf06"
> newContract.getMystring()
"you are the best man"

测试成功!!

参考:
1,以太坊从搭建环境到部署合约
2,以太坊智能合约部署与交互

上一篇下一篇

猜你喜欢

热点阅读