前前后后大约对接了百来种虚拟币了,一直没有记录的习惯,虽然很多重复的类似的币(节点和api类似btc或eth或xrp),但是也有很多不同的币
陆续发现随着时间一些以前接的慢慢会想不起来如何维护,一些坑需要很久才能回忆,或者一些老节点的升级到手上一脸蒙
有些节点的文档还非常难找,找到第一次结果就找不到第二次了,于是决定好好开始记录,方便一些较冷门的币种有些同志不知如何对接可以有个参考
今天对接的是Theta,文档还算全,参考这里的官方文档 :https://github.com/thetatoken/guardian-mainnet-guide
首先要有个服务器 推荐linux系统,我个人是ubuntu18.04 因为16.04一些比较新的节点会无法运行
下面命令按序执行看,简单粗暴
screen -S theta_mainnet # 一般在linux上用screen进行需要保持前台进行的程序(看运行状态)比较方便
mkdir ~/theta_mainnet
cd ~/theta_mainnet
mkdir bin
mkdir -p guardian_mainnet/node
curl -k --output bin/theta `curl -k 'https://mainnet-data.thetatoken.org/binary?os=linux&name=theta'`
curl -k --output bin/thetacli `curl -k 'https://mainnet-data.thetatoken.org/binary?os=linux&name=thetacli'`
wget -O guardian_mainnet/node/snapshot `curl -k https://mainnet-data.thetatoken.org/snapshot`
curl -k --output guardian_mainnet/node/config.yaml `curl -k 'https://mainnet-data.thetatoken.org/config?is_guardian=true'`
chmod +x bin/theta
chmod +x bin/thetacli
cd bin/
ls
这时候就看见theta thetacli两个程序,准备工作完成.
然后进入~/theta_mainnet/guardian_mainnet/node 查看一下config.yaml 可以看到节点的rpc端口是16888 地址默认是127.0.0.1 可以改成其他的
回到 theta_mainnet/bin目录 ,执行
./theta start --config=../guardian_mainnet/node
开始跑节点,但是文档里没有说明节点生成的文档在哪,有点坑,
执行
./theta --help
Available Commands:
help Help about any command
init Initialize Theta node configuration.
start Start Theta node.
version Print version of current Theta binary.
Flags:
--chain_correction string chain correction path
--chain_import string chain import path
--config string config path (default is /root/.theta)
--data string data path (default to config path)
-h, --help help for theta
--key string key path (default to config path)
--password string password for the node
--snapshot string snapshot path
可以看到默认的data位置是--config所在的位置,这里我们要修改一下,因为区块链的data一般是不断增长的,为了扩容云服务器方便,我们用的磁盘都是外挂磁盘,方便扩容
这里我所用的位置是/root/theta-data 一个简单粗暴的方式就是直接把guardian_mainnet这个文件夹直接移动到/root/theta-data,不过我没有这样做,我还是老老实实用参数
第一次启动会让你设置节点密码,暂时不知道有啥用,但是每次启动都要输入会很烦,这里可以直接也用参数
启动:
./theta start --config=../guardian_mainnet/node --data /root/theta-data --password '123123'
可以看到节点再跑了.这时候按ctrl+a+d 切出这个screen,这里screen的优点就体现出来了.(输入screen -r theta_mainnet可以回去)
执行下面命令查看一下节点同步状态:
./thetacli query status
这里有一个坑,因为一开始是通过快照同步的,所以一开始的时候这个命令会提示连接被拒绝,用命令netstat -tunpl |grep 16888可以看到端口没有监听,等到端口监听开始了就可以使用命令了
一般来说,对接需要的rpc是这几个:生成地址,转账,扫描区块和交易详情,查询余额
在theta里,对应的是这几个: (参考文档:https://docs.thetatoken.org/docs/rpc-api-reference)
// 生成地址
curl -X POST -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"thetacli.NewKey","params":[{"password":"qwertyuiop"}],"id":1}' http://localhost:16889/rpc
// Result
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"address": "0x8318dd49f83A2684E30d5fB50cD0D3D69aA82EAd"
}
}
// 根据高度扫块
curl -X POST -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"theta.GetBlockByHeight","params":[{"height":"3"}],"id":1}' http://localhost:16888/rpc
// Result
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"chain_id": "privatenet",
"epoch": "5",
"height": "3",
"parent": "0x724b0f68d8e45f930b95bac224fa7d67eef243307b4e84f0f666198d1d70e9d7",
"transactions_hash": "0x2bf2c62185fceed239a55bd27ada030cf75970f09122addb2e419e70cafebdf0",
"state_hash": "0xd41742c2b0d70e3bac1d88b2af69a2491d8c65c650af6ec4d2b8873897f8becc",
"timestamp": "1548102762",
"proposer": "0x2e833968e5bb786ae419c4d13189fb081cc43bab",
"children": ["0x21d3c2bb25d0c85a1f5c3ff81bc7eeae998bf98db1dba461fb3f69a434feb90c"],
"status": 4,
"hash": "0x9f1e77b08c9fa8984096a735d0aae6b0e43aee297e42c54ce36334103ddd67a7",
"transactions": [...]
}
}
// 根据txhash查询交易详情
curl -X POST -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"theta.GetTransaction","params":[{"hash":"0xf3cc94af7a1520b384999ad106ade9738b6cde66e2377ceab37067329d7173a0"}],"id":1}' http://localhost:16888/rpc
// Result
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"block_hash": "0x9f1e77b08c9fa8984096a735d0aae6b0e43aee297e42c54ce36334103ddd67a7",
"block_height": "3",
"status": "finalized",
"hash": "0xf3cc94af7a1520b384999ad106ade9738b6cde66e2377ceab37067329d7173a0",
"transaction": {
"fee": {
"thetawei": "0",
"tfuelwei": "1000000000000"
},
"inputs": [{
"address": "0x2e833968e5bb786ae419c4d13189fb081cc43bab",
"coins": {
"thetawei": "10000000000000000000",
"tfuelwei": "20000001000000000000"
},
"sequence": "1",
"signature": "0x2f8f17b13c07e57d4c5d2c89e87d9e608f0eff22ef1f96eed5647b063265450216ef4f7a8578bf702cf26db00fb2e758521873bb1b68528325c84b59a2debc7400"
}],
"outputs": [{
"address": "0x9f1233798e905e173560071255140b4a8abd3ec6",
"coins": {
"thetawei": "10000000000000000000",
"tfuelwei": "20000000000000000000"
}
}]
}
}
}
// 发起交易请求 异步
curl -X POST -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"thetacli.Send","params":[{"chain_id":"privatenet", "from":"0x2E833968E5bB786Ae419c4d13189fB081Cc43bab", "to":"0xA47B89c94a50C32CEACE9cF64340C4Dce6E5EcC6", "thetawei":"99000000000000000000", "tfuelwei":"88000000000000000000", "fee":"1000000000000", "sequence":"6", "async":true}],"id":1}' http://localhost:16889/rpc
// Async Result
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"hash": "0xe3e82ae1e08ca49f85842729bd3c70ba0874d59cca3812fe0807506463851d22",
"block": null
}
}
// 发起交易请求 同步
curl -X POST -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"thetacli.Send","params":[{"chain_id":"privatenet", "from":"0x2E833968E5bB786Ae419c4d13189fB081Cc43bab", "to":"0xA47B89c94a50C32CEACE9cF64340C4Dce6E5EcC6", "thetawei":"99000000000000000000", "tfuelwei":"88000000000000000000", "fee":"1000000000000", "sequence":"7", "async":false}],"id":1}' http://localhost:16889/rpc
// Sync Result
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"hash": "0xd4dfa1b763cac0c18c816e31ff585c01f2c4f2604dfff01cb6638d6d19e1bd1e",
"block": {
"ChainID": "privatenet",
"Epoch": 170511,
"Height": 170472,
"Parent": "0xe36483c52eeee44634038252bb33dfe6b70c439c94c89236c6f18c1a4a676e01",
"HCC": {
"Votes": {},
"BlockHash": "0xe36483c52eeee44634038252bb33dfe6b70c439c94c89236c6f18c1a4a676e01"
},
"TxHash": "0xff0d7f1bd6aa699369a935de46c287b30917958c6dbd2d542d31548eaf279525",
"ReceiptHash": "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
"Bloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"StateHash": "0x49374ccea64633a51358816d29a55f372d0fbe7e2f9cb1447ddd2c7519fc17e5",
"Timestamp": 1550129583,
"Proposer": "0x2e833968e5bb786ae419c4d13189fb081cc43bab",
"Signature": "0x96dad1ff2ccc4eb2e18ca99f488cb9d6e6f3333c53a3347d7f8ded5f7301b613763bf0301a85b8f8e7342858c29dd1b262b1b6bdb9552fa5c9b6b61a9b4f6d5c01"
}
}
}
// 查询余额
curl -X POST -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"theta.GetAccount","params":[{"address":"0x2E833968E5bB786Ae419c4d13189fB081Cc43bab"}],"id":1}' http://localhost:16888/rpc
// Result
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"sequence": "1",
"coins": {
"thetawei": "994999990000000000000000000",
"tfuelwei": "4999999979999999000000000000"
},
"reserved_funds": [],
"last_updated_block_height": "0",
"root": "0x0000000000000000000000000000000000000000000000000000000000000000",
"code": "0x0000000000000000000000000000000000000000000000000000000000000000"
}
}
不过在转账的时候不能直接用密码解锁钱包,所以就还有下面这一步,在转账之前执行:
// 解锁钱包
curl -X POST -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"thetacli.UnlockKey","params":[{"address":"0x2E833968E5bB786Ae419c4d13189fB081Cc43bab", "password":"qwertyuiop"}],"id":1}' http://localhost:16889/rpc
// Result
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"unlocked": true
}
}
//转账完后锁定钱包
curl -X POST -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"thetacli.LockKey","params":[{"address":"0x2E833968E5bB786Ae419c4d13189fB081Cc43bab"}],"id":1}' http://localhost:16889/rpc
// Result
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"unlocked": false
}
}
目前就先记录这么多,如果又其他坑我再进行编辑
补充:Theta的涉及钱包等的rpc操作需要独立启动cli 的rpc服务,端口默认16889:
./thetacli daemon start --port xxx
后来发现获取最新高度需要用到cli里的query status方法,rpc文档里并没有,通过观察cli 的query account方法和比对rpc里的theta.GetAccount方法后,测试得出获取最新状态的方法是:
//获取最新状态
curl -X POST -H 'Content-Type: application/json' --data '{"jsonrpc":"2.0","method":"theta.GetStatus","params":[],"id":1}' http://localhost:16888/rpc
// Result
{
"jsonrpc": "2.0",
"id": 1,
"result": {
"address": "0x111111111122312231312",
"chain_id": "mainnet",
"peer_id": "0x111111111122312231312",
"latest_finalized_block_hash": "0x17ae171779d9886b506b115eea98103673c78610fc69a928a7e2c3943bda71c4",
"latest_finalized_block_height": "8749992",
"latest_finalized_block_time": "1609998423",
"latest_finalized_block_epoch": "8822291",
"current_epoch": "8822293",
"current_height": "8749992",
"current_time": "1609998509",
"syncing": true
}
}