diff --git a/README.md b/README.md index 687dd08..4a09038 100644 --- a/README.md +++ b/README.md @@ -83,24 +83,17 @@ fn log(self: a, serializer: fn(a) -> String) { So we can attach it to anywhere like this: ```diff -Input { - output_reference: OutputReference(TransactionId('tx_0'), 0), +let input = Input { + output_reference: OutputReference(TransactionId("tx_0"), 0), output: Output { + value: value.from_lovelace(111_000_000) |> value.add("pid", "name", 1), address: Address { - payment_credential: ScriptCredential('script_hash') - stake_credential: None + payment_credential: ScriptCredential("script_hash"), + stake_credential: None, }, - value: Value ([ - h'', - h'', - 111000000 - ],[ - h'706964', # pid - h'6E616D65', # name - 1 - ]), - datum: NoDatum - } + datum: NoDatum, + reference_script: None, + }, } + |> log(stringify.input) ``` diff --git a/lib/stringify.ak b/lib/stringify.ak index 45fa571..1c507f7 100644 --- a/lib/stringify.ak +++ b/lib/stringify.ak @@ -50,11 +50,11 @@ fn do_out_ref(self: OutputReference) { let OutputReference(TransactionId(tx_hash), index) = self @"OutputReference(TransactionId({}), {})" |> apply_params( - [ - string.from_bytearray(tx_hash) |> with_single_quotes, - string.from_int(index), - ], - ) + [ + string.from_bytearray(tx_hash) |> with_single_quotes, + string.from_int(index), + ], + ) } pub fn address(self: Address) { @@ -64,11 +64,11 @@ pub fn address(self: Address) { fn do_address(self: Address) { @"Address {\npayment_credential: {},\nstake_credential: {},\n}" |> apply_params( - [ - credential(self.payment_credential), - stake_credential(self.stake_credential), - ], - ) + [ + credential(self.payment_credential), + stake_credential(self.stake_credential), + ], + ) } pub fn value(self: Value) { @@ -78,27 +78,27 @@ pub fn value(self: Value) { fn do_value(self: Value) { @"Value" |> string.concat( - self - |> flatten - |> list.map( - fn(asset) { - let (pid, name, qty) = asset - [ - to_safe_string(pid) |> with_single_quotes, - to_safe_string(name) |> with_single_quotes, - string.from_int(qty), - ] - |> string.join(@", ") - |> with_parentheses - }, - ) - |> string.join( - @",\n", - ) - |> as_block - |> with_square_brackets - |> with_parentheses, - ) + self + |> flatten + |> list.map( + fn(asset) { + let (pid, name, qty) = asset + [ + to_safe_string(pid) |> with_single_quotes, + to_safe_string(name) |> with_single_quotes, + string.from_int(qty), + ] + |> string.join(@", ") + |> with_parentheses + }, + ) + |> string.join( + @",\n", + ) + |> as_block + |> with_square_brackets + |> with_parentheses, + ) } pub fn minted_value(self: MintedValue) { @@ -117,11 +117,11 @@ pub fn datum(self: Datum) { InlineDatum(dtm) -> @"InlineDatum({})" |> apply_params( - [ - cbor.diagnostic(dtm) - |> format_cbor, - ], - ) + [ + cbor.diagnostic(dtm) + |> format_cbor, + ], + ) _ -> fail @"unsupported datum" } } @@ -158,16 +158,16 @@ fn do_redeemers(self: Dict) { Spend(ref) -> @"Spend" |> string.concat( - out_ref(ref) - |> with_parentheses, - ) + out_ref(ref) + |> with_parentheses, + ) Mint(pid) -> @"Mint" |> string.concat( - string.from_bytearray(pid) - |> with_single_quotes - |> with_parentheses, - ) + string.from_bytearray(pid) + |> with_single_quotes + |> with_parentheses, + ) WithdrawFrom(cred) -> string.concat( @"WithdrawFrom", @@ -181,8 +181,8 @@ fn do_redeemers(self: Dict) { }, ) |> string.join( - @",\n", - ) + @",\n", + ) |> as_block |> with_square_brackets } @@ -190,50 +190,50 @@ fn do_redeemers(self: Dict) { pub fn tx(self: Transaction) { @"Transaction {\nredeemers: {},\nextra_signatories: {},\nmint: {},\nreference_inputs: {},\ninputs: {},\noutputs: {},\n}" |> apply_params( - [ - do_redeemers(self.redeemers), - self.extra_signatories - |> list.map( - fn(sig) { - string.from_bytearray(sig) - |> with_single_quotes - }, - ) - |> string.join(@", ") - |> with_square_brackets, - value(self.mint |> from_minted_value), - when self.reference_inputs is { - [] -> @"[]" - _ -> - list.map(self.reference_inputs, fn(inp) { do_input(inp) }) - |> string.join( - @",\n", - ) - |> as_block - |> with_square_brackets - }, - when self.inputs is { - [] -> @"[]" - _ -> - list.map(self.inputs, fn(inp) { do_input(inp) }) - |> string.join( - @",\n", - ) - |> as_block - |> with_square_brackets - }, - when self.outputs is { - [] -> @"[]" - _ -> - list.map(self.outputs, fn(out) { do_output(out) }) - |> string.join( - @",\n", - ) - |> as_block - |> with_square_brackets - }, - ], - ) + [ + do_redeemers(self.redeemers), + self.extra_signatories + |> list.map( + fn(sig) { + string.from_bytearray(sig) + |> with_single_quotes + }, + ) + |> string.join(@", ") + |> with_square_brackets, + value(self.mint |> from_minted_value), + when self.reference_inputs is { + [] -> @"[]" + _ -> + list.map(self.reference_inputs, fn(inp) { do_input(inp) }) + |> string.join( + @",\n", + ) + |> as_block + |> with_square_brackets + }, + when self.inputs is { + [] -> @"[]" + _ -> + list.map(self.inputs, fn(inp) { do_input(inp) }) + |> string.join( + @",\n", + ) + |> as_block + |> with_square_brackets + }, + when self.outputs is { + [] -> @"[]" + _ -> + list.map(self.outputs, fn(out) { do_output(out) }) + |> string.join( + @",\n", + ) + |> as_block + |> with_square_brackets + }, + ], + ) |> indent } @@ -269,15 +269,15 @@ fn do_indent(bytes: ByteArray, lvl: Int) { left |> bytearray.concat("\n") |> bytearray.concat( - tab( - if is_close(bytearray.slice(bytes, cursor + 1, cursor + 1)) { - indent_lvl - 1 - } else { - indent_lvl - }, - ) - |> bytearray.from_string, - ) + tab( + if is_close(bytearray.slice(bytes, cursor + 1, cursor + 1)) { + indent_lvl - 1 + } else { + indent_lvl + }, + ) + |> bytearray.from_string, + ) |> bytearray.concat(do_indent(right, indent_lvl)) } None -> bytes @@ -298,8 +298,8 @@ fn do_apply_params(template: ByteArray, params: List) -> ByteArray { bytearray.slice(template, 0, from - 1) |> bytearray.concat(bytearray.from_string(param)) |> bytearray.concat( - do_apply_params(bytearray.drop(template, to + 1), rest_params), - ) + do_apply_params(bytearray.drop(template, to + 1), rest_params), + ) } [] -> template } @@ -355,7 +355,7 @@ fn is_close(chr: ByteArray) { } } -pub fn newline(self: ByteArray) { +fn newline(self: ByteArray) { self |> bytearray.concat("\n") } @@ -408,98 +408,98 @@ fn to_bytearray(byte: Int) { fn do_format_cbor(bytes) { bytes |> bytearray.foldl( - ("", 0, False, ""), - fn(byte, results) { - let (builder, level, has_token, token) = results - let chr = to_bytearray(byte) - if is_open(chr) { - ( - builder - |> bytearray.concat(chr), - level + 1, - has_token, - token, - ) - } else if is_close(chr) { - ( - builder - |> fn(builder) { - if !has_token && builtin.length_of_bytearray(token) > 0 { - builder - |> bytearray.concat(", # ") - |> bytearray.concat( - token - |> hex_to_string, - ) - } else { - builder - } + ("", 0, False, ""), + fn(byte, results) { + let (builder, level, has_token, token) = results + let chr = to_bytearray(byte) + if is_open(chr) { + ( + builder + |> bytearray.concat(chr), + level + 1, + has_token, + token, + ) + } else if is_close(chr) { + ( + builder + |> fn(builder) { + if !has_token && builtin.length_of_bytearray(token) > 0 { + builder + |> bytearray.concat(", # ") + |> bytearray.concat( + token + |> hex_to_string, + ) + } else { + builder } - |> fn(builder) { - // pretty empty struct to be inline - let last_chr = builder |> pop_back(level + 1) |> last_char - if is_open(last_chr) { - builder |> pop_back(level + 1) - } else { - builder |> newline - } + } + |> fn(builder) { + // pretty empty struct to be inline + let last_chr = builder |> pop_back(level + 1) |> last_char + if is_open(last_chr) { + builder |> pop_back(level + 1) + } else { + builder |> newline } - |> bytearray.concat(chr), - level - 1, - has_token, - "", - ) - } else if chr == "'" { - (builder |> bytearray.concat(chr), level, !has_token, token) - } else if chr == "," { - ( - builder - |> bytearray.concat(chr) - |> fn(builder) { - if !has_token && builtin.length_of_bytearray(token) > 0 { - builder - |> bytearray.concat(" # ") - |> bytearray.concat( - token - |> hex_to_string, - ) - } else { - builder - } + } + |> bytearray.concat(chr), + level - 1, + has_token, + "", + ) + } else if chr == "'" { + (builder |> bytearray.concat(chr), level, !has_token, token) + } else if chr == "," { + ( + builder + |> bytearray.concat(chr) + |> fn(builder) { + if !has_token && builtin.length_of_bytearray(token) > 0 { + builder + |> bytearray.concat(" # ") + |> bytearray.concat( + token + |> hex_to_string, + ) + } else { + builder } - |> newline, - level, - has_token, - if !has_token { - "" - } else { - token - }, - ) - } else if chr == "_" { - ( - builder // |> pop_back(level + 1) - |> newline, - level, - has_token, - token, - ) - } else if chr == " " { - (builder, level, has_token, token) - } else { - ( - builder |> bytearray.concat(chr), - level, - has_token, - if has_token { - token |> bytearray.concat(chr) - } else { - token - }, - ) - } - }, - ) + } + |> newline, + level, + has_token, + if !has_token { + "" + } else { + token + }, + ) + } else if chr == "_" { + ( + builder // |> pop_back(level + 1) + |> newline, + level, + has_token, + token, + ) + } else if chr == " " { + (builder, level, has_token, token) + } else { + ( + builder |> bytearray.concat(chr), + level, + has_token, + if has_token { + token |> bytearray.concat(chr) + } else { + token + }, + ) + } + }, + ) |> first_of_quad } diff --git a/lib/stringify_test.ak b/lib/stringify_test.ak index 49bddab..8d3ebcf 100644 --- a/lib/stringify_test.ak +++ b/lib/stringify_test.ak @@ -1,7 +1,6 @@ use aiken/bytearray use aiken/cbor use aiken/dict -use aiken/string use aiken/transaction.{ InlineDatum, Input, Mint, NoDatum, Output, OutputReference, ScriptPurpose, Spend, Transaction, TransactionId, @@ -106,10 +105,7 @@ test log_data() { } fn script_purpose_compare(a: ScriptPurpose, b: ScriptPurpose) { - bytearray.compare( - a |> cbor.diagnostic |> string.to_bytearray, - b |> cbor.diagnostic |> string.to_bytearray, - ) + bytearray.compare(cbor.serialise(a), cbor.serialise(b)) } test log_tx() { @@ -119,21 +115,21 @@ test log_tx() { ..t, redeemers: t.redeemers |> dict.insert( - Spend(OutputReference(TransactionId("tx_1"), 12)), - Void, - script_purpose_compare, - ) + Spend(OutputReference(TransactionId("tx_1"), 12)), + Void, + script_purpose_compare, + ) |> dict.insert( - Mint("policy"), - MyDatum { - bytes: "bytes", - int: 1, - str: @"str", - int_list: [1, 2, 3, 4], - bytes_list: ["1", "2", "3"], - }, - script_purpose_compare, - ), + Mint("policy"), + MyDatum { + bytes: "bytes", + int: 1, + str: @"str", + int_list: [1, 2, 3, 4], + bytes_list: ["1", "2", "3"], + }, + script_purpose_compare, + ), extra_signatories: ["sig1", "sig2"], mint: value.from_asset("policy_id", "asset_name", 112) |> value.add("policy_id", "asset_name_2", 1)