-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First Draft for Singular One-to-Many UTxO Indexer
- Loading branch information
Showing
1 changed file
with
42 additions
and
0 deletions.
There are no files selected for viewing
42 changes: 42 additions & 0 deletions
42
lib/aiken-design-patterns/singular-utxo-indexer-one-to-many.ak
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use aiken/list | ||
use aiken/transaction.{Input, Output, ScriptContext, Spend, Transaction} | ||
|
||
pub fn spend( | ||
validation_logic: fn(Output, List<Output>) -> Bool, | ||
redeemer: (Int, List<Int>), | ||
ctx: ScriptContext, | ||
) -> Bool { | ||
expect ScriptContext { transaction: tx, purpose: Spend(own_ref) } = ctx | ||
|
||
let Transaction { inputs, outputs, .. } = tx | ||
|
||
let (in_ix, out_ixs) = redeemer | ||
|
||
expect Some(Input { output: in_utxo, output_reference: in_ref }) = | ||
inputs |> list.at(in_ix) | ||
|
||
let fold_res = | ||
list.foldr( | ||
out_ixs, | ||
(list.length(outputs), []), | ||
fn(curr_ix, acc_tuple) { | ||
let (prev_ix, acc) = acc_tuple | ||
// Folding from right and expecting ascending order from head to tail | ||
// (for a more intuitive API) to disallow duplicates. | ||
if curr_ix < prev_ix { | ||
expect Some(out_utxo) = outputs |> list.at(curr_ix) | ||
(curr_ix, acc |> list.push(out_utxo)) | ||
} else { | ||
// Output indices must be ordered. | ||
fail | ||
} | ||
}, | ||
) | ||
|
||
let (_, out_utxos) = fold_res | ||
|
||
// Indicated input must match the spending one. | ||
expect (own_ref == in_ref)? | ||
|
||
validation_logic(in_utxo, out_utxos) | ||
} |