How to Use Infura Conflux API?
How to Use Infura Conflux API?
It's cool to build your Dapp on Conflux, then how do you get it started? Tokenview launched Conflux Infura Services which make you develop Conflux Dapp very easily. First, you need to sign up for a Tokenview developer account; then, choose the interface you use, which currently supports the JSON-RPC interface.
The following will guide you on how to use these interfaces and how to develop your Conflux Dapp using Node.js.
JSON-RPC
You can communicate with Conflux node by calpng the JSON-RPC interface through HTTPS, which is a stateless call, and when your call ends, the connection closes.
Simulate JSON-RPC Request
You can use Postman to call JSON-RPC, together with your apikey and your Conflux node URL information. Now let's call the API「 cfx_getBlockByHash 」to get the information of a block hash. For example:
0x02f0f3ec83331b95d461b08f3e30bb3e4effe37917123f2cdd932a9c35fefd83
Enter the Confux node URL path in Postman:
https://developer.tokenview.io/mainnet/conflux?apikey={YOUR_APIKEY}
Select the POST method, 'Raw' in Body, 'JSON (apppcation/json)' format, and fill in your request parameters in JSON format.
Notes for each parameter:
{
'jsonrpc':'2.0', // Version,
'method':'cfx_getBlockByHash', // API name
'params':['0x02f0f3ec83331b95d461b08f3e30bb3e4effe37917123f2cdd932a9c35fefd83',true], // Parameter
'id':1
}
Click 'Send' to send the request, and you will see the result returned from Tokenview Conflux Infura.
Send JSON-RPC requests using Node.js
Let's use Node.js to call the Conflux Infura node URL through the code. Let’s calpng the interface「 cfx_getBlockByHash 」 to get a block hash, same as above,
0x02f0f3ec83331b95d461b08f3e30bb3e4effe37917123f2cdd932a9c35fefd83
The steps are as follows:
Install npm.
create file app.js
Typing in these codes:
start a command window, and execute
node app.js
, you will get the following result:
Conflux Truffle
Let's use conflux truffle to initialize the connection to the Conflux node and implement a contract deployment.
Open the command window and execute the following command:
Execute
npm install -g conflux-truffle
to install conflux truffle;After the installation is complete, execute
cfxtruffle -v
to check the success of the installation;Install conflux portal to create a conflux wallet, charge a fee for filling 10 cfx, deployment contracts, and then get your private key;
Execute the command
cfxtruffle init cfxfirstproject
to create the cfxtruffle project;Modify the configuration information of the truffle-config.js file to connect the Conflux node.
Execute
cfxtruffle create contract Coin
to create the contract;Execute
cfxtruffle create migration Coin
to generate the deployment script;Execute
cfxtruffle compile
to compile the contract;Execute
cfxtruffle deploy
to deploy the contract.
Thus, you have completed the process of creating and deploying your smart contract using Tokenview Conflux Infura, isn't it cool?
Go to Tokenview API Service system to get your own apikey and start to create your Conflux Dapp Now!