Published 10 December 2018

Using Credits API in C++ (demo). Cmake. Cross platform

Requirements

Requirements

Windows x64

Linux

API

Download Credits API files:https://github.com/CREDITSCOM/CS-API

Boost

Download and combine: https://www.boost.org/

Thrift

Download and combine: https://thrift.apache.org/

Combining of application

Combining of application

File created for OC Linux: build64.sh  

rm -r -f build64

rm -r -f CS-API

rm -r -f thrift

rm API.cpp

rm api_constants.cpp

rm API_server.skeleton.cpp

rm api_types.cpp

rm variant_constants.cpp

rm variant_types.cpp

rm API.h

rm api_constants.h

rm api_types.h

rm variant_constants.h

rm variant_types.h

 

git clone https://github.com/CREDITSCOM/CS-API

thrift --gen cpp -out . ./CS-API/api.thrift

thrift --gen cpp -out . ./CS-API/variant.thrift

 

git clone https://github.com/apache/thrift.git

mkdir build64

cd build64

cmake ..

cmake --build .

File created for OC Windows: build64.cmd

rmdir /S /Q build64

rmdir /S /Q CS-API

rmdir /S /Q thrift

del /Q API.cpp

del /Q api_constants.cpp

del /Q API_server.skeleton.cpp

del /Q api_types.cpp

del /Q variant_constants.cpp

del /Q variant_types.cpp

del /Q API.h

del /Q api_constants.h

del /Q api_types.h

del /Q variant_constants.h

del /Q variant_types.h

 

git clone https://github.com/CREDITSCOM/CS-API

thrift.exe --gen cpp -out . .\CS-API\api.thrift

thrift.exe --gen cpp -out . .\CS-API\variant.thrift

 

git clone https://github.com/apache/thrift.git

mkdir build64

cd build64

cmake .. -A x64

cmake --build .

CMakeLists.txt

CMakeLists.txt

In this file it is assumed that:

Windows, Boost downloaded in d:/boost

Linux< Boost downloaded in ~/boost

cmake_minimum_required(VERSION 3.5 FATAL_ERROR)

 

set(CMAKE_CXX_STANDARD 14)

set(CMAKE_CXX_STANDARD_REQUIRED ON)

 

if (MSVC)

   set(BOOST_ROOT d:/boost)

else()

   set(BOOST_ROOT ~/boost)

endif()

 

if (MSVC)

   add_definitions(-DNOMINMAX)

endif()

 

add_definitions(-DUSE_STD_THREAD)

if (MSVC)

   option(BUILD_COMPILER "" OFF)

endif(MSVC)

option(WITH_BOOST_STATIC "" ON)

option(BUILD_TESTING "" OFF)

option(BUILD_TUTORIALS "" OFF)

option(BUILD_EXAMPLES "" OFF)

option(BUILD_PYTHON "" OFF)

option(WITH_SHARED_LIB "" OFF)

option(WITH_STATIC_LIB "" ON)

option(WITH_C_GLIB "" OFF)

option(WITH_JAVA "" OFF)

option(WITH_PYTHON "" OFF)

option(WITH_QT4 "" OFF)

option(WITH_QT5 "" OFF)

option(WITH_ZLIB "" OFF)

option(WITH_STDTHREADS "" ON)

option(WITH_LIBEVENT "" OFF)

option(WITH_OPENSSL "" OFF)

add_subdirectory(thrift)

 

find_package(Boost 1.68 REQUIRED)

 

include_directories(${Boost_INCLUDE_DIRS})

include_directories("./thrift/lib/cpp/src")

 

add_executable(main

   api_constants.h

   api_types.h

   API.h

   base58.h

   variant_constants.h

   variant_types.h

   api_constants.cpp

   api_types.cpp

   API.cpp

   base58.cpp

   variant_constants.cpp

   variant_types.cpp

   variant_aux.cpp

   main.cpp

)

 

target_link_libraries(main ${Boost_LIBRARIES})

target_link_libraries(main thrift_static)

Application development

Application development

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())

   {

      std::cout << "Transport was opened" << std::endl;

 

      const char* rs = "H5ptdUUfjJBGiK2X3gN2EzNYxituCUUnXv2tiMdQKP3b";

      std::string str = EncodeBase58((unsigned char*)rs, (unsigned char*)rs + strlen(rs));

 

      std::vector<unsigned char> vec;

      auto arr = DecodeBase58(rs, vec);

      std::string dst(vec.size(), 0);

      memcpy_s((void*)dst.c_str(), vec.size(), &(vec[0]), vec.size());

 

      Address          addr = dst;

      Currency         cur = 'cs';

      BalanceGetResult bg_res;

      try

      {

         api->BalanceGet(bg_res, addr, cur);

      }

      catch (...)

      {

         return 1;

      }

 

      bg_res.printTo(std::cout);

      std::cout << std::endl << std::endl;

 

      transport->close();

   }

}

Operational capability test. GetBalance method call.

Operational capability test. GetBalance method call.

Program results. We see previously requested balance

Link on Github

12 Useful article

Contents