### Install Aiken Design Patterns Package Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/README.md Install the design patterns package using the aiken CLI. Specify the desired version for your project. ```bash aiken add anastasia-labs/aiken-design-patterns --version v1.7.0 ``` -------------------------------- ### Build and Test Aiken Design Patterns Package Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/README.md Compile all functions and examples, and run the included unit tests for the Aiken design patterns package. ```bash aiken build ``` ```bash aiken check ``` -------------------------------- ### Example Test with Fuzzer Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utils.md Demonstrates how to use a fuzzer (single_asset_value_fuzzer) within an Aiken test case to generate a Value and perform assertions. ```aiken use aiken_design_patterns/utils test success_example_with_fuzzer( value: Value via utils.single_asset_value_fuzzer(), ) { let with_lovelace = assets.merge(value, assets.from_lovelace(2_000_000)) // Test logic here True } ``` -------------------------------- ### Aiken Computation Withdrawal Wrapper Example Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-merkelized-validator.md Example of a staking validator using computation_withdrawal_wrapper. It executes a computation function with the redeemer's input and verifies the result. ```aiken validator withdraw { withdraw( redeemer: ComputationRedeemer, Int>, _own_credential: Credential, _tx: Transaction, ) { merkelized_validator.computation_withdrawal_wrapper( redeemer: redeemer, function: fn(numbers) { list.foldl(numbers, 0, fn(n, acc) { acc + n }) } ) } else(_) { fail } } ``` -------------------------------- ### delegated_compute Function Example Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-merkelized-validator.md Demonstrates how to use the `delegated_compute` function within a validator script. It offloads a computation to a specified staking validator and verifies the result. ```aiken pub type MyComputationInput = List pub type MyComputationResult = Int validator spend { spend(_datum, _redeemer, _own_ref, tx: Transaction) { let computation_result = merkelized_validator.delegated_compute( function_input: [1, 2, 3, 4, 5], staking_validator: my_staking_validator_hash, redeemers: tx.redeemers, redeemer_index: 0, input_data_coercer: fn(input_data) { expect input_list: List = input_data input_list }, output_data_coercer: fn(output_data) { expect result: Int = output_data result } ) // Now use the computation_result computation_result == 15 } else(_) { fail } } ``` -------------------------------- ### Aiken Spending Validator Example with validate_mint_minimal (Policy ID Parameter) Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-tx-level-minter.md Example of a spending validator that takes the minting policy ID as a parameter and uses `validate_mint_minimal` to ensure minting activity. This confirms the minting endpoint is executed. ```aiken validator spend(mint_policy: PolicyId) { spend(_datum, _redeemer, _own_ref, tx: Transaction) { // Ensure minting endpoint is executed by verifying mint activity tx_level_minter.validate_mint_minimal( mint_script_hash: mint_policy, mint: tx.mint ) } else(_) { fail } } ``` -------------------------------- ### Example Usage of Parameter Validation Wrapper Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md Demonstrates how to use the `wrapper` function within a validator script to check a parameter's hash and execute custom validation logic. Includes definitions for custom redeemer and parameter types. ```aiken use aiken_design_patterns/parameter_validation pub type MyRedeemer { action: Int, } pub type MyParam { owner: ByteArray, threshold: Int, } validator spend { spend( _datum, outer_redeemer: ParameterizedRedeemer, _own_ref, tx: Transaction, ) { let owner_hash = blake2b_224(#"owned_script_bytes") parameter_validation.wrapper( hashed_parameter: owner_hash, parameter_serialiser: fn(param) { // Serialize the parameter param.owner |> bytearray.concat(integer.to_bytearray(param.threshold)) }, outer_redeemer: outer_redeemer, validator_function: fn(param, redeemer) { and { param.threshold > 0, redeemer.action >= 0, } } ) } else(_) { fail } } ``` -------------------------------- ### init Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-linked-list.md Initializes a new linked list by creating a root element. It validates the minting of the root NFT, the contents of the produced output, the inline datum structure, and allows for custom validation of Lovelace and root data. The root element must not have any links, signifying the start of a new list. ```APIDOC ## init ### Description Initializes a new linked list with a root element. Validates minting of the root NFT, output contents, datum structure, and custom root data validation. The root must have no link. ### Method (Aiken Function) ### Parameters - **nonce_validated** (Bool) - Required - Must be true; indicates nonce validation happened outside this function. - **produced_element_output** (Output) - Required - The UTxO output containing the root element. - **tx_mint** (Value) - Required - Transaction mint field (must contain exactly one root NFT mint). - **root_validator** (fn(Lovelace, Data) -> Bool) - Required - Custom validation for root's Lovelace and data. ### Return Type RootEval — A reader function that must be finalized with `run_root_with`. ### Example ```aiken validator mint { mint(_redeemer, own_policy: PolicyId, tx: Transaction) { let root_output = tx.outputs |> list.at(0) linked_list.init( nonce_validated: True, // Assume nonce was validated produced_element_output: root_output, tx_mint: tx.mint, root_validator: fn(lovelace, data) { // Require minimum 2 ADA in root lovelace >= 2_000_000 } ) |> linked_list.run_root_with(own_policy, my_root_key) } else(_) { fail } } ``` ``` -------------------------------- ### Aiken Spending Validator Example with validate_mint_minimal Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-tx-level-minter.md Example of a spending validator using `validate_mint_minimal` to ensure the minting endpoint is executed by verifying mint activity. This is a minimal check without redeemer validation. ```aiken validator spend { spend(_datum, _redeemer, _own_ref, tx: Transaction) { tx_level_minter.validate_mint_minimal( mint_script_hash: my_policy_id, mint: tx.mint ) } else(_) { fail } } ``` -------------------------------- ### Aiken Validation Withdrawal Wrapper Example Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-merkelized-validator.md Example of a staking validator using validation_withdrawal_wrapper. It executes a validation function with the redeemer's input and returns the result. ```aiken validator withdraw { withdraw( redeemer: ValidationRedeemer, _own_credential: Credential, _tx: Transaction, ) { merkelized_validator.validation_withdrawal_wrapper( redeemer: redeemer, validation: fn(input) { // Complex validation logic input.field_a > 0 && input.field_b < 1000 } ) } else(_) { fail } } ``` -------------------------------- ### Aiken Staking Validator with Computation Wrapper Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-merkelized-validator.md Example of a staking/withdrawal validator using computation_withdrawal_wrapper. It delegates an expensive computation. ```aiken validator withdraw { withdraw( redeemer: ComputationRedeemer, _own_credential: Credential, _tx: Transaction, ) { merkelized_validator.computation_withdrawal_wrapper( redeemer: redeemer, function: fn(input) { // Expensive computation delegated here expect nums: List = input list.foldl(nums, 0, fn(n, acc) { acc + n }) } ) } else(_) { fail } } ``` -------------------------------- ### Singular UTxO Indexer: one_to_one Example Usage Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utxo-indexers.md Demonstrates how to use the `one_to_one` function within an Aiken validator to spend a UTxO. It includes custom logic to compare the value of the input and output. Note that this function does not inherently protect against double satisfaction. ```aiken use aiken_design_patterns/singular_utxo_indexer use cardano/transaction.{Input, Output} validator spend { spend( _datum, _redeemer, own_ref: OutputReference, tx: Transaction, ) { singular_utxo_indexer.one_to_one( input_index: 0, output_index: 0, own_ref: own_ref, inputs: tx.inputs, outputs: tx.outputs, double_satisfaction_prevented: True, validation_logic: fn(in_utxo, out_utxo) { // Custom logic comparing input and output in_utxo.output.value == out_utxo.value } ) } else(_) { fail } } ``` -------------------------------- ### Aiken Delegated Validation Example Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-merkelized-validator.md Example of using delegated_validation within a spending validator. It extracts a ValidationRedeemer, coerces input, and verifies it. ```aiken validator spend { spend(_datum, _redeemer, _own_ref, tx: Transaction) { merkelized_validator.delegated_validation( function_input: my_complex_input, staking_validator: validator_hash, redeemers: tx.redeemers, redeemer_index: 0, input_data_coercer: fn(data) { expect coerced: ComplexType = data coerced } ) } else(_) { fail } } ``` -------------------------------- ### ComputationRedeemer Example Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-merkelized-validator.md Illustrates the usage of the ComputationRedeemer type for a summation computation. The input is a list of integers, and the result is their sum. ```aiken pub type ComputationInput = List pub type ComputationResult = Int let valid_computation = ComputationRedeemer { input_arg: [1, 2, 3, 4, 5], result: 15, } ``` -------------------------------- ### Parameterized Spending Script Example Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md This Aiken code illustrates the use of parameter validation in a spending script. It employs the `wrapper` function to validate the redeemer against a parameterized script, ensuring that the correct parameters are used for spending. ```aiken validator spend { spend( _datum, redeemer: ParameterizedRedeemer, _own_ref, tx: Transaction, ) { parameter_validation.wrapper( hashed_parameter: blake2b_224(MY_TEMPLATE_PREFIX), parameter_serialiser: fn(owner) { owner }, outer_redeemer: redeemer, validator_function: fn(owner, spend_redeemer) { spend_redeemer.owner == owner } ) } else(_) { fail } } ``` -------------------------------- ### Aiken Minting Policy Example with validate_mint Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-tx-level-minter.md Example of a minting policy using `validate_mint` to perform custom validation on the redeemer and minted tokens. It expects a single minted token and validates the redeemer data. ```aiken use aiken_design_patterns/tx_level_minter validator mint { mint( redeemer: MintRedeemer, _own_policy: PolicyId, tx: Transaction, ) { tx_level_minter.validate_mint( mint_script_hash: _own_policy, mint: tx.mint, redeemers: tx.redeemers, mint_redeemer_index: 0, mint_validator: fn(rdmr_data, tokens) { // Validate the redeemer expect my_rdmr: MintRedeemer = rdmr_data // Validate the minted tokens dict.size(tokens) == 1 } ) } else(_) { fail } } ``` -------------------------------- ### Aiken spend_for_adding_or_removing_an_element Usage Example Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-linked-list.md Demonstrates how to use the `spend_for_adding_or_removing_an_element` function within an Aiken validator to ensure transaction minting activity matches the list's NFT policy ID. ```aiken validator spend { spend(_datum, _redeemer, _own_ref, tx: Transaction) { linked_list.spend_for_adding_or_removing_an_element( list_nft_policy_id: MY_LIST_POLICY_ID, tx_mint: tx.mint ) } else(_) { fail } } ``` -------------------------------- ### Parameterized Minting Script Example Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md This Aiken code demonstrates how to use parameter validation in a minting script. It ensures that minted tokens are sent to addresses derived from a parameterized script hash, using `apply_param` to define the expected parameters. ```aiken validator mint { mint(_redeemer, own_policy: PolicyId, tx: Transaction) { // Ensure tokens go to authorized instances of template script let authorized_script_hash = parameter_validation.apply_param( version: 3, prefix: MY_TEMPLATE_PREFIX, param: allowed_owner ) list.any(tx.outputs, fn(output) { output.address.payment_credential == Script(authorized_script_hash) }) } else(_) { fail } } ``` -------------------------------- ### Import Aiken Design Patterns Modules Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/README.md Import various design pattern modules into your Aiken smart contract. Ensure the package is installed before importing. ```aiken use aiken_design_patterns/merkelized_validator use aiken_design_patterns/multi_utxo_indexer use aiken_design_patterns/linked_list use aiken_design_patterns/parameter_validation use aiken_design_patterns/singular_utxo_indexer use aiken_design_patterns/stake_validator use aiken_design_patterns/tx_level_minter ``` -------------------------------- ### Aiken Example: Using validate_withdraw_with_amount Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-stake-validator.md Illustrates using `validate_withdraw_with_amount` to validate both the redeemer data and the withdrawal amount, with a specific check for a zero amount. ```aiken let redeemer_data, withdraw_amount, <- stake_validator.validate_withdraw_with_amount( withdraw_script_hash: withdraw_script_hash, redeemers: redeemers, withdraw_redeemer_index: redeemer.withdraw_redeemer_index, withdrawals: withdrawals, withdrawal_index: redeemer.withdrawal_index, withdraw_redeemer_validator: fn(rdmr, amount) { // Validation: ensure amount is zero amount == 0 } ) ``` -------------------------------- ### Property-Based Testing with Fuzzers Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/README.md Implement property-based tests using provided fuzzers for more robust contract validation. This example demonstrates testing a function that expects a single asset value. ```aiken test my_function_with_value( v: Value via utils.single_asset_value_fuzzer(), ) { // Test implementation True } ``` -------------------------------- ### Aiken Spending Validator with Delegated Compute Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-merkelized-validator.md Example of a spending validator calling delegated_compute. It extracts and verifies results from a staking script. ```aiken validator spend { spend(_datum, _redeemer, _own_ref, tx: Transaction) { let result = merkelized_validator.delegated_compute( function_input: my_data, staking_validator: STAKING_VALIDATOR_HASH, redeemers: tx.redeemers, redeemer_index: 0, input_data_coercer: fn(d) { expect d: Data = d; d }, output_data_coercer: fn(d) { expect d: Int = d; d } ) result > 100 } else(_) { fail } } ``` -------------------------------- ### prepend_unordered Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-linked-list.md Prepends a new node to the start of an unordered linked list, inserting it immediately after the root element. The anchor must be the root. ```APIDOC ## prepend_unordered ### Description Inserts a new node immediately after the root element. Anchor must be the root. Receives 7 parameters with explicit `RootData` (not `Option`) ### Function Signature ```aiken pub fn prepend_unordered( continued_root_element_output: Output, new_node_output: Output, inputs: List, tx_mint: Value, additional_validations: fn( Input, LovelaceChange, RootData, Lovelace, NodeKey, NodeData, Link, ) -> Bool, ) -> Eval ``` ### Source `lib/aiken-design-patterns/linked-list.ak:533-600` ``` -------------------------------- ### Aiken Example: Using validate_withdraw Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-stake-validator.md Demonstrates how to use the `validate_withdraw` function within a spend validator to verify a withdrawal and apply custom validation to its redeemer data. ```aiken validator spend(withdraw_script_hash: ScriptHash) { spend( _datum, redeemer: ExampleSpendRedeemer, own_out_ref: OutputReference, tx: Transaction, ) { let Transaction { withdrawals, redeemers, .. } = tx stake_validator.validate_withdraw( withdraw_script_hash: withdraw_script_hash, redeemers: redeemers, withdraw_redeemer_index: redeemer.withdraw_redeemer_index, withdraw_redeemer_validator: fn(rdmr) { // Custom validation logic applied to the redeemer expect out_ref: OutputReference = rdmr out_ref == own_out_ref } ) } else(_) { fail } } ``` -------------------------------- ### Prepend Node to Unordered List Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-linked-list.md Prepends a new node to the start of an unordered linked list, inserting it immediately after the root. The anchor must be the root element. The validation callback receives 7 parameters with explicit `RootData`. ```aiken pub fn prepend_unordered( continued_root_element_output: Output, new_node_output: Output, inputs: List, tx_mint: Value, additional_validations: fn( Input, LovelaceChange, RootData, Lovelace, NodeKey, NodeData, Link, ) -> Bool, ) -> Eval ``` -------------------------------- ### Example Usage of normalize_time_range in a Validator Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-validity-range-normalization.md Demonstrates how to use the normalize_time_range function within a validator to process transaction validity ranges. It shows pattern matching on the resulting NormalizedTimeRange to implement different time-based constraints. ```aiken use aiken_design_patterns/validity_range_normalization validator spend { spend(_datum, _redeemer, _own_ref, tx: Transaction) { let normalized = validity_range_normalization.normalize_time_range( tx.validity_range ) when normalized is { ClosedRange { lower, upper } -> { // Time is constrained between lower and upper // Can safely use both values in calculations let range_size = upper - lower range_size > 0 } FromNegInf { upper } -> { // Only upper bound is constrained upper > 0 } ToPosInf { lower } -> { // Only lower bound is constrained lower >= 0 } Always -> { // No time constraints True } InvalidRange -> { // Impossible; should not reach here in valid transactions fail } } } else(_) { fail } } ``` -------------------------------- ### run_root_with Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-linked-list.md Finalizes a `RootEval` operation. ```APIDOC ## run_root_with ### Description Finalizes a `RootEval` operation. ### Signature ```aiken pub fn run_root_with( reader: RootEval, list_nft_policy_id: PolicyId, root_key: RootKey, ) -> Bool ``` ``` -------------------------------- ### Clone Aiken Design Patterns Repository Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/README.md Clone the design patterns repository to your local machine. Navigate into the cloned directory to run tests. ```bash git clone https://github.com/Anastasia-Labs/aiken-design-patterns cd aiken-design-patterns ``` -------------------------------- ### Pattern Matching on Normalized Time Range Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-validity-range-normalization.md Illustrates how to use pattern matching to check for specific conditions of a normalized time range. This snippet shows how to determine if a range is bounded. ```aiken let normalized = normalize_time_range(tx.validity_range) let is_bounded = when normalized is { ClosedRange { .. } -> True FromNegInf { .. } -> False ToPosInf { .. } -> False Always -> False InvalidRange -> False } ``` -------------------------------- ### Run Aiken Unit Tests Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/README.md Execute all property-based tests within your Aiken modules. This command checks the correctness and robustness of your smart contracts. ```bash aiken check ``` -------------------------------- ### Specializing Linked List Element Types Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/types.md Demonstrates how to specialize generic linked list element types with custom data structures for root and node data. ```aiken use aiken_design_patterns/linked_list pub type MyRootData { owner: ByteArray, created_at: Int, } pub type MyNodeData { value: Int, timestamp: Int, } pub type MyElement = linked_list.Element pub type MyElementData = linked_list.ElementData ``` -------------------------------- ### Fuzzers for Lists of User Outputs and Inputs Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utils.md Generates lists of random outputs and inputs for user wallets. ```aiken pub fn user_outputs_fuzzer() -> Fuzzer> ``` ```aiken pub fn user_inputs_fuzzer() -> Fuzzer> ``` -------------------------------- ### apply_param Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md Applies a single parameter to a parameterized script, hashing the parameter with blake2b_224. Computes what the script hash would be if the parameter were applied to the template. The parameter is hashed with blake2b_224 before being combined with the prefix. ```APIDOC ## apply_param ### Description Applies a single parameter to a parameterized script, hashing the parameter with blake2b_224. Computes what the script hash would be if the parameter were applied to the template. The parameter is hashed with blake2b_224 before being combined with the prefix. ### Function Signature ```aiken pub fn apply_param( version: Int, prefix: ByteArray, param: ByteArray, ) -> ScriptHash ``` ### Parameters #### Path Parameters - **version** (Int) - Required - Script version (1, 2, or 3 depending on Plutus version) - **prefix** (ByteArray) - Required - CBOR prefix from a single encoded result of the parameterized script - **param** (ByteArray) - Required - Serialized parameter value (will be hashed with blake2b_224) ### Return Type `ScriptHash` — The script hash of the script with the parameter applied. ### Example ```aiken use aiken_design_patterns/parameter_validation let owner_address = #"1234567890abcdef..." let owner_hash = owner_address |> bytearray.hash // Pre-hash if needed let parameterized_script_hash = parameter_validation.apply_param( version: 3, prefix: my_script_prefix, param: owner_hash ) ``` ``` -------------------------------- ### Apply Two Parameters (One Pre-Hashed) to Script Template Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md Use `apply_prehashed_param_2` when the second parameter is already a 28-byte hash. The first parameter is hashed normally, while the second is used directly. ```aiken pub fn apply_prehashed_param_2( version: Int, prefix: ByteArray, param_0: ByteArray, param_1: ByteArray, ) -> ScriptHash ``` -------------------------------- ### ParameterizedRedeemer3 / wrapper_3 Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md For scripts with three parameters. ```APIDOC ## ParameterizedRedeemer3 / wrapper_3 ### Description For scripts with three parameters. ### Type Definition ```aiken pub type ParameterizedRedeemer3 { param_0: p, param_1: q, param_2: s, redeemer: r, } ``` ``` -------------------------------- ### Fuzzer for Script Output with Given Asset Name Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utils.md Generates script UTxOs with specific asset names, requiring both the asset name and datum. ```aiken pub fn authentic_script_output_with_given_asset_name_fuzzer( asset_name: AssetName, datum: Datum, ) -> Fuzzer<(ByteArray, Output)> ``` -------------------------------- ### ParameterizedRedeemer2 / wrapper_2 Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md For scripts with two parameters. ```APIDOC ## ParameterizedRedeemer2 / wrapper_2 ### Description For scripts with two parameters. ### Type Definition ```aiken pub type ParameterizedRedeemer2 { param_0: p, param_1: q, redeemer: r, } ``` ``` -------------------------------- ### Fuzzers for Authentic Script Outputs and Inputs Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utils.md Generates script UTxOs with authentication NFTs, requiring a datum. ```aiken pub fn authentic_script_output_fuzzer( datum: Datum, ) -> Fuzzer<(ByteArray, Output)> ``` ```aiken pub fn authentic_script_input_fuzzer(datum: Datum) -> Fuzzer<(ByteArray, Input)> ``` -------------------------------- ### Initialize Linked List with Root Element Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-linked-list.md Initializes a new linked list by creating a root element. This function validates the root NFT minting, the UTxO content, the inline datum structure, and custom validation rules for Lovelace and data. It ensures the root has no initial link. ```aiken pub fn init( nonce_validated: Bool, produced_element_output: Output, tx_mint: Value, root_validator: fn(Lovelace, Data) -> Bool, ) -> RootEval ``` ```aiken validator mint { mint(_redeemer, own_policy: PolicyId, tx: Transaction) { let root_output = tx.outputs |> list.at(0) linked_list.init( nonce_validated: True, // Assume nonce was validated produced_element_output: root_output, tx_mint: tx.mint, root_validator: fn(lovelace, data) { // Require minimum 2 ADA in root lovelace >= 2_000_000 } ) |> linked_list.run_root_with(own_policy, my_root_key) } else(_) { fail } } ``` -------------------------------- ### apply_param_2 Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md Applies two parameters to a parameterized script (first is hashed, second is hashed). ```APIDOC ## apply_param_2 ### Description Applies two parameters to a parameterized script (first is hashed, second is hashed). ### Function Signature ```aiken pub fn apply_param_2( version: Int, prefix: ByteArray, param_0: ByteArray, param_1: ByteArray, ) -> ScriptHash ``` ### Parameters #### Path Parameters - **version** (Int) - Required - Script version - **prefix** (ByteArray) - Required - CBOR prefix from template script - **param_0** (ByteArray) - Required - First parameter (will be hashed) - **param_1** (ByteArray) - Required - Second parameter (will be hashed) ### Return Type `ScriptHash` — The script hash with both parameters applied. ``` -------------------------------- ### Parameter2 / wrapper_no_redeemer_2 Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md For two-parameter scripts without redeemer. ```APIDOC ## Parameter2 / wrapper_no_redeemer_2 ### Description For two-parameter scripts without redeemer. ### Type Definition ```aiken pub type Parameter2 { param_0: p, param_1: q, } ``` ``` -------------------------------- ### apply_prehashed_param_2 Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md Applies two parameters where the first is hashed normally and second is prehashed. `param_1` is expected to be exactly 28 bytes (output of blake2b_224). This is useful when the second parameter is a script hash or similar 28-byte hash. ```APIDOC ## apply_prehashed_param_2 ### Description Applies two parameters where the first is hashed normally and second is prehashed. `param_1` is expected to be exactly 28 bytes (output of blake2b_224). This is useful when the second parameter is a script hash or similar 28-byte hash. ### Function Signature ```aiken pub fn apply_prehashed_param_2( version: Int, prefix: ByteArray, param_0: ByteArray, param_1: ByteArray, ) -> ScriptHash ``` ### Parameters #### Path Parameters - **version** (Int) - Required - Script version - **prefix** (ByteArray) - Required - CBOR prefix from template script - **param_0** (ByteArray) - Required - First parameter (will be hashed) - **param_1** (ByteArray) - Required - Second parameter (must be 28 bytes, prehashed) ### Return Type `ScriptHash` — The script hash with both parameters applied. ``` -------------------------------- ### Utility Functions Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/README.md Helper functions and testing utilities for the Aiken Design Patterns library. Includes utilities for redeemer access, input management, asset extraction, and testing fuzzers. ```APIDOC ## Utility Functions ### Description Helper functions and testing utilities for the Aiken Design Patterns library. Includes utilities for redeemer access, input management, asset extraction, and testing fuzzers. ### Utilities - Redeemer access, input management, asset extraction - Testing fuzzers for property-based testing ``` -------------------------------- ### Fuzzers for User Outputs and Inputs Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utils.md Generates random outputs and inputs for typical user wallets. ```aiken pub fn user_output_fuzzer() -> Fuzzer ``` ```aiken pub fn user_input_fuzzer() -> Fuzzer ``` -------------------------------- ### apply_param_3 / apply_prehashed_param_3 Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md Similar to the 2-parameter variants but for three parameters. See source for complete signatures. ```APIDOC ## apply_param_3 / apply_prehashed_param_3 ### Description Similar to the 2-parameter variants but for three parameters. See source for complete signatures. ``` -------------------------------- ### UTxO Indexer Functions Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/README.md Functions for efficiently mapping inputs to outputs. Supports singular mappings like one-to-one and one-to-many, as well as multi-input mappings with or without redeemers. ```APIDOC ## UTxO Indexers ### Description Functions for efficiently mapping inputs to outputs. Supports singular mappings like one-to-one and one-to-many, as well as multi-input mappings with or without redeemers. ### Functions - Singular: `one_to_one`, `one_to_many` - Multi: `one_to_one_no_redeemer`, `one_to_one_with_redeemer` ``` -------------------------------- ### Parameter3 / wrapper_no_redeemer_3 Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md For three-parameter scripts without redeemer. ```APIDOC ## Parameter3 / wrapper_no_redeemer_3 ### Description For three-parameter scripts without redeemer. ### Type Definition ```aiken pub type Parameter3 { param_0: p, param_1: q, param_2: s, } ``` ``` -------------------------------- ### Resolve Output Reference Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utils.md Finds the output corresponding to a given output reference by recursively searching transaction inputs. Fails if the reference is not found. ```aiken pub fn resolve_output_reference( inputs: List, output_ref: OutputReference, ) -> Output ``` -------------------------------- ### fold_from_root Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-linked-list.md Reduces a list by consuming the root and its first child, burning the child NFT. The continued root must have its link updated to what the child linked to. Validation receives 9 parameters including the root input, the folding node input, and both original and continued root data. ```APIDOC ## fold_from_root ### Description Consumes the root and its immediate first child, burning the child's NFT. The continued root must have its link updated to what the child linked to. Validation receives 9 parameters including the root input, the folding node input, and both original and continued root data. ### Signature ```aiken pub fn fold_from_root( anchor_root_input_outref: OutputReference, continued_anchor_root_output: Output, inputs: List, tx_mint: Value, additional_validations: fn( Input, LovelaceChange, RootData, Input, Lovelace, NodeKey, NodeData, Link, RootData, ) -> Bool, ) -> Eval ``` ``` -------------------------------- ### wrapper Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md Validates a parameter against its hash and invokes validator logic for single-parameter scripts. ```APIDOC ## wrapper ### Description Validates a parameter against its hash and invokes validator logic for single-parameter scripts. ### Method `wrapper` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters - **hashed_parameter** (Hash) - Required - The blake2b_224 hash of the expected parameter - **parameter_serialiser** (fn(p) -> ByteArray) - Required - Function to serialize the parameter to bytes - **outer_redeemer** (ParameterizedRedeemer) - Required - The redeemer containing parameter and custom data - **validator_function** (fn(p, redeemer) -> Bool) - Required - Validator logic that receives parameter and redeemer ### Request Example ```aiken use aiken_design_patterns/parameter_validation pub type MyRedeemer { action: Int, } pub type MyParam { owner: ByteArray, threshold: Int, } validator spend { spend( _datum, outer_redeemer: ParameterizedRedeemer, _own_ref, tx: Transaction, ) { let owner_hash = blake2b_224(#"owned_script_bytes") parameter_validation.wrapper( hashed_parameter: owner_hash, parameter_serialiser: fn(param) { // Serialize the parameter param.owner |> bytearray.concat(integer.to_bytearray(param.threshold)) }, outer_redeemer: outer_redeemer, validator_function: fn(param, redeemer) { and { param.threshold > 0, redeemer.action >= 0, } } ) } else(_) { fail } } ``` ### Response #### Success Response (Bool) - **True** if parameter hash validates and validator function succeeds. #### Response Example `Bool` ``` -------------------------------- ### run_element_with Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-linked-list.md Finalizes an `ElementEval` operation. ```APIDOC ## run_element_with ### Description Finalizes an `ElementEval` operation. ### Signature ```aiken pub fn run_element_with( reader: ElementEval, list_nft_policy_id: PolicyId, root_key: RootKey, node_key_prefix: NodeKeyPrefix, node_key_prefix_length: NodeKeyPrefixLength, ) -> a ``` ``` -------------------------------- ### Fuzzer for Input Lists with Authentic Script Input Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utils.md Generates sorted input lists that contain exactly one script input, along with the datum and associated data. ```aiken pub fn inputs_with_an_authentic_script_input_fuzzer( datum: Datum, ) -> Fuzzer<(ByteArray, Input, List)> ``` -------------------------------- ### Fuzzer for Output References Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utils.md Generates random output references. ```aiken pub fn output_reference_fuzzer() -> Fuzzer ``` -------------------------------- ### Apply Two Parameters to Script Template Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md Use `apply_param_2` to apply two parameters to a script template, where both parameters will be hashed. This is useful for templates requiring two distinct inputs. ```aiken pub fn apply_param_2( version: Int, prefix: ByteArray, param_0: ByteArray, param_1: ByteArray, ) -> ScriptHash ``` -------------------------------- ### Apply Single Parameter to Script Template Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md Use `apply_param` to compute the script hash resulting from applying a single, non-hashed parameter to a template script. The parameter is hashed using blake2b_224 before being combined with the prefix. ```aiken use aiken_design_patterns/parameter_validation let owner_address = #"1234567890abcdef..." let owner_hash = owner_address |> bytearray.hash // Pre-hash if needed let parameterized_script_hash = parameter_validation.apply_param( version: 3, prefix: my_script_prefix, param: owner_hash ) ``` -------------------------------- ### Aiken run_root_with Function Signature Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-linked-list.md Defines the signature for finalizing a `RootEval` operation. This function is used to complete root-related linked list operations. ```aiken pub fn run_root_with( reader: RootEval, list_nft_policy_id: PolicyId, root_key: RootKey, ) -> Bool ``` -------------------------------- ### Match and Validate Input-Output Pair Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/README.md Efficiently matches and validates a specific input-output pair within a transaction. This pattern is useful for ensuring data integrity and correct state transitions between transaction inputs and outputs. ```aiken singular_utxo_indexer.one_to_one( input_index: 0, output_index: 0, own_ref: own_out_ref, inputs: tx.inputs, outputs: tx.outputs, double_satisfaction_prevented: True, validation_logic: fn(input, output) { // Custom validation here input.output.value == output.value } ) ``` -------------------------------- ### Find First Script Input Index Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utils.md Finds the index of the first transaction input that has a script payment credential. This function will fail if no script inputs are found. ```aiken pub fn find_index_of_first_script_input(inputs: List) -> Int ``` -------------------------------- ### Apply Pre-Hashed Single Parameter to Script Template Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md Use `apply_prehashed_param` when the parameter is already hashed (e.g., a script hash). This function directly uses the provided pre-hashed parameter. ```aiken pub fn apply_prehashed_param( version: Int, prefix: ByteArray, param: ByteArray, ) -> ScriptHash ``` -------------------------------- ### Multi UTXO Indexer: One to One No Redeemer Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utxo-indexers.md Use this pattern to validate multiple script inputs against specific outputs without needing access to redeemers. Ensure input and output indices are provided in strictly ascending order. ```aiken use aiken_design_patterns/multi_utxo_indexer multi_utxo_indexer.one_to_one_no_redeemer( indices: [(0, 0), (1, 1), (2, 2)], spending_script_hash: my_script_hash, inputs: tx.inputs, outputs: tx.outputs, validation_logic: fn(in_idx, in_utxo, out_idx, out_utxo) { // Validate each input-output pair in_utxo.output.address == out_utxo.address } ) ``` -------------------------------- ### Sort Transaction Inputs Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utils.md Sorts transaction inputs by their output references in ascending order using canonical ordering. Useful for deterministic processing. ```aiken pub fn sort_inputs(inputs: List) -> List ``` -------------------------------- ### Fuzzers for Random Script Outputs and Inputs Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utils.md Generates random outputs and inputs at random script addresses, requiring a datum. ```aiken pub fn script_output_fuzzer(datum: Datum) -> Fuzzer<(ByteArray, Output)> ``` ```aiken pub fn script_input_fuzzer(datum: Datum) -> Fuzzer<(ByteArray, Input)> ``` -------------------------------- ### authentic_input_is_reproduced_unchanged Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utils.md Verify that an authenticated input is reproduced exactly. Checks if the output is an exact reproduction of the input with the same payment credential. ```APIDOC ## authentic_input_is_reproduced_unchanged ### Description Verify that an authenticated input is reproduced exactly. ### Parameters #### Path Parameters - **auth_symbol** (PolicyId) - Required - The policy ID of the authentication asset - **optional_auth_name** (Option) - Required - Asset name to verify (None accepts any) - **in_utxo** (Output) - Required - Input UTxO - **out_utxo** (Output) - Required - Output UTxO ### Return Type `Bool` — True if the output is an exact reproduction of the input with same payment credential. ``` -------------------------------- ### Singular UTXO Indexer: One to Many Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utxo-indexers.md Use this pattern when a single input UTXO needs to be validated against multiple output UTXOs. It allows for custom validation logic for the collective outputs and for each individual output. ```aiken singular_utxo_indexer.one_to_many( input_index: 0, output_indices: [0, 1, 2], own_ref: own_ref, inputs: tx.inputs, outputs: tx.outputs, double_satisfaction_prevented: True, input_collective_outputs_validator: fn(in_utxo, out_utxos) { // Called once: validate total output value let total_value = list.foldr(out_utxos, value.zero, fn(out, acc) { value.add(acc, out.value) }) total_value == in_utxo.output.value }, input_output_validator: fn(in_utxo, out_idx, out_utxo) { // Called for each output: per-output validation out_utxo.address == in_utxo.output.address } ) ``` -------------------------------- ### Singular UTxO Indexer: one_to_many Function Signature Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utxo-indexers.md Defines the signature for the `one_to_many` function, designed for mapping a single input to multiple outputs. It includes parameters for input and output indices, references, transaction details, and two distinct validation functions. ```aiken pub fn one_to_many( input_index: Int, output_indices: List, own_ref: OutputReference, inputs: List, outputs: List, double_satisfaction_prevented: Bool, input_collective_outputs_validator: fn(Input, List) -> Bool, input_output_validator: fn(Input, Int, Output) -> Bool, ) -> Bool ``` -------------------------------- ### Aiken run_eval_with Function Signature Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-linked-list.md Shows the signature for finalizing an `Eval` operation, which is necessary for linked list operations. It requires policy IDs and key prefixes for context. ```aiken pub fn run_eval_with( reader: Eval, list_nft_policy_id: PolicyId, root_key: RootKey, node_key_prefix: NodeKeyPrefix, node_key_prefix_length: NodeKeyPrefixLength, ) -> Bool ``` -------------------------------- ### Stake Validator Functions Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/README.md Functions for delegating computation to staking scripts using the 'withdraw zero trick'. Includes options for custom redeemer validation, including withdrawal amount, and a minimal overhead version. ```APIDOC ## Stake Validator ### Description Functions for delegating computation to staking scripts using the 'withdraw zero trick'. Includes options for custom redeemer validation, including withdrawal amount, and a minimal overhead version. ### Functions - `validate_withdraw` — with custom redeemer validation - `validate_withdraw_with_amount` — including withdrawal amount - `validate_withdraw_minimal` — minimal overhead version ``` -------------------------------- ### Aiken fold_from_root Function Signature Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-linked-list.md Defines the signature for folding a list from the root, consuming the root and its first child while burning the child's NFT. It requires detailed input parameters for transaction validation. ```aiken pub fn fold_from_root( anchor_root_input_outref: OutputReference, continued_anchor_root_output: Output, inputs: List, tx_mint: Value, additional_validations: fn( Input, LovelaceChange, RootData, Input, Lovelace, NodeKey, NodeData, Link, RootData, ) -> Bool, ) -> Eval ``` -------------------------------- ### Singular UTxO Indexer: one_to_one Function Signature Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utxo-indexers.md Defines the signature for the `one_to_one` function, which maps a single input to a single output with integrity validation. It requires specific indices, references, transaction details, and custom validation logic. ```aiken pub fn one_to_one( input_index: Int, output_index: Int, own_ref: OutputReference, inputs: List, outputs: List, double_satisfaction_prevented: Bool, validation_logic: fn(Input, Output) -> Bool, ) -> Bool ``` -------------------------------- ### Verify Authentic Input Reproduction Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-utils.md Verifies that an authenticated input UTxO is reproduced exactly in an output UTxO, checking for the same payment credential and optional asset name. ```aiken pub fn authentic_input_is_reproduced_unchanged( auth_symbol: PolicyId, optional_auth_name: Option, in_utxo: Output, out_utxo: Output, ) -> Bool ``` -------------------------------- ### Parameter Type Source: https://github.com/anastasia-labs/aiken-design-patterns/blob/main/_autodocs/api-reference-parameter-validation.md Represents a parameter for scripts that do not require custom redeemer data. ```APIDOC ## Parameter Type ### Description Represents a parameter for scripts that do not require custom redeemer data. ### Type Definition ```aiken pub type Parameter

{ param: p, } ``` ```