Skip to content

Commit

Permalink
fixup! feat: add provider interface
Browse files Browse the repository at this point in the history
  • Loading branch information
AngelCastilloB committed Sep 29, 2024
1 parent 79fbf5f commit 553f687
Show file tree
Hide file tree
Showing 29 changed files with 1,939 additions and 373 deletions.
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ SET(PROJECT_NAME "cardano-c")
# Option that the user can optionally select
OPTION (TESTING_ENABLED "Enables unit test build." ON)
OPTION (DOXYGEN_ENABLED "Build documentation" OFF)
OPTION (EXAMPLES_ENABLED "Build examples" OFF)

# Find external dependencies
LIST (APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake)
Expand Down Expand Up @@ -67,6 +68,7 @@ MESSAGE ( STATUS "CMAKE_PREFIX_PATH = ${CMAKE_PREFIX_PATH}" )
MESSAGE ( STATUS "ARCHITECTURE = ${ARCHITECTURE}" )
MESSAGE ( STATUS "TESTING ENABLED = ${TESTING_ENABLED}")
MESSAGE ( STATUS "DOXYGEN ENABLED = ${DOXYGEN_ENABLED}")
MESSAGE ( STATUS "EXAMPLES ENABLED = ${EXAMPLES_ENABLED}")
MESSAGE ( STATUS "CMAKE_C_CLANG_TIDY = ${CMAKE_C_CLANG_TIDY}")
MESSAGE ( STATUS )
MESSAGE ( STATUS "change a configuration variable with: cmake -D<Variable>=<Value>" )
Expand Down Expand Up @@ -159,6 +161,10 @@ IF (CMAKE_BUILD_TYPE STREQUAL "Fuzz")
ADD_SUBDIRECTORY (fuzz ${BUILD_DIR}/fuzz)
ENDIF ()

# Build examples
IF (EXAMPLES_ENABLED)
ADD_SUBDIRECTORY (examples ${BUILD_DIR}/examples)
ENDIF ()

# Packaging.
SET (CPACK_SOURCE_IGNORE_FILES ".git,.swp")
Expand Down
4 changes: 4 additions & 0 deletions doc/src/sections/api/providers/provider.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ Provider

------------

.. doxygenfunction:: cardano_provider_rewards_balance

------------

.. doxygenfunction:: cardano_provider_get_unspent_outputs_with_asset

------------
Expand Down
4 changes: 4 additions & 0 deletions doc/src/sections/api/providers/provider_impl.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ Provider Implementation

------------

.. doxygentypedef:: cardano_get_rewards_balance_func_t

------------

.. doxygentypedef:: cardano_get_unspent_outputs_with_asset_func_t

------------
Expand Down
20 changes: 20 additions & 0 deletions examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#Detect all examples targets
FILE(GLOB EXAMPLE_SOURCES "*_example.c")
MESSAGE(STATUS "Example sources: ${EXAMPLE_SOURCES}")
FILE (GLOB_RECURSE PROVIDERS_HEADER_FILES providers/*.h include/*.inl)
FILE (GLOB_RECURSE PROVIDERS_SOURCE_FILES providers/*.c)

FIND_PACKAGE(CURL REQUIRED)

FOREACH(EXAMPLE_SOURCE ${EXAMPLE_SOURCES})
GET_FILENAME_COMPONENT(EXAMPLE_TARGET_NAME ${EXAMPLE_SOURCE} NAME_WE) # Extracts the filename without extension

# Define executable target for the example
ADD_EXECUTABLE(${EXAMPLE_TARGET_NAME} ${EXAMPLE_SOURCE} ${PROVIDERS_SOURCE_FILES})
MESSAGE(STATUS "Example source: ${EXAMPLE_TARGET_NAME}")

# Include directories
TARGET_INCLUDE_DIRECTORIES(${EXAMPLE_TARGET_NAME} PRIVATE ${CARDANO_C_INCLUDE_DIR})
TARGET_INCLUDE_DIRECTORIES(${EXAMPLE_TARGET_NAME} PRIVATE ${PROVIDERS_HEADER_FILES})
TARGET_LINK_LIBRARIES(${EXAMPLE_TARGET_NAME} PRIVATE cardano-c ${CURL_LIBRARIES})
ENDFOREACH()
41 changes: 41 additions & 0 deletions examples/provider_example.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include "providers/provider_factory.h"
#include <cardano/cardano.h>
#include <stdio.h>
#include <string.h>

int
main()
{
const char* api_key = "preprodeMB9jfka6qXsluxEhPLhKczRdaC5QKab";
printf("libcardano-c %s\n", cardano_get_lib_version());

cardano_provider_t* provider = NULL;

cardano_error_t result = create_blockfrost_provider(CARDANO_NETWORK_MAGIC_PREPROD, api_key, strlen(api_key), &provider);

if (result != CARDANO_SUCCESS)
{
printf("Failed to create blockfrost provider: %s\n", cardano_provider_get_last_error(provider));
return 1;
}

printf("Provider name: %s\n", cardano_provider_get_name(provider));

cardano_protocol_parameters_t* parameters = NULL;

result = cardano_provider_get_parameters(provider, &parameters);

if (result != CARDANO_SUCCESS)
{
printf("Failed to get protocol parameters:\n%s: %s\n", cardano_error_to_string(result), cardano_provider_get_last_error(provider));
return 1;
}

printf("Protocol parameters:\n");
printf(" - ADA per utxo byte: %zu\n", cardano_protocol_parameters_get_ada_per_utxo_byte(parameters));

cardano_protocol_parameters_unref(&parameters);
cardano_provider_unref(&provider);

return 0;
}
Loading

0 comments on commit 553f687

Please sign in to comment.