智能合约编写01

Hello.sol

pragma solidity ^0.4.22;

contract Hello {
    /* Define variable owner of the type address */
    address public owner;

    /* Define variable greeting of the type string */
    string public greeting ;

    /* Function to recover the funds on the contract */
    function kill() public { 
      if (msg.sender == owner)selfdestruct(owner);
    }

    /* This runs when the contract is executed */
    constructor(string _greeting) public {
        owner = msg.sender;
        greeting = _greeting;
    }

    /* Main function */
    function greet() public constant returns (string) {
        return greeting;
    }
}

编译合约

#编译方法有两种,一种是命令行编译(当前使用),一种是用Remid IDE编译(后面有介绍)
#从github上面下载solc编译工具
#用下面的命令行,生成abi和bin文件
../solc -o . --bin --abi Hello.sol

helloAbi01 = JSON.parse('ABI文件的全部内容');
helloBin01 = '0x'+'BIN文件的全部内容';

开启私链或者测试链

测试链开启,请查看前面的章节

私链开启,请查看后面的章节

本例用了私链

部署并使用合约

#在BC01开启geth
./geth --datadir=~/eth001 --networkid 1 --bootnodes=enode://f2576f4b3334979404226043bb412e9a1f3c33d2ce9f9894e1478f9dfde5493a3f3db189393db841b308cbf35dfa4a6dafa63796968eae8721b285c12ff40bae@[172.16.172.81]:30301 console

#在BC01解锁账号
personal.unlockAccount('c230847d2669091b77053df0cbc7f533a0a4cf1c','patient1',3600)

#在BC01部署合约
var helloAbi01 = JSON.parse('[{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"greeting","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]');
var helloFactory01 = eth.contract(helloAbi01);   
var helloBin01 = '0x608060405234801561001057600080fd5b5060405161055b38038061055b83398101806040528101908080518201929190505050336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff1602179055508060019080519060200190610089929190610090565b5050610135565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f106100d157805160ff19168380011785556100ff565b828001600101855582156100ff579182015b828111156100fe5782518255916020019190600101906100e3565b5b50905061010c9190610110565b5090565b61013291905b8082111561012e576000816000905550600101610116565b5090565b90565b610417806101446000396000f300608060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff16806341c0e1b5146100675780638da5cb5b1461007e578063cfae3217146100d5578063ef690cc014610165575b600080fd5b34801561007357600080fd5b5061007c6101f5565b005b34801561008a57600080fd5b50610093610286565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b3480156100e157600080fd5b506100ea6102ab565b6040518080602001828103825283818151815260200191508051906020019080838360005b8381101561012a57808201518184015260208101905061010f565b50505050905090810190601f1680156101575780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b34801561017157600080fd5b5061017a61034d565b6040518080602001828103825283818151815260200191508051906020019080838360005b838110156101ba57808201518184015260208101905061019f565b50505050905090810190601f1680156101e75780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff161415610284576000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16ff5b565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b606060018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103435780601f1061031857610100808354040283529160200191610343565b820191906000526020600020905b81548152906001019060200180831161032657829003601f168201915b5050505050905090565b60018054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156103e35780601f106103b8576101008083540402835291602001916103e3565b820191906000526020600020905b8154815290600101906020018083116103c657829003601f168201915b5050505050815600a165627a7a723058207f63f089b6ce164c7d54c1e4025ac268fd741bae3107a7f1e57f382f4c8a26000029';

var myHello01 = helloFactory01.new('Hello Eth!',{from: eth.accounts[0], data: helloBin01, gas: '100000000'}, function (e, contract){
    if(e) {
      console.error(e);
      return;
    }

    if(!contract.address) {
      console.log("Contract transaction send: TransactionHash: " + contract.transactionHash + " waiting to be mined...");

    } else {
      console.log("Contract mined! Address: " + contract.address);
      console.log(contract);
    }
 });

#部署后结果,一定要记录合约地址
INFO [05-01|21:39:52] Submitted contract creation              fullhash=0xff2a2bcf13fef68d117724a3cf6183ee56723ee324f113ddf6c3b32ed6e0b201 contract=0xBa67BbD4cF60C7Ab8277954B9b6445fF9589c3a6
Contract transaction send: TransactionHash: 0xff2a2bcf13fef68d117724a3cf6183ee56723ee324f113ddf6c3b32ed6e0b201 waiting to be mined...
undefined

#等待合约被挖坑

#在BC02调用合约
var helloAbi01 = JSON.parse('[{"constant":false,"inputs":[],"name":"kill","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"greet","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"greeting","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[{"name":"_greeting","type":"string"}],"payable":false,"stateMutability":"nonpayable","type":"constructor"}]');
var helloFactory01 = eth.contract(helloAbi01);   
#这个地址就是部署合约时,返回的合约地址
var myHello01 = helloFactory01.at('0xBa67BbD4cF60C7Ab8277954B9b6445fF9589c3a6')  
myHello01.greet();

最后更新于