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 Oct 1, 2024
1 parent 7850495 commit 598d626
Show file tree
Hide file tree
Showing 4 changed files with 914 additions and 29 deletions.
104 changes: 99 additions & 5 deletions examples/provider_example.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,51 @@

/* MAIN **********************************************************************/

static cardano_transaction_input_set_t*
get_inputs()
{
cardano_transaction_input_t* input1 = NULL;
cardano_transaction_input_t* input2 = NULL;
cardano_transaction_input_t* input3 = NULL;

cardano_cbor_reader_t* reader1 = cardano_cbor_reader_from_hex("825820f28133700ebd27592a9f6e680a6711831f06b50996165a14573bb7276015566d08", strlen("825820f28133700ebd27592a9f6e680a6711831f06b50996165a14573bb7276015566d08"));
cardano_cbor_reader_t* reader2 = cardano_cbor_reader_from_hex("825820052b9375e94317f05463d0d9b211921a2431cab49b3ce21ad37953e2b135987c1847", strlen("825820052b9375e94317f05463d0d9b211921a2431cab49b3ce21ad37953e2b135987c1847"));
cardano_cbor_reader_t* reader3 = cardano_cbor_reader_from_hex("825820141cbe81494e2dc55b7f0e79b3ffd68a034f8a7e758d4bcf4cdec61e40d11dcf1854", strlen("825820141cbe81494e2dc55b7f0e79b3ffd68a034f8a7e758d4bcf4cdec61e40d11dcf1854"));

cardano_error_t result = cardano_transaction_input_from_cbor(reader1, &input1);
CARDANO_UNUSED(result);

result = cardano_transaction_input_from_cbor(reader2, &input2);
CARDANO_UNUSED(result);

result = cardano_transaction_input_from_cbor(reader3, &input3);
CARDANO_UNUSED(result);

cardano_transaction_input_set_t* inputs = NULL;

result = cardano_transaction_input_set_new(&inputs);
CARDANO_UNUSED(result);

result = cardano_transaction_input_set_add(inputs, input1);
CARDANO_UNUSED(result);

result = cardano_transaction_input_set_add(inputs, input2);
CARDANO_UNUSED(result);

result = cardano_transaction_input_set_add(inputs, input3);
CARDANO_UNUSED(result);

cardano_transaction_input_unref(&input1);
cardano_transaction_input_unref(&input2);
cardano_transaction_input_unref(&input3);

cardano_cbor_reader_unref(&reader1);
cardano_cbor_reader_unref(&reader2);
cardano_cbor_reader_unref(&reader3);

return inputs;
}

/**
* \brief Entry point of the program.
*
Expand Down Expand Up @@ -62,12 +107,16 @@ main()

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

cardano_utxo_list_t* utxo_list = NULL;
cardano_address_t* address = NULL;
cardano_utxo_list_t* utxo_list = NULL;
cardano_utxo_t* utxo = NULL;
cardano_address_t* address = NULL;
cardano_protocol_parameters_t* parameters = NULL;
cardano_asset_id_t* asset_id = NULL;
cardano_transaction_input_set_t* inputs = get_inputs();

// addr_test1wppg9l6relcpls4u667twqyggkrpfrs5cdge9hhl9cv2upchtch0h
//addr_test1wrazf7es2yngqh8jzexpv8v99g88xvx0nz83le2cea755eqf68ll6
result = cardano_address_from_string("addr_test1wrazf7es2yngqh8jzexpv8v99g88xvx0nz83le2cea755eqf68ll6", strlen("addr_test1wrazf7es2yngqh8jzexpv8v99g88xvx0nz83le2cea755eqf68ll6"), &address);
// addr_test1wrazf7es2yngqh8jzexpv8v99g88xvx0nz83le2cea755eqf68ll6
result = cardano_address_from_string("addr_test1wppg9l6relcpls4u667twqyggkrpfrs5cdge9hhl9cv2upchtch0h", strlen("addr_test1wppg9l6relcpls4u667twqyggkrpfrs5cdge9hhl9cv2upchtch0h"), &address);

if (result != CARDANO_SUCCESS)
{
Expand All @@ -77,21 +126,66 @@ main()
return 1;
}

result = cardano_provider_get_unspent_outputs(provider, address, &utxo_list);
result = cardano_asset_id_from_hex("f8b0701b14c4e588a0f68e45d91d501c62580887dc1cc863d6c0d8b4457175696e6550696f6e656572486f7273653030313237", strlen("f8b0701b14c4e588a0f68e45d91d501c62580887dc1cc863d6c0d8b4457175696e6550696f6e656572486f7273653030313237"), &asset_id);

if (result != CARDANO_SUCCESS)
{
cardano_provider_unref(&provider);
cardano_address_unref(&address);

printf("Failed to create asset id: %s\n", cardano_provider_get_last_error(provider));
return 1;
}

result = cardano_provider_resolve_unspent_outputs(provider, inputs, &utxo_list);

cardano_address_unref(&address);
cardano_asset_id_unref(&asset_id);
cardano_transaction_input_set_unref(&inputs);

if (result != CARDANO_SUCCESS)
{
cardano_utxo_list_unref(&utxo_list);
cardano_provider_unref(&provider);
cardano_protocol_parameters_unref(&parameters);
cardano_utxo_unref(&utxo);

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

size_t utxo_count = cardano_utxo_list_get_length(utxo_list);

printf("UTXO count: %zu\n", utxo_count);

for (size_t i = 0; i < utxo_count; i++)
{
cardano_utxo_t* utxo = NULL;
result = cardano_utxo_list_get(utxo_list, i, &utxo);
CARDANO_UNUSED(result);

cardano_cbor_writer_t* writer = cardano_cbor_writer_new();

result = cardano_utxo_to_cbor(utxo, writer);
CARDANO_UNUSED(result);

size_t cbor_size = cardano_cbor_writer_get_hex_size(writer);
char* cbor_hex = malloc(cbor_size);

result = cardano_cbor_writer_encode_hex(writer, cbor_hex, cbor_size);
CARDANO_UNUSED(result);

printf("UTXO %zu: %s\n", i, cbor_hex);

free(cbor_hex);
cardano_cbor_writer_unref(&writer);
cardano_utxo_unref(&utxo);
}

cardano_utxo_list_unref(&utxo_list);
cardano_provider_unref(&provider);
cardano_protocol_parameters_unref(&parameters);
cardano_utxo_unref(&utxo);

return 0;
}
Expand Down
85 changes: 85 additions & 0 deletions examples/providers/blockfrost/blockfrost_parsers.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,33 @@ parse_blockfrost_unspent_outputs(
size_t size,
cardano_utxo_list_t** utxo_list);

/**
* \brief Parses unspent transaction outputs (UTXOs) from a Blockfrost API response for a given transaction hash.
*
* This function parses the JSON response from the Blockfrost API to extract the UTXOs associated with the specified transaction hash. The results
* are stored in a UTXO list.
*
* \param[in] provider A pointer to an initialized \ref cardano_provider_impl_t object that provides the necessary context for parsing the UTXOs.
* This parameter must not be NULL.
* \param[in] json A pointer to a JSON string representing the Blockfrost API response containing the UTXOs. This parameter must not be NULL.
* \param[in] size The size of the JSON string in bytes.
* \param[in] tx_hash A pointer to a null-terminated string representing the transaction hash for which to retrieve UTXOs. This parameter must not be NULL.
* \param[in] tx_hash_len The length of the \p tx_hash string.
* \param[out] utxo_list A pointer to a pointer of \ref cardano_utxo_list_t that will be populated with the UTXOs associated with the given transaction hash.
* The caller must manage the lifecycle of this list and free it when no longer needed.
*
* \return \ref cardano_error_t indicating the outcome of the operation. Returns \ref CARDANO_SUCCESS if the UTXOs were successfully parsed,
* or an appropriate error code if an error occurred (e.g., invalid JSON format, failure to parse the UTXOs, or invalid transaction hash).
*/
cardano_error_t
parse_blockfrost_tx_unspent_outputs(
cardano_provider_impl_t* provider,
const char* json,
size_t size,
const char* tx_hash,
size_t tx_hash_len,
cardano_utxo_list_t** utxo_list);

/**
* \brief Retrieves a script from the Blockfrost API using a script hash.
*
Expand Down Expand Up @@ -163,6 +190,64 @@ blockfrost_construct_rewards_url(
cardano_provider_impl_t* provider_impl,
const char* bech32);

/**
* \brief Constructs the URL for retrieving UTXOs associated with a given Bech32 address and asset ID from the Blockfrost API.
*
* This function constructs a URL used to query the Blockfrost API for the UTXOs that are associated with the specified Bech32 address and asset ID.
* The URL will also include pagination parameters, allowing the user to specify the page and the maximum number of results per page.
*
* \param[in] provider_impl A pointer to an initialized \ref cardano_provider_impl_t object that provides the necessary context (e.g., API base URL).
* This parameter must not be NULL.
* \param[in] bech32 A pointer to a null-terminated string containing the Bech32 address for which to retrieve UTXOs. This parameter must not be NULL.
* \param[in] asset_id A pointer to a null-terminated string containing the asset ID to filter the UTXOs by. This parameter must not be NULL.
* \param[in] page The page number to retrieve. Used for pagination.
* \param[in] max_results The maximum number of results to retrieve per page.
*
* \return A dynamically allocated string containing the full URL for querying UTXOs with the specified asset. The caller is responsible for freeing the
* returned string using \c free when it is no longer needed. Returns NULL if the URL could not be constructed (e.g., due to invalid input).
*/
char*
blockfrost_construct_utxo_with_asset_url(
cardano_provider_impl_t* provider_impl,
const char* bech32,
const char* asset_id,
size_t page,
size_t max_results);

/**
* \brief Constructs the URL for retrieving addresses associated with a given asset ID from the Blockfrost API.
*
* This function constructs a URL used to query the Blockfrost API for the addresses that hold the specified asset.
*
* \param[in] provider_impl A pointer to an initialized \ref cardano_provider_impl_t object that provides the necessary context (e.g., API base URL).
* This parameter must not be NULL.
* \param[in] asset_id A pointer to a null-terminated string containing the asset ID to query for addresses. This parameter must not be NULL.
*
* \return A dynamically allocated string containing the full URL for querying addresses holding the specified asset. The caller is responsible for freeing the
* returned string using \c free when it is no longer needed. Returns NULL if the URL could not be constructed (e.g., due to invalid input).
*/
char*
blockfrost_construct_addresses_with_asset_url(
cardano_provider_impl_t* provider_impl,
const char* asset_id);

/**
* \brief Constructs a URL for retrieving UTXOs from a transaction.
*
* This function constructs the URL required to fetch the UTXOs associated with a given transaction ID.
* The URL is built based on the Blockfrost API and the network details provided in the `provider_impl`.
*
* \param[in] provider_impl A pointer to the provider implementation that contains network information.
* \param[in] tx_id A string representing the transaction ID.
*
* \return A dynamically allocated string containing the constructed URL, or NULL if memory allocation fails.
* The caller is responsible for freeing the memory returned by this function.
*/
char*
blockfrost_construct_transaction_utxos_url(
cardano_provider_impl_t* provider_impl,
const char* tx_id);

#ifdef __cplusplus
}
#endif /* __cplusplus */
Expand Down
Loading

0 comments on commit 598d626

Please sign in to comment.