sudo apt-get install libboost-all-dev
sudo apt-get -y install build-essential cmake
sudo apt-get install libcurl4-openssl-dev
sudo apt-get install doxygen python-pydot python-pydot-ng graphviz
make deps
This is work in progress, so installation is only possible from the source code. Here are instructions how to compile the library and run examples:
git clone https://github.com/usetech-llc/polkadot_api_cpp
cd polkadot_api_cpp
cmake .
make
sudo make install
make clip
Connect example will establish WebSocket connection to the polkadot node and read and output basic information.
bin/clip connect
Balance example will establish WebSocket connection and read and output address balance in DOTs.
bin/clip balance <your polkadot address>
trasnfer <from address> <to address> <amount> <private key file>
make test
#include <polkadotcpp/polkadot.h>
/usr/lib/polkadotcpp/libpolkacpp.a
Parity node URL is the only required parameter, though URL must include port. Example:
auto api = polkadot::api::getInstance()->app();
This call will establish connection and start message thread. It will also ping the node several times per minute with "health" request to keep the connection alive.
api->connect("wss://poc3-rpc.polkadot.io:443/");
The data is returned as C structures. This example shows how to read (and output) system info:
auto systemInfo = api->getSystemInfo();
cout << " Chain ID : " << systemInfo->chainId << endl
<< " Chain Name : " << systemInfo->chainName << endl
<< " Token Decimals : " << systemInfo->tokenDecimals << endl
<< " Token Symbol : " << systemInfo->tokenSymbol << endl;
Subscribing is done by calling a matching subscribe method with parameters (if needed) and a callback (or a lambda function). This example subscribes for balance updates and prints new balance when it arrives:
api->subscribeBalance(address, [&](uint128 balance) {
cout << endl << " Balance: " << (uint64_t)balance << endl << endl;
});
In order to stop subscription, call matching unsubsribe method:
api->unsubscribeBalance();
This example sends some DOTs between addresses and waits for execution. Transaction is signed with private key.
api->signAndSendTransfer(senderAddr, senderPrivateKeyStr, recipientAddr, amount, [&](string result) {
if (result == "ready")
cout << endl << endl << " ---=== Transaction was registered in network ===--- " << endl << endl << endl;
if (result == "finalized") {
cout << endl << endl << " ---=== Transaction was mined! ===--- " << endl << endl << endl;
done = true;
}
});
When connection is not needed anymore, call
api->disconnect();
The complete documentation of API interface can be found at this starting point: https://htmlpreview.github.io/?https://github.com/usetech-llc/polkadot_api_cpp/blob/master/doc/html/classIApplication.html
For more examples see:
- examples folder,
- test folder with unit and E2E tests,
- polkadot_ui repository, which implements a GTK 3.0 UI and uses this API.