首先安装一个testrpc
,用npm
来安装。
# 安装 testrpc
npm install -g ethereumjs-testrpc //testrpc现在用ganache-cli来替代了,npm install -g ganache-cli
# 安装 truffle #这个可以通过truffle进行查看当前的链内容,也可以通过truffle发币
npm install -g truffle
用testrpc
模拟一个本地的私有链:testrpc -p 8646 -i 5266
- -p 表示监听8646端口
- -i 表示networkid为5266,ethereum主网id为1.
用curl
发送post
请求,就可以看到我们用testrpc启动后,系统自动创建的10个账户
//获取所有account
curl 127.0.0.1:8646 -X POST --data '{"id":5266,"jsonrpc":"2.0","method":"eth_accounts", "params":[]}' -H "Content-type: application/json;charset=UTF-8"
用curl
发送post
请求,就可以看到0xc9fb925d5c347185ce39ce6b627268a516ce4315
账户剩下多少eth。
//获取单个用户的eth
curl -X POST --data '{"id":5266,"jsonrpc":"2.0","method":"eth_getBalance", "params":["0xc9fb925d5c347185ce39ce6b627268a516ce4315","latest"]}' 127.0.0.1:8646 -H "Content-type: application/json;charset=UTF-8"
创建一个账号:
curl 127.0.0.1:8545 -X POST --data '{"id":5266,"jsonrpc":"2.0","method":"personal_newAccount", "params":["yourAccountPassword"]}' -H "Content-type: application/json;charset=UTF-8"
//yourAccountPassword是字母和数字的结合最好
来一段php代码:
<?php
//header("Content-type:application/json;charset=utf-8");
function http_post_data($url, $data_string)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
"Content-Type: application/json; charset=utf-8",
"Content-Length: " . strlen($data_string)]
);
ob_start();
curl_exec($ch);
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return [$return_code, $return_content];
}
//curl -X POST --data '{"id":5266,"jsonrpc":"2.0","method":"eth_getBalance", "params":["0xc9fb925d5c347185ce39ce6b627268a516ce4315","latest"]}' 127.0.0.1:8646
$params = ["0x41de94fb75cc86856306433340430d1ae63dc30c", "latest"];
$data = ["id" => 5266, "jsonrpc" => "2.0", "method" => "eth_getBalance", 'params' => $params];
$url = '127.0.0.1:8646';
$data = json_encode($data);
list($return_code, $return_content) = http_post_data($url, $data); //return_code是http状态码
var_dump($return_content);
怎么样?简单吧?来,鼓个掌。!!!!
注:也可以通过goeth来启动一个私链:
echo "===== Starting geth node =====";
set -x;
nohup geth --datadir $GETH_HOME -verbosity $VERBOSITY --bootnodes $BOOTNODE_URLS --maxpeers $MAX_PEERS --nat none --networkid $NETWORK_ID --identity $IDENTITY $MINE_OPTIONS $FAST_SYNC --rpc --rpcaddr "$IPADDR" --rpccorsdomain "*" --rpcapi "eth,net,web3,admin,personal" >> $GETH_LOG_FILE_PATH 2>&1 &
if [ $? -ne 0 ]; then echo "Previous command failed. Exiting"; exit $?; fi
set +x;
echo "===== Started geth node =====";
以太坊官方JSON-RPC:
https://github.com/ethereum/wiki/wiki/JSON-RPC
https://blogs.msdn.microsoft.com/pkirchner/2017/07/12/getting-started-with-ethereum-tutorials-on-azure/
搜索
标签
study
ab
amap
apache
apahe
awk
aws
bat
centos
CFS
chrome
cmd
cnpm
composer
consul
crontab
css
curl
cygwin
devops
di
docker
docker,docker-compose
ethereum
excel
fiddler
fluentd
framework
front-end
git
gitgui
github
glide
go
golang
gorm
grafana
gzip
ioc
item2
iterm2
javascript
jenkins
jsonp
kafka
laradock
laravel
larval
linux
liunux
log
mac
mac, wi-fi
macos
magento
mariaDB
minikube
mongoDB
msp
mysql
netbeans
nginx
nodejs
nohup
npm
nsq
php
php-fpm
php7
phpstorm
php扩展
Protobuf
python
redis
scp
server
shell
soap
socket
socket5
sql
sre
ssdb
ssh
ssl
study
sublime
swift
system
td-agent
uml
v2ray
vagrant
vagrnat
vim
vpn
vue
vue.js
webpack
webrtc
websocket
webtatic
windows
windows7
word
wps
xdebug
yarn
yii2
yum
zookeeper
世界国家
互联网
以太坊
分类
前端
小程序
打印机
排序算法
搞笑
权限
粤语
缓存
网络
虚拟机
视频
设计模式
项目管理
热门文章
友情链接