在自己的电脑上安装配置以太坊开发环境,搭建以太坊私有链,编写一个简单的智能合约,通过以太坊JSON RPC和JavaScript API编程接口将其部署到所创建的以太坊私有链,并可调用合约获得正确的合约执行结果
以太坊开发环境搭建及使用
系统:OS X
配置以太坊环境
- 安装Go环境
- 下载地址:https://golang.org/dl/
- 下载后双击安装,默认会安装到"/usr/local/go"
编辑~/.bash_profile文件,配置gopath环境变量:
vim ~/.bash_profile
添加以下环境变量
export GOPATH="/usr/local/go"
export GOBIN="$GOPATH/bin"
export PATH="$GOBIN:$PATH"
在终端执行以下命令,使配置文件立即生效
source ~/.bash_profile
查看安装是否成功
- 安装Node.js 、npm
brew install node
查看是否安装成功
- 安装以太坊Ethereum
通过以下命令安装以太坊
需要先安装包管理器brew
brew tap ethereum/ethereum
brew install ethereum
查看是否安装成功
- 安装solc编译器
solc是智能合约Solidity的编译器,在终端执行以下命令安装solc:
npm install solc
查看是否安装成功
which solc
搭建以太坊私有链
- 编写创世块文件
命令行输入:
vim genesis.json
genesis.json文件内容:
{
"config": {
"chainId": 11,
"homesteadBlock": 0,
"eip155Block": 0,
"eip158Block": 0
},
"alloc" : {
},
"coinbase" : "0x0000000000000000000000000000000000000000",
"difficulty" : "0x20000",
"extraData" : "",
"gasLimit" : "0x2fefd8",
"nonce" : "0x0000000000000042",
"mixhash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"parentHash" : "0x0000000000000000000000000000000000000000000000000000000000000000",
"timestamp" : "0x00"
}
保存并退出
- 初始化创世块
- 启动以太坊
geth --nodiscover --rpc --rpcport "8545" --maxpeers '5' --networkid 11 console
-
创建账户(公钥)
输入以下命令3次,创建3个以太坊账户 -
启动挖旷
-
停止挖旷
-
查看账户余额
通过JSON RPC编译智能合约并部署
- 编写智能合约
testContract.sol
pragma solidity ^0.5.0;
contract testContract{
function multiply (uint a)public returns(uint d)
{
d=a*7;
}
}
- 编译合约
solcjs --abi testContract.sol
生成testContract_sol_testContract.abi
solcjs --bin testContract.sol
生成testContract_sol_testContract.bin
- 获得调用账户
curl -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_coinbase","id":1}' localhost:8545
- 部署合约
curl -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{"from":"0xfa5169ed2b43cf6e3b3111ba9bb19f768e5a178e","data":"0x608060405234801561001057600080fd5b5060c88061001f6000396000f3fe608060405260043610603f576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063c6888fa1146044575b600080fd5b348015604f57600080fd5b50607960048036036020811015606457600080fd5b8101908080359060200190929190505050608f565b6040518082815260200191505060405180910390f35b600060078202905091905056fea165627a7a72305820cb6fd898c32856169d9a329776b72b4d0a234d652a7fb330c543933c45bc3ca30029","gas":"0x10000"}],"id":1}' localhost:8545
- 调用合约方法
调用合约方法前要先**对应的用户账户
curl -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_sendTransaction","params":[{"from":"0xfa5169ed2b43cf6e3b3111ba9bb19f768e5a178e","to":"0x250b8e8e7798af6ca673d4d30018d615bc45d659","data":"0xc6888f0000000000000000000000000000000000000000000000000000000000000006"}],"id":8}' localhost:8545
结果
- 根据交易哈希查找交易详情
根据返回结果中的result中的交易哈希值查询本次交易的详情
curl -H "Content-Type: application/json" --data '{"jsonrpc":"2.0","method":"eth_getTransactionReceipt","params":["0x2dbf37ba484133352d719c1e1fbb3e60ba58062d62bfaea8c04da189a3247958"],"id":1}' localhost:8545
通过JavaScript API编译智能合约并部署
-
登录以太坊
-
部署合约
web3.eth.contract(abi).new({from:eth.accounts[0],data:code,gas:1000000})
- 调用合约方法
获取合约
调用方法