Using of CREDITS API in Visual C++ (demo)

Requirements

Windows x64

Download CREDITS API files: https://github.com/CREDITSCOM/thrift-interface-definitions

To install additional libraries use the package manager: https://github.com/Microsoft/vcpkg

Install additional libraries:

boost (windows): vcpkg install boost: x64-windows

thrift (windows): vcpkg install thrift: x64-windows

Create a console application in Visual Studio

CREDITS API Source Code Generation

Open the folder which you have used for download of Credits API from the GitHub, for example, in d: \ credits \ github \ CS-API \

Let's say thrift is installed in the folder: d: \ vcpkg \

execute commands:

D: \ vcpkg \ packages \ thrift_x64-windows \ tools \ thrift \ thrift.exe --gen cpp api.thrift

D: \ vcpkg \ packages \ thrift_x64-windows \ tools \ thrift \ thrift.exe --gen cpp variant.thrift

As the result you will get a folder with files: gen-cpp,

copy the files from the folder to the folder of our application, for example in: d: \ credits \ app \ app \

Add files to our project


Make changes to source files

Add line #define NOMINMAX to pch.h

add to all * .cpp files a line at the beginning of the file: #include “pch.h”

go to: https://developers.credits.com/en/Articles/a76

copy the snippet and paste it into the main function

Next, go to the address: https://developers.credits.com/en/Articles/a77, copy the snippet and paste instead of the line “// do something”

change the port 9090 to 9080

The result is shown on the codes:

#include "pch.h" #include <iostream> #include <thrift\stdcxx.h> #include <thrift\transport\TSocket.h> #include <thrift\transport\TBufferTransports.h> #include <thrift\protocol\TBinaryProtocol.h> #include "base58.h" #include "API.h" using namespace apache::thrift::transport; using namespace apache::thrift::protocol; using namespace api; int main() { std::shared_ptr<TSocket> socket = std::shared_ptr<TSocket>(new TSocket("127.0.0.1", 9090)); std::shared_ptr<TTransport> transport = std::shared_ptr<TTransport>(new TBufferedTransport(socket)); std::shared_ptr<TProtocol> protocol = std::shared_ptr<TProtocol>(new TBinaryProtocol(transport)); std::shared_ptr<APIClient> api = std::shared_ptr<APIClient>(new APIClient(protocol)); try { transport->open(); } catch (...) { printf("thrift error: failed connect to node\n"); } if (transport->isOpen()) { /** api code begin */ // do something /** api code end */ transport->close(); } return 0; } ****************************************************************************************************** if (transport->isOpen()) { std::cout << "Transport was opened" << std::endl; /** api code begin */ /** BalanceGet begin */ const char* rs = "H5ptdUUfjJBGiK2X3gN2EzNYxituCUUnXv2tiMdQKP3b"; std::string str = EncodeBase58((unsigned char*)rs, (unsigned char*)rs + strlen(rs)); std::vector<unsigned char> vvv; auto arr = DecodeBase58(rs, vvv); std::string dst(vvv.size(), 0); memcpy_s((void*)dst.c_str(), vvv.size(), &(vvv[0]), vvv.size()); Address addr = dst;// "H5ptdUUfjJBGiK2X3gN2EzNYxituCUUnXv2tiMdQKP3b"; Currency cur = 'cs'; BalanceGetResult bg_res; TransactionGetResult t_res; TransactionsGetResult tr_res; try { api->BalanceGet(bg_res, addr, cur); //api->TransactionsGet(tr_res, addr, 0, 10); } catch (...) { return 1; } bg_res.printTo(std::cout); std::cout << std::endl << std::endl; //tr_res.printTo(std::cout); //std::cout << std::endl << std::endl; /** BalanceGet end */ // do something /** api code end */ transport->close(); }

Due to a thrift generator error do the following


create a stub.

Check the performance. Call the GetBalance Method

As the result of the program we see the requested balance:


GitHub link: https://github.com/pvl1175/CreditsCPlusPlusDemo