在Conflux上构建你的Dapp是一件很酷的事情,那么如何开始呢?Tokenview 推出了 Conflux Infura Services。因此,你可以非常简单的开发 Conflux Dapp。首先,你需要注册一个Tokenview 开发者账户;然后,选择你使用哪种接口,目前支持JSON-RPC接口。以下文字将引导你如何使用这些接口以及如何使用Node.js开发你的Conflux Dapp。
- JSON-RPC
- Conflux truffle
JSON-RPC
你可以通过HTTPS调用JSON-RPC接口来和Conflux节点进行沟通,这种方式的调用是无状态调用,当你的调用结束时,连接随即关闭。
模拟JSON-RPC请求
你可以使用 Postman 进行JSON-RPC的调用,使用你的 apikey 以及你的 Conflux 节点URL信息,现在让我们调用接口「 cfx_getBlockByHash 」获取某个区块 hash 的信息,如:
0x02f0f3ec83331b95d461b08f3e30bb3e4effe37917123f2cdd932a9c35fefd83 。
在Postman中填入Confux节点 URL 路径:
https://developer.tokenview.com/mainnet/conflux?apikey={YOUR_APIKEY}
选择 POST 方法,Body 里选择 “Raw”,格式选择 ”JSON(application/json)”,按照JSON格式填写你的请求参数。
各参数注解:
{
"jsonrpc":"2.0", // 版本信息,
"method":"cfx_getBlockByHash", // 接口名称
"params":["0x02f0f3ec83331b95d461b08f3e30bb3e4effe37917123f2cdd932a9c35fefd83",true], // 接口参数
"id":1
}
点击"Send"发送请求,你会看到 Conflux Infura 返回的接口信息。
{
"jsonrpc": "2.0",
"result": {
"adaptive": false,
"blame": "0x0",
"custom": [
[
1
]
],
"deferredLogsBloomHash": "0xd397b3b043d87fcd6fad1291ff0bfd16401c274896d8c63a923727f077b8e0b5",
"deferredReceiptsRoot": "0x12af19d53c378426ebe08ad33e48caf3efdaaade0994770c161c0637e65a6566",
"deferredStateRoot": "0xfe9218ef6cfcf818793fac10196d0cff62485340de4e1b0a4a6df2f3822400ad",
"difficulty": "0x3d063473a4",
"epochNumber": "0x500766",
"gasLimit": "0x1c9c380",
"gasUsed": "0x358e7",
"hash": "0x02f0f3ec83331b95d461b08f3e30bb3e4effe37917123f2cdd932a9c35fefd83",
"height": "0x500766",
"miner": "CFX:TYPE.USER:AAMGVYZHT7H1ZXDGHB9EE9W26WRZ8RD3GJ837392DP",
"nonce": "0x500b0000001106d3",
"parentHash": "0xe54157a4e60eea9096ced9f4e19d8b78ee54e519c725ac5c3d1ad86e9c43b04d",
"powQuality": "0x4e32212ed5",
"refereeHashes": [
"0xb035e549a9d08390bb190c3a2cd71fd4085c97dd6fa229d7292198e6d742c618"
],
"size": "0x4f9",
"timestamp": "0x5ff2d71a",
"transactions": [
{
"blockHash": "0x02f0f3ec83331b95d461b08f3e30bb3e4effe37917123f2cdd932a9c35fefd83",
"chainId": "0x405",
"contractCreated": null,
"data": "0xe7e0ec5d0000000000000000000000001e61c5dab363c1fdb903b61178b380d2cc7df9990000000000000000000000000000000000000000000000000002d79883d2000000000000000000000000000000000000000000000000003775f2c88af6e300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000176cc982a6200000000000000000000000083b17dc886a35015fdb5965ae1ed18eee018e4110000000000000000000000008287cb032e6e72ce5501d6148fda7148d25560640000000000000000000000001e61c5dab363c1fdb903b61178b380d2cc7df99900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c6bf526340000000000000000000000000001e61c5dab363c1fdb903b61178b380d2cc7df9990000000000000000000000000000000000000000000000000002d79883d2000000000000000000000000000000000000000000000000003775f2c88af6e300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000176cc982a6200000000000000000000000083b17dc886a35015fdb5965ae1ed18eee018e4110000000000000000000000008287cb032e6e72ce5501d6148fda7148d25560640000000000000000000000001e61c5dab363c1fdb903b61178b380d2cc7df99900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001c6bf52634000000000000000000000000000000000000000000000000000000000000000038000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000002d79883d20000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004163074d6e86161f059ebade4ea34c114b07eb41256b125f69b0920bb801f4f6101182375f2c1cccc488f85cde5ea7fb658d238fbe62dcd6fc85496a5fc66dcff61c00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004172970130e1c622567b1f2a7b344c68c22f14819e4aff6f1ab7c976807aad811105421bee8b4ea7f320d951e73282d797245c3f37a3a4cbccfa023e69b654bc271b00000000000000000000000000000000000000000000000000000000000000",
"epochHeight": "0x500762",
"from": "CFX:TYPE.USER:AAP6SU0S2UZ36X19HSCP55SR6N42YR1YK6R2RX2EH7",
"gas": "0x57e40",
"gasPrice": "0x1",
"hash": "0x933f94d5ef13a7ac9dbf96d4bffbe4c9ead6c40f914f8715375497e594e54728",
"nonce": "0x74594",
"r": "0xd48a44ff52d11eba7c1b387f9ef6712825ae5fe7a54879c0d2b77052d2160cb6",
"s": "0x2dd3f7a374fee0bef87f31278bf730e97ea0540890e0c544099e18e69f8cf03a",
"status": "0x0",
"storageLimit": "0x800",
"to": "CFX:TYPE.CONTRACT:ACCKUCYY5FHZKNBXMEEXWTAJ3BXMEG25B2B50PTA6V",
"transactionIndex": "0x0",
"v": "0x1",
"value": "0x0"
}
],
"transactionsRoot": "0x3a0efdf232d035a676027b143f5707cb77b1a1c2cfc0decd084b64ebf2b3313a"
},
"id": 1
}
使用Node.js发送JSON-RPC请求
下面我们使用Node.js通过代码来调用Conflux Infura节点URL。仍然调用接口「 cfx_getBlockByHash 」获取某个区块 hash,同上:0x02f0f3ec83331b95d461b08f3e30bb3e4effe37917123f2cdd932a9c35fefd83 。
具体步骤如下:
- 安装 npm。
- 创建 app.js 文件。
- 写入以下代码:
var request = require('request'); var headers = { 'Content-Type': 'application/json' }; var dataString = '{"jsonrpc":"2.0","method":"cfx_getBlockByHash","params":["0x02f0f3ec83331b95d461b08f3e30bb3e4effe37917123f2cdd932a9c35fefd83",true],"id":1}'; var options = { url: `https://developer.tokenview.com/mainnet/conflux?apikey=YOUR_APIKEY`, method: 'POST', headers: headers, body: dataString, }; function callback(error, response, body) { if (!error && response.statusCode == 200) { json = response.body; var obj = JSON.parse(json); console.log(obj); } else{ console.log(error); } } request(options, callback);
- 启动 command 窗口执行 node app.js ,得到如下结果:
{ "jsonrpc": "2.0",
"result":
{ "adaptive": false,
"blame": "0x0",
"custom": [ ["Array"] ],
"deferredLogsBloomHash":
"0xd397b3b043d87fcd6fad1291ff0bfd16401c274896d8c63a923727f077b8e0b5",
"deferredReceiptsRoot":
"0x12af19d53c378426ebe08ad33e48caf3efdaaade0994770c161c0637e65a6566",
"deferredStateRoot":
"0xfe9218ef6cfcf818793fac10196d0cff62485340de4e1b0a4a6df2f3822400ad",
"difficulty": "0x3d063473a4",
"epochNumber": "0x500766",
"gasLimit": "0x1c9c380",
"gasUsed": "0x358e7",
"hash":
"0x02f0f3ec83331b95d461b08f3e30bb3e4effe37917123f2cdd932a9c35fefd83",
"height": "0x500766",
"miner": "CFX:TYPE.USER:AAMGVYZHT7H1ZXDGHB9EE9W26WRZ8RD3GJ837392DP",
"nonce": "0x500b0000001106d3",
"parentHash":
"0xe54157a4e60eea9096ced9f4e19d8b78ee54e519c725ac5c3d1ad86e9c43b04d",
"powQuality": "0x4e32212ed5",
"refereeHashes":
[ "0xb035e549a9d08390bb190c3a2cd71fd4085c97dd6fa229d7292198e6d742c618" ],
"size": "0x4f9",
"timestamp": "0x5ff2d71a",
"transactions": [ ["Object"] ],
"transactionsRoot":
"0x3a0efdf232d035a676027b143f5707cb77b1a1c2cfc0decd084b64ebf2b3313a" },
"id": 1 }
Conflux truffle
下面我们使用conflux truffle来初始化到达Conflux节点的连接,并且实现一个合约的部署。请打开 command 窗口执行以下命令:
1. 使用 npm install -g conflux-truffle
命令来安装 conflux truffle。
2. 安装完成后使用 cfxtruffle -v
检测安装成功。
3. 安装 conflux portal 创建一个 conflux 的钱包,往里充入 10 个 cfx,部署合约的时候需要手续费,然后获取你的私钥。
4. 使用命令 cfxtruffle init cfxfirstproject
来创建 cfxtruffle 项目。
5. 修改 truffle-config.js 文件的配置信息来连接 Conflux 节点。
development: {
url: "https://developer.tokenview.com/mainnet/conflux?apikey=YOUR_APIKEY",
network_id: "1029",
type:"conflux",
privateKeys:["你的私钥"] // 注意从portal获取的私钥要加前缀0x
},
6. 运行 cfxtruffle create contract Coin 创建合约。
7. 运行 cfxtruffle create migration Coin 生成部署脚本。
8. 运行 cfxtruffle compile 编译合约。
9. 运行 cfxtruffle deploy 部署合约。
这样就完成了使用 Infura 的过程,是不是很酷呢,现在就去 Tokenview API 服务管理系统( https://services.tokenview.com/ )获取你自己的 apikey ,并创建你的 Conflux Dapp 吧!