合約部署須要挖礦才能成功,咱們新開一個窗口用與挖礦,新開一個控制檯,輸入命令:geth attach 鏈接到控制檯,執行miner.start(1),開始挖礦。web
智能合約的部署須要編譯,這裏用在線編譯 https://remix.ethereum.orgapp
合約DEMO以下:分佈式
pragma solidity ^0.4.0; contract InfoContract { string name; uint age; function setInfo(string _name, uint _age) public { name = _name; age = _age; } function getInfo() public constant returns (string, uint) { return (name, age); } }
編譯後部署代碼:函數
var infocontractContract = web3.eth.contract([{"constant":true,"inputs":[],"name":"getInfo","outputs":[{"name":"","type":"string"},{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_name","type":"string"},{"name":"_age","type":"uint256"}],"name":"setInfo","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"}]); var infocontract = infocontractContract.new( { from: web3.eth.accounts[0], data: '0x608060405234801561001057600080fd5b506102fa806100206000396000f30060806040526004361061004c576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff1680635a9b0b89146100515780638262963b146100e8575b600080fd5b34801561005d57600080fd5b5061006661015b565b6040518080602001838152602001828103825284818151815260200191508051906020019080838360005b838110156100ac578082015181840152602081019050610091565b50505050905090810190601f1680156100d95780820380516001836020036101000a031916815260200191505b50935050505060405180910390f35b3480156100f457600080fd5b50610159600480360381019080803590602001908201803590602001908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050919291929080359060200190929190505050610207565b005b6060600080600154818054600181600116156101000203166002900480601f0160208091040260200160405190810160405280929190818152602001828054600181600116156101000203166002900480156101f85780601f106101cd576101008083540402835291602001916101f8565b820191906000526020600020905b8154815290600101906020018083116101db57829003601f168201915b50505050509150915091509091565b816000908051906020019061021d929190610229565b50806001819055505050565b828054600181600116156101000203166002900490600052602060002090601f016020900481019282601f1061026a57805160ff1916838001178555610298565b82800160010185558215610298579182015b8281111561029757825182559160200191906001019061027c565b5b5090506102a591906102a9565b5090565b6102cb91905b808211156102c75760008160009055506001016102af565b5090565b905600a165627a7a723058209cd9d1fc6f2d24528eb8432fe797470bba6b502f377dd0d96024c8e01dd3de2c0029', gas: '4700000' }, function (e, contract){ console.log(e, contract); if (typeof contract.address !== 'undefined') { console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash); } })
將編譯好的代碼拷貝直接放到控制檯便可。若是出現Error: account is locked undefined錯誤的話, 則使用personal.unlockAccount(eth.accounts[0],'password')命令將用戶解鎖。Password爲你建立帳號的密碼。 一段時間後geth窗口就會出現Contract mined! address..., 代表合約代碼發佈成功。ui
合約部署成功後,在控制檯能夠直接調用。spa
set方法改變了合約內部狀態,因此也要解鎖交易發送者帳戶,消耗gas,而且須要礦工打包交易持久化到區塊。3d
> infocontract.setInfo.sendTransaction("xiaoming",18,{from:eth.accounts[0]})
get方法沒有改變合約內部狀態,因此不須要消耗gas和礦工打包。blog
> infocontract.getInfo() ["xiaoming", 18]
以太坊是一個分佈式的智能合約平臺,能夠分發代幣(Token)。 訪問 https://etherscan.io/tokens 能夠了解到,若是這麼多代幣的標準不統一,對於其餘人來查看代碼是至關痛苦的,衆籌的人也就沒有辦法來檢查代幣分發的是否合理,也沒有辦法作到多種錢包的兼容。token
爲了咱們發佈的Token被以太坊錢包支持,要求咱們在實現代幣的時候必需要遵照的協議,如指定代幣名稱、總量、實現代幣交易函數等。接口
因此才推出了一種以太坊代幣的標準:ERC20標準。
ERC20標準實際上就是一個合約接口標準。
function name() constant returns (string name)
返回string類型的ERC20代幣的名字,例如:StatusNetwork
function symbol() constant returns (string symbol)
返回string類型的ERC20代幣的符號,也就是代幣的簡稱,例如:SNT。
function decimals() constant returns (uint8 decimals)
支持幾位小數點後幾位。若是設置爲3。也就是支持0.001表示。
function totalSupply() constant returns (uint256 totalSupply)
發行代幣的總量,能夠經過這個函數來獲取。全部智能合約發行的代幣總量是必定的,totalSupply必須設置初始值。若是不設置初始值,這個代幣發行就說明有問題。
function balanceOf(address _owner) constant returns (uint256 balance)
輸入地址,能夠獲取該地址代幣的餘額。
function transfer(address _to, uint256 _value) returns (bool success)
調用transfer函數將本身的token轉帳給_to地址,_value爲轉帳個數
function approve(address _spender, uint256 _value) returns (bool success)
批准_spender帳戶從本身的帳戶轉移_value個token。能夠分屢次轉移。
function transferFrom(address _from, address _to, uint256 _value) returns (bool success)
與approve搭配使用,approve批准以後,調用transferFrom函數來轉移token。
function allowance(address _owner, address _spender) constant returns (uint256 remaining)
返回_spender還能提取token的個數。
帳戶A有1000個ETH,想容許B帳戶隨意調用100個ETH。A帳戶按照如下形式調用approve函數approve(B,100)。當B帳戶想用這100個ETH中的10個ETH給C帳戶時,則調用transferFrom(A, C, 10)。這時調用allowance(A, B)能夠查看B帳戶還可以調用A帳戶多少個token。
Events
event Transfer(address indexed _from, address indexed _to, uint256 _value)
當成功轉移token時,必定要觸發Transfer事件
event Approval(address indexed _owner, address indexed _spender, uint256 _value)
當調用approval函數成功時,必定要觸發Approval事件