1,启动私有节点
启动私有节点并输入密码,私有节点启动并开始挖矿。
geth -targetgaslimit 4294967295 -rpc -rpcaddr "127.0.0.1" -rpcport "8101" -port "30303" -rpcapi "db,eth,web3,net,personal" -networkid 11100 -identity 123456 -nodiscover -maxpeers 5 -datadir "%cd%\chain" -unlock 0 -rpccorsdomain "*" -mine console
2,打开Mist,并打开Remix-Solidity IDE
新建智能合约,输入以下内容
pragma solidity ^0.4.20;
contract KVTest{
function k(uint a, uint b) returns (uint c)
{
c = a * b;
return c;
}
}
Note:Solidity IDE有多种使用方式,Mist内置式其一,也可以通过访问地址:
https://ethereum.github.io/browser-solidity
来打开,还可以在本地编译IDE使用,本地编译可参考Github:https://github.com/ethereum/remix-ide 。
3,点击 Start to compile,等代码编译完成
4,可通过Remix自带的javascript VM来进行测试
点击右侧Run tab,Environment选择JavaScript VM,点击Deploy。
等待Pending transactions变为0,切下面出现KVTest的合约,在合约中输入测试数字,直接点击k运行。
之后再编译器下部的Console中可看到如下内容。测试成功。
5,合约部署
再Compile Tab中点击detail
可看到Web3Deploy的内容如下:
var kvtestContract = web3.eth.contract([{"constant":false,"inputs":[{"name":"a","type":"uint256"},{"name":"b","type":"uint256"}],"name":"k","outputs":[{"name":"c","type":"uint256"}],"payable":false,"stateMutability":"nonpayable","type":"function"}]);
var kvtest = kvtestContract.new(
{
from: web3.eth.accounts[0],
data: '0x608060405234801561001057600080fd5b5060ce8061001f6000396000f300608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c2761031146044575b600080fd5b348015604f57600080fd5b5060766004803603810190808035906020019092919080359060200190929190505050608c565b6040518082815260200191505060405180910390f35b60008183026000819055506000549050929150505600a165627a7a7230582036c1d0b335d24f3a53353ed194ffc92e54219ecb539cbeb262d48740848638f30029',
gas: '4700000'
}, function (e, contract){
console.log(e, contract);
if (typeof contract.address !== 'undefined') {
console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
}
})
----------------------------------------------------------------------------------------------------------------------------------
Note:合约ABI(Application Binary Interface)信息解析:
[
{
"constant": false,
"inputs": [
{
"name": "a",
"type": "uint256"
},
{
"name": "b",
"type": "uint256"
}
],
"name": "k",
"outputs": [
{
"name": "c",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
}
]
type:方法类型,包括function,constructor,fallback(缺省方法)可以缺省,默认为function
name:方法名
inputs:方法参数,它是一个对应数组,数组里的每个对象都是一个参数说明
name:参数名
type:参数类型
outputs:方法返回值,格式和inputs类型一样,如果没有返回值可以缺省
constant:布尔值,如果为true说明方法不会修改合约的状态变量
payable:布尔值,标明方法是否可以接受ether
构造方法和缺省方法不能有name和outputs,缺省方法也不能有inputs,向一个没有payable标注的方法发送ether会抛异常。
----------------------------------------------------------------------------------------------------------------------------------
点击复制按钮。打开Geth客户端,粘贴回车,合约即被部署到网络中。
再Console中输入:kvtest.k.call(5,8),得到结果40, 合约部署成功。
简单的合约开发完毕。