Transaction Output¶
-
typedef struct cardano_transaction_output_t cardano_transaction_output_t¶
A transaction output object includes the address which represents a public key hash or a script hash that can unlock the output, and the funds that are held inside.
-
cardano_error_t cardano_transaction_output_new(cardano_address_t *address, uint64_t amount, cardano_transaction_output_t **transaction_output)¶
Creates and initializes a new instance of a transaction output.
This function allocates and initializes a new instance of a cardano_transaction_output_t object, which represents an output of a transaction in the Cardano network.
Usage Example:
cardano_address_t* address = ...; // Assume address is already initialized uint64_t amount = 1000000; // 1 ADA in lovelaces cardano_transaction_output_t* transaction_output = NULL; cardano_error_t result = cardano_transaction_output_new(address, amount, &transaction_output); if (result == CARDANO_SUCCESS) { // Use the transaction output // Free resources when done cardano_transaction_output_unref(&transaction_output); } else { printf("Failed to create the transaction output: %s\n", cardano_error_to_string(result)); } cardano_address_unref(&address); // Cleanup the address object- Parameters:¶
- cardano_address_t *address¶
[in] A pointer to a cardano_address_t object representing the recipient’s address.
- uint64_t amount¶
[in] The amount of ADA in lovelaces that the output will hold.
- cardano_transaction_output_t **transaction_output¶
[out] On successful initialization, this will point to a newly created cardano_transaction_output_t object. This object represents a “strong reference”, meaning that it is fully initialized and ready for use. The caller is responsible for managing the lifecycle of this object. Specifically, once the transaction output is no longer needed, the caller must release it by calling cardano_transaction_output_unref.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the transaction output was successfully created, or an appropriate error code indicating the failure reason, such as CARDANO_ERROR_POINTER_IS_NULL if the input address pointer is NULL.
-
cardano_error_t cardano_transaction_output_from_cbor(cardano_cbor_reader_t *reader, cardano_transaction_output_t **transaction_output)¶
Creates a cardano_transaction_output_t from a CBOR reader.
This function parses CBOR data using a provided cardano_cbor_reader_t and constructs a cardano_transaction_output_t object. It assumes that the CBOR reader is set up correctly and that the CBOR data corresponds to the structure expected for a transaction_output.
Usage Example:
cardano_cbor_reader_t* reader = cardano_cbor_reader_new(cbor_data, data_size); cardano_transaction_output_t* transaction_output = NULL; cardano_error_t result = cardano_transaction_output_from_cbor(reader, &transaction_output); if (result == CARDANO_SUCCESS) { // Use the transaction_output // Once done, ensure to clean up and release the transaction_output cardano_transaction_output_unref(&transaction_output); } else { const char* error = cardano_cbor_reader_get_last_error(reader); printf("Failed to decode transaction_output: %s\n", error); } cardano_cbor_reader_unref(&reader); // Cleanup the CBOR readerNote
If the function fails, the last error can be retrieved by calling cardano_cbor_reader_get_last_error with the reader. The caller is responsible for freeing the created cardano_transaction_output_t object by calling cardano_transaction_output_unref when it is no longer needed.
- Parameters:¶
- cardano_cbor_reader_t *reader¶
[in] A pointer to an initialized cardano_cbor_reader_t that is ready to read the CBOR-encoded data.
- cardano_transaction_output_t **transaction_output¶
[out] A pointer to a pointer of cardano_transaction_output_t that will be set to the address of the newly created transaction_output object upon successful decoding.
- Returns:¶
A cardano_error_t value indicating the outcome of the operation. Returns CARDANO_SUCCESS if the transaction output were successfully created, or an appropriate error code if an error occurred.
-
cardano_error_t cardano_transaction_output_to_cbor(const cardano_transaction_output_t *transaction_output, cardano_cbor_writer_t *writer)¶
Serializes transaction output into CBOR format using a CBOR writer.
This function serializes the given cardano_transaction_output_t object using a cardano_cbor_writer_t.
Usage Example:
cardano_transaction_output_t* transaction_output = ...; cardano_cbor_writer_t* writer = cardano_cbor_writer_new(); if (writer) { cardano_error_t result = cardano_transaction_output_to_cbor(transaction_output, writer); if (result == CARDANO_SUCCESS) { // Use the writer's buffer containing the serialized data } else { const char* error_message = cardano_cbor_writer_get_last_error(writer); printf("Serialization failed: %s\n", error_message); } cardano_cbor_writer_unref(&writer); } cardano_transaction_output_unref(&transaction_output);- Parameters:¶
- const cardano_transaction_output_t *transaction_output¶
[in] A constant pointer to the cardano_transaction_output_t object that is to be serialized.
- cardano_cbor_writer_t *writer¶
[out] A pointer to a cardano_cbor_writer_t where the CBOR serialized data will be written. The writer must already be initialized and ready to accept the data.
- Returns:¶
Returns CARDANO_SUCCESS if the serialization is successful. If the
transaction_outputorwriteris NULL, returns CARDANO_ERROR_POINTER_IS_NULL.
-
cardano_error_t cardano_transaction_output_to_cip116_json(const cardano_transaction_output_t *output, cardano_json_writer_t *writer)¶
Serializes a transaction output to CIP-116 JSON.
The function writes the full JSON object, including the surrounding braces. Keys are written in the order: “address”, “amount”, “plutus_data” (if present), “script_ref” (if present).
- Parameters:¶
- const cardano_transaction_output_t *output¶
[in] Pointer to a valid cardano_transaction_output_t.
- cardano_json_writer_t *writer¶
[in] Pointer to a valid cardano_json_writer_t.
- Returns:¶
CARDANO_SUCCESS On success. CARDANO_ERROR_POINTER_IS_NULL If
outputorwriteris NULL. CARDANO_ERROR_MEMORY_ALLOCATION_FAILED If memory allocation fails. Other Any error propagated from nested writers.
-
cardano_address_t *cardano_transaction_output_get_address(cardano_transaction_output_t *output)¶
Retrieves the address associated with a transaction output.
This function fetches the address from a given cardano_transaction_output_t object. The address indicates the recipient of the funds specified in the output of a transaction.
Usage Example:
cardano_transaction_output_t* output = ...; // Assume output is already initialized cardano_address_t* address = cardano_transaction_output_get_address(output); if (address != NULL) { // Process the address cardano_address_unref(&address); // Release the address when done } else { printf("Invalid transaction output or uninitialized output.\n"); }- Parameters:¶
- cardano_transaction_output_t *output¶
[in] A pointer to an initialized cardano_transaction_output_t object.
- Returns:¶
A pointer to the cardano_address_t object representing the address. If the output pointer is NULL, this function returns NULL. Note that the returned address is a new reference and must be released using cardano_address_unref when it is no longer needed.
-
cardano_error_t cardano_transaction_output_set_address(cardano_transaction_output_t *output, cardano_address_t *address)¶
Sets the address for a transaction output.
This function assigns a new address to a cardano_transaction_output_t object. The address is where the output, holding certain funds or assets, is intended to be sent.
Usage Example:
cardano_transaction_output_t* output = ...; // Assume output is already initialized cardano_address_t* address = ...; // Assume address is initialized cardano_error_t result = cardano_transaction_output_set_address(output, address); if (result == CARDANO_SUCCESS) { // The address is now set for the output } else { printf("Failed to set the address.\n"); } // Clean up resources cardano_address_unref(&address);Note
This function increases the reference count of the address object; therefore, the caller retains ownership of their respective references. It is the caller’s responsibility to release their reference to the address when it is no longer needed.
- Parameters:¶
- cardano_transaction_output_t *output¶
[inout] A pointer to an initialized cardano_transaction_output_t object to which the address will be set.
- cardano_address_t *address¶
[in] A pointer to an initialized cardano_address_t object representing the address.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the address was successfully set, or an appropriate error code indicating the failure reason, such as CARDANO_ERROR_POINTER_IS_NULL if any of the input pointers are NULL.
-
cardano_value_t *cardano_transaction_output_get_value(cardano_transaction_output_t *output)¶
Retrieves the value held by a transaction output.
This function fetches the value contained in the transaction output.
Usage Example:
const cardano_transaction_output_t* output = ...; // Assume output is already initialized cardano_value_t* value = cardano_transaction_output_get_value(output); if (value != NULL) { // Process the value cardano_value_unref(&amount); // Release the value when done } else { printf("Invalid transaction output or uninitialized output.\n"); }- Parameters:¶
- cardano_transaction_output_t *output¶
[in] A constant pointer to an initialized cardano_transaction_output_t object.
- Returns:¶
The value contained in the transaction output. If the output pointer is NULL, the function will return NULL.
-
cardano_error_t cardano_transaction_output_set_value(cardano_transaction_output_t *output, cardano_value_t *value)¶
Sets the value for a transaction output.
This function assigns a specified value to a cardano_transaction_output_t object.
Usage Example:
cardano_transaction_output_t* output = ...; // Assume output is already initialized cardano_value_t* value = {...}; cardano_error_t result = cardano_transaction_output_set_amount(output, value); if (result == CARDANO_SUCCESS) { printf("Amount set successfully.\n"); } else { printf("Failed to set the amount.\n"); } // Release the value when done cardano_value_unref(&value);- Parameters:¶
- cardano_transaction_output_t *output¶
[inout] A pointer to an initialized cardano_transaction_output_t object to which the amount will be set.
- cardano_value_t *value¶
[in] The value to set for the transaction output.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the amount was successfully set, or an appropriate error code indicating the failure reason, such as CARDANO_ERROR_POINTER_IS_NULL if the output pointer is NULL.
-
cardano_datum_t *cardano_transaction_output_get_datum(cardano_transaction_output_t *output)¶
Retrieves the datum associated with a transaction output.
This function fetches the datum from a given cardano_transaction_output_t object. A datum is optional state data associated with a transaction output that can be utilized by Plutus scripts to dictate transaction validity based on script logic. This function is used primarily in the context of transactions involving smart contracts on the Cardano network.
Usage Example:
cardano_transaction_output_t* output = ...; // Assume output is already initialized cardano_datum_t* datum = cardano_transaction_output_get_datum(output); if (datum != NULL) { // Process the datum cardano_datum_unref(&datum); } else { printf("No datum associated with this output.\n"); }- Parameters:¶
- cardano_transaction_output_t *output¶
[in] A pointer to an initialized cardano_transaction_output_t object from which the datum is retrieved.
- Returns:¶
A pointer to the cardano_datum_t object representing the datum, or NULL if no datum is associated with the transaction output. Note that the returned datum is a new reference and must be released using cardano_datum_unref when it is no longer needed.
-
cardano_error_t cardano_transaction_output_set_datum(cardano_transaction_output_t *output, cardano_datum_t *datum)¶
Sets the datum for a transaction output.
This function assigns a datum to a cardano_transaction_output_t object. A datum is a piece of state data that can be used by Plutus scripts to influence the behavior of smart contracts on the Cardano network. The datum is optional and can be set to NULL to indicate that no datum is associated with the transaction output.
Usage Example:
cardano_transaction_output_t* output = ...; // Assume output is already initialized cardano_datum_t* datum = cardano_datum_new(...); // Assume datum is initialized, or NULL to unset cardano_error_t result = cardano_transaction_output_set_datum(output, datum); if (result == CARDANO_SUCCESS) { printf("Datum set successfully.\n"); } else { printf("Failed to set the datum.\n"); } // If datum is not NULL, release it after setting if (datum) { cardano_datum_unref(&datum); }Note
This function increases the reference count of the datum object; therefore, the caller retains ownership of their respective references. It is the caller’s responsibility to release their reference to the datum when it is no longer needed.
- Parameters:¶
- cardano_transaction_output_t *output¶
[inout] A pointer to an initialized cardano_transaction_output_t object to which the datum will be set.
- cardano_datum_t *datum¶
[in] A pointer to an initialized cardano_datum_t object representing the datum to be associated with the output. This parameter can be NULL if the datum is to be removed.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the datum was successfully set, or an appropriate error code indicating the failure reason, such as CARDANO_ERROR_POINTER_IS_NULL if the output pointer is NULL.
-
cardano_script_t *cardano_transaction_output_get_script_ref(cardano_transaction_output_t *output)¶
Retrieves the script reference from a transaction output.
This function fetches the script reference from a specified cardano_transaction_output_t object. A script reference is a mechanism in Cardano that allows transactions to refer to scripts included in other outputs. By using script references, a transaction can fulfill script execution requirements without directly including the script, thereby reducing transaction size and simplifying script management.
Usage Example:
cardano_transaction_output_t* output = ...; // Assume output is already initialized cardano_script_t* script_ref = cardano_transaction_output_get_script_ref(output); if (script_ref != NULL) { // Process the script reference cardano_script_unref(&script_ref); } else { printf("No script reference found in this output.\n"); }- Parameters:¶
- cardano_transaction_output_t *output¶
[in] A pointer to an initialized cardano_transaction_output_t object from which the script reference is to be retrieved.
- Returns:¶
A pointer to the cardano_script_t object representing the script reference. If the output does not contain a script reference, this function returns NULL. Note that the returned script reference is a new reference and must be released using cardano_script_unref when it is no longer needed.
-
cardano_error_t cardano_transaction_output_set_script_ref(cardano_transaction_output_t *output, cardano_script_t *script_ref)¶
Sets the script reference for a transaction output.
This function assigns a script reference to a specified cardano_transaction_output_t object. A script reference allows a transaction output to indirectly satisfy script execution requirements by referencing a script present in another output. This capability can lead to more efficient transaction sizes and simplified script management by reusing existing scripts.
Usage Example:
cardano_transaction_output_t* output = ...; // Assume output is already initialized cardano_script_t* script_ref = ...; // Assume script_ref is initialized or NULL to remove the reference cardano_error_t result = cardano_transaction_output_set_script_ref(output, script_ref); if (result == CARDANO_SUCCESS) { printf("Script reference set successfully.\n"); } else { printf("Failed to set the script reference.\n"); } // Note: Cleanup the script reference if it was newly created and is no longer needed if (script_ref) { cardano_script_unref(&script_ref); }- Parameters:¶
- cardano_transaction_output_t *output¶
[inout] A pointer to an initialized cardano_transaction_output_t object to which the script reference will be set.
- cardano_script_t *script_ref¶
[in] A pointer to an initialized cardano_script_t object representing the script reference. This parameter can be NULL if the intention is to remove an existing script reference from the output.
- Returns:¶
cardano_error_t indicating the outcome of the operation. Returns CARDANO_SUCCESS if the script reference was successfully set, or an appropriate error code indicating the failure reason, such as CARDANO_ERROR_POINTER_IS_NULL if the output pointer is NULL.
-
bool cardano_transaction_output_equals(const cardano_transaction_output_t *lhs, const cardano_transaction_output_t *rhs)¶
Compares two transaction output objects for equality.
This function compares two transaction output objects to determine if they are equal.
Usage Example:
cardano_transaction_output_t* transaction_output1 = NULL; cardano_transaction_output_t* transaction_output2 = NULL; // Assume transaction_output1 and transaction_output2 are initialized properly bool are_equal = cardano_transaction_output_equals(transaction_output1, transaction_output2); if (are_equal) { printf("The transaction_output objects are equal.\n"); } else { printf("The transaction_output objects are not equal.\n"); } // Clean up cardano_transaction_output_unref(&transaction_output1); cardano_transaction_output_unref(&transaction_output2);- Parameters:¶
- const cardano_transaction_output_t *lhs¶
[in] Pointer to the first transaction output object.
- const cardano_transaction_output_t *rhs¶
[in] Pointer to the second transaction output object.
- Returns:¶
trueif the transaction_output objects are equal,falseotherwise.
-
void cardano_transaction_output_unref(cardano_transaction_output_t **transaction_output)¶
Decrements the reference count of a cardano_transaction_output_t object.
This function is responsible for managing the lifecycle of a cardano_transaction_output_t object by decreasing its reference count. When the reference count reaches zero, the transaction_output is finalized; its associated resources are released, and its memory is deallocated.
Usage Example:
cardano_transaction_output_t* transaction_output = cardano_transaction_output_new(major, minor); // Perform operations with the transaction_output... cardano_transaction_output_unref(&transaction_output); // At this point, transaction_output is NULL and cannot be used.Note
After calling cardano_transaction_output_unref, the pointer to the cardano_transaction_output_t object will be set to NULL to prevent its reuse.
- Parameters:¶
- cardano_transaction_output_t **transaction_output¶
[inout] A pointer to the pointer of the transaction_output object. This double indirection allows the function to set the caller’s pointer to NULL, avoiding dangling pointer issues after the object has been freed.
-
void cardano_transaction_output_ref(cardano_transaction_output_t *transaction_output)¶
Increases the reference count of the cardano_transaction_output_t object.
This function is used to manually increment the reference count of an cardano_transaction_output_t object, indicating that another part of the code has taken ownership of it. This ensures the object remains allocated and valid until all owners have released their reference by calling cardano_transaction_output_unref.
Usage Example:
// Assuming transaction_output is a previously created transaction_output object cardano_transaction_output_ref(transaction_output); // Now transaction_output can be safely used elsewhere without worrying about premature deallocationNote
Always ensure that for every call to cardano_transaction_output_ref there is a corresponding call to cardano_transaction_output_unref to prevent memory leaks.
- Parameters:¶
- cardano_transaction_output_t *transaction_output¶
A pointer to the cardano_transaction_output_t object whose reference count is to be incremented.
-
size_t cardano_transaction_output_refcount(const cardano_transaction_output_t *transaction_output)¶
Retrieves the current reference count of the cardano_transaction_output_t object.
This function returns the number of active references to an cardano_transaction_output_t object. It’s useful for debugging purposes or managing the lifecycle of the object in complex scenarios.
Usage Example:
// Assuming transaction_output is a previously created transaction_output object size_t ref_count = cardano_transaction_output_refcount(transaction_output); printf("Reference count: %zu\n", ref_count);Warning
This function does not account for transitive references. A transitive reference occurs when an object holds a reference to another object, rather than directly to the cardano_transaction_output_t. As such, the reported count may not fully represent the total number of conceptual references in cases where such transitive relationships exist.
- Parameters:¶
- const cardano_transaction_output_t *transaction_output¶
A pointer to the cardano_transaction_output_t object whose reference count is queried. The object must not be NULL.
- Returns:¶
The number of active references to the specified cardano_transaction_output_t object. If the object is properly managed (i.e., every cardano_transaction_output_ref call is matched with a cardano_transaction_output_unref call), this count should reach zero right before the object is deallocated.
-
void cardano_transaction_output_set_last_error(cardano_transaction_output_t *transaction_output, const char *message)¶
Sets the last error message for a given cardano_transaction_output_t object.
Records an error message in the transaction_output’s last_error buffer, overwriting any existing message. This is useful for storing descriptive error information that can be later retrieved. The message is truncated if it exceeds the buffer’s capacity.
Note
The error message is limited to 1023 characters, including the null terminator, due to the fixed size of the last_error buffer.
- Parameters:¶
- cardano_transaction_output_t *transaction_output¶
[in] A pointer to the cardano_transaction_output_t instance whose last error message is to be set. If
NULL, the function does nothing.- const char *message¶
[in] A null-terminated string containing the error message. If
NULL, the transaction_output’s last_error is set to an empty string, indicating no error.
-
const char *cardano_transaction_output_get_last_error(const cardano_transaction_output_t *transaction_output)¶
Retrieves the last error message recorded for a specific transaction_output.
This function returns a pointer to the null-terminated string containing the last error message set by cardano_transaction_output_set_last_error for the given transaction_output. If no error message has been set, or if the last_error buffer was explicitly cleared, an empty string is returned, indicating no error.
Note
The returned string points to internal storage within the object and must not be modified by the caller. The string remains valid until the next call to cardano_transaction_output_set_last_error for the same transaction_output, or until the transaction_output is deallocated.
- Parameters:¶
- const cardano_transaction_output_t *transaction_output¶
[in] A pointer to the cardano_transaction_output_t instance whose last error message is to be retrieved. If the transaction_output is NULL, the function returns a generic error message indicating the null transaction_output.
- Returns:¶
A pointer to a null-terminated string containing the last error message for the specified transaction_output. If the transaction_output is NULL, “Object is NULL.” is returned to indicate the error.