fix: gift card example stdlib update

closes #596
This commit is contained in:
rvcas 2023-06-13 00:38:29 -04:00
parent 22d491bb88
commit 37b2f0c239
No known key found for this signature in database
GPG Key ID: C09B64E263F7D68C
8 changed files with 51 additions and 85 deletions

View File

@ -8,6 +8,7 @@ use aiken/transaction/value.{Value}
pub fn count(self: List<a>, predicate: fn(a) -> Bool) -> Int { pub fn count(self: List<a>, predicate: fn(a) -> Bool) -> Int {
list.foldl( list.foldl(
self, self,
0,
fn(item, total) { fn(item, total) {
if predicate(item) { if predicate(item) {
total + 1 total + 1
@ -15,7 +16,6 @@ pub fn count(self: List<a>, predicate: fn(a) -> Bool) -> Int {
total total
} }
}, },
0,
) )
} }
@ -25,9 +25,9 @@ test foldl_value_test1() {
let foo = let foo =
fn(i: Value, acc: (Value, Int)) { fn(i: Value, acc: (Value, Int)) {
let (v, int) = acc let (v, int) = acc
(value.add(i, v), int + 1) (value.merge(i, v), int + 1)
} }
list.foldl([val1, val2], foo, (value.zero(), 0)) == ( list.foldl([val1, val2], (value.zero(), 0), foo) == (
value.from_lovelace(3000000), value.from_lovelace(3000000),
2, 2,
) )
@ -39,9 +39,9 @@ test foldl_value_test2() {
let foo = let foo =
fn(i: Value, acc: (Value, Int)) { fn(i: Value, acc: (Value, Int)) {
let (v, int) = acc let (v, int) = acc
(value.add(i, v), int + 1) (value.merge(i, v), int + 1)
} }
list.foldl([val1, val2], foo, (value.from_lovelace(0), 0)) == ( list.foldl([val1, val2], (value.from_lovelace(0), 0), foo) == (
value.from_lovelace(3000000), value.from_lovelace(3000000),
2, 2,
) )

View File

@ -28,8 +28,7 @@ fn assert_purpose(purpose) {
ref.transaction_id == TransactionId( ref.transaction_id == TransactionId(
#"0000000000000000000000000000000000000000000000000000000000000000", #"0000000000000000000000000000000000000000000000000000000000000000",
) && ref.output_index == 0 ) && ref.output_index == 0
_ -> _ -> error @"script purpose isn't 'Spend'"
error @"script purpose isn't 'Spend'"
} }
} }
@ -50,7 +49,6 @@ fn assert_outputs(transaction) {
output.reference_script == None, output.reference_script == None,
] ]
|> list.and |> list.and
_ -> _ -> error @"unexpected number of outputs"
error @"unexpected number of outputs"
} }
} }

View File

@ -17,8 +17,7 @@ validator {
} }
fn assert_datum(datum) { fn assert_datum(datum) {
let my_datum: Data = let my_datum: Data = Void
Void
datum == my_datum datum == my_datum
} }
@ -27,14 +26,12 @@ type MyDatum {
} }
fn assert_datums(datums) { fn assert_datums(datums) {
let my_datum = let my_datum = MyDatum(42)
MyDatum(42)
expect Some(datum) = expect Some(datum) =
dict.get(datums, blake2b_256(builtin.serialise_data(my_datum))) dict.get(datums, blake2b_256(builtin.serialise_data(my_datum)))
expect datum: MyDatum = expect datum: MyDatum = datum
datum
my_datum == datum && dict.size(datums) == 2 my_datum == datum && dict.size(datums) == 2
} }
@ -43,8 +40,7 @@ fn assert_outputs(outputs) {
when outputs is { when outputs is {
[output_1, output_2, ..] -> [output_1, output_2, ..] ->
assert_first_output(output_1) && assert_second_output(output_2) assert_first_output(output_1) && assert_second_output(output_2)
_ -> _ -> error @"expected transaction to have (at least) 2 outputs"
error @"expected transaction to have (at least) 2 outputs"
} }
} }
@ -70,10 +66,8 @@ fn assert_second_output(output) {
), ),
), ),
when output.datum is { when output.datum is {
InlineDatum(_) -> InlineDatum(_) -> True
True _ -> error @"expected inline datum"
_ ->
error @"expected inline datum"
}, },
] ]
|> list.and |> list.and

View File

@ -19,28 +19,22 @@ fn assert_purpose(ctx) {
ctx.transaction.mint ctx.transaction.mint
|> value.without_lovelace |> value.without_lovelace
|> value.policies |> value.policies
expect Mint(policy_id) = expect Mint(policy_id) = ctx.purpose
ctx.purpose
my_policy_id == policy_id my_policy_id == policy_id
} }
fn assert_mint(purpose, transaction) { fn assert_mint(purpose, transaction) {
expect Mint(policy_id) = expect Mint(policy_id) = purpose
purpose let tokens = value.tokens(transaction.mint, policy_id)
let tokens =
value.tokens(transaction.mint, policy_id)
when dict.get(tokens, #"666f6f") is { when dict.get(tokens, #"666f6f") is {
None -> None -> error @"token not found"
error @"token not found" Some(quantity) -> quantity == 1337
Some(quantity) ->
quantity == 1337
} }
} }
fn assert_redeemers(ctx, my_redeemer) { fn assert_redeemers(ctx, my_redeemer) {
expect Some(redeemer) = expect Some(redeemer) = dict.get(ctx.transaction.redeemers, ctx.purpose)
dict.get(ctx.transaction.redeemers, ctx.purpose)
my_redeemer == redeemer && dict.size(ctx.transaction.redeemers) == 1 my_redeemer == redeemer && dict.size(ctx.transaction.redeemers) == 1
} }

View File

@ -23,16 +23,12 @@ validator {
[ [
when dict.get(ctx.transaction.withdrawals, alice) is { when dict.get(ctx.transaction.withdrawals, alice) is {
None -> None -> error @"alice's withdrawal not found"
error @"alice's withdrawal not found" Some(value) -> value == 42
Some(value) ->
value == 42
}, },
when dict.get(ctx.transaction.withdrawals, bob) is { when dict.get(ctx.transaction.withdrawals, bob) is {
None -> None -> error @"bob's withdrawal not found"
error @"bob's withdrawal not found" Some(value) -> value == 14
Some(value) ->
value == 14
}, },
dict.keys(ctx.transaction.withdrawals) == [alice, bob], dict.keys(ctx.transaction.withdrawals) == [alice, bob],
] ]

View File

@ -24,14 +24,11 @@ validator(creator: ByteArray) {
_r: Data, _r: Data,
ctx: ScriptContext, ctx: ScriptContext,
) { ) {
let ScriptContext { transaction, purpose } = let ScriptContext { transaction, purpose } = ctx
ctx
let Transaction { inputs, mint, .. } = let Transaction { inputs, mint, .. } = transaction
transaction
expect Spend(own_ref) = expect Spend(own_ref) = purpose
purpose
expect Some(own_input) = expect Some(own_input) =
list.find(inputs, fn(input) { input.output_reference == own_ref }) list.find(inputs, fn(input) { input.output_reference == own_ref })
@ -39,39 +36,37 @@ validator(creator: ByteArray) {
let Input { let Input {
output: Output { address: Address { payment_credential, .. }, .. }, output: Output { address: Address { payment_credential, .. }, .. },
.. ..
} = } = own_input
own_input
expect ScriptCredential(own_validator_hash) = expect ScriptCredential(own_validator_hash) = payment_credential
payment_credential
value.quantity_of(mint, own_validator_hash, datum) == -1 (
mint
|> value.from_minted_value
|> value.quantity_of(own_validator_hash, datum)
) == -1
} }
fn gift_card(rdmr: Action, ctx: ScriptContext) -> Bool { fn gift_card(rdmr: Action, ctx: ScriptContext) -> Bool {
// get values from transaction and purpose // get values from transaction and purpose
let ScriptContext { transaction, purpose } = let ScriptContext { transaction, purpose } = ctx
ctx
expect tx.Mint(policy_id) = expect tx.Mint(policy_id) = purpose
purpose
let Transaction { inputs, mint, extra_signatories, outputs, .. } = let Transaction { inputs, mint, extra_signatories, outputs, .. } =
transaction transaction
let minted_assets = let minted_assets =
mint mint
|> from_minted_value |> value.from_minted_value
|> value.tokens(policy_id) |> value.tokens(policy_id)
|> dict.to_list() |> dict.to_list()
when rdmr is { when rdmr is {
Mint(total) -> { Mint(total) -> {
expect [input, ..] = expect [input, ..] = inputs
inputs
// Base is created from serializing a utxo ref being spent. Thus this guarantees a unique base // Base is created from serializing a utxo ref being spent. Thus this guarantees a unique base
let base = let base = cbor.serialise(input.output_reference)
cbor.serialise(input.output_reference)
// Create a list of expected token names // Create a list of expected token names
let expected_minted_token_names = let expected_minted_token_names =
create_expected_minted_nfts(base, total, []) create_expected_minted_nfts(base, total, [])
@ -91,8 +86,7 @@ validator(creator: ByteArray) {
list.all( list.all(
minted_assets, minted_assets,
fn(asset) { fn(asset) {
let (_, amount) = let (_, amount) = asset
asset
amount == -1 amount == -1
}, },
) )
@ -123,8 +117,7 @@ fn check_mint_and_outputs(
validator_cred: PaymentCredential, validator_cred: PaymentCredential,
) -> Bool { ) -> Bool {
when minted_assets is { when minted_assets is {
[] -> [] -> True
True
[(minted_asset_name, quantity), ..rest_assets] -> { [(minted_asset_name, quantity), ..rest_assets] -> {
expect True = expect True =
list.any( list.any(
@ -135,8 +128,7 @@ fn check_mint_and_outputs(
list.any( list.any(
outputs, outputs,
fn(output) { fn(output) {
let Output { address, datum, .. } = let Output { address, datum, .. } = output
output
datum == InlineDatum(minted_asset_name) && address.payment_credential == validator_cred datum == InlineDatum(minted_asset_name) && address.payment_credential == validator_cred
}, },
) )
@ -158,8 +150,7 @@ fn create_expected_minted_nfts(
if counter == 0 { if counter == 0 {
accum accum
} else { } else {
let token_name = let token_name = blake2b_256(bytearray.push(base, counter))
blake2b_256(bytearray.push(base, counter))
let accum = let accum =
[token_name, ..accum] [token_name, ..accum]

View File

@ -10,18 +10,15 @@ type Action {
validator(token_name: ByteArray, utxo_ref: OutputReference) { validator(token_name: ByteArray, utxo_ref: OutputReference) {
fn gift_card(rdmr: Action, ctx: ScriptContext) -> Bool { fn gift_card(rdmr: Action, ctx: ScriptContext) -> Bool {
let ScriptContext { transaction, purpose } = let ScriptContext { transaction, purpose } = ctx
ctx
expect tx.Mint(policy_id) = expect tx.Mint(policy_id) = purpose
purpose
let Transaction { inputs, mint, .. } = let Transaction { inputs, mint, .. } = transaction
transaction
expect [(asset_name, amount)] = expect [(asset_name, amount)] =
mint mint
|> from_minted_value |> value.from_minted_value
|> value.tokens(policy_id) |> value.tokens(policy_id)
|> dict.to_list() |> dict.to_list()
@ -31,23 +28,20 @@ validator(token_name: ByteArray, utxo_ref: OutputReference) {
list.find(inputs, fn(input) { input.output_reference == utxo_ref }) list.find(inputs, fn(input) { input.output_reference == utxo_ref })
amount == 1 && asset_name == token_name amount == 1 && asset_name == token_name
} }
Burn -> Burn -> amount == -1 && asset_name == token_name
amount == -1 && asset_name == token_name
} }
} }
} }
validator(token_name: ByteArray, policy_id: ByteArray) { validator(token_name: ByteArray, policy_id: ByteArray) {
fn redeem(_d: Data, _r: Data, ctx: ScriptContext) -> Bool { fn redeem(_d: Data, _r: Data, ctx: ScriptContext) -> Bool {
let ScriptContext { transaction, .. } = let ScriptContext { transaction, .. } = ctx
ctx
let Transaction { mint, .. } = let Transaction { mint, .. } = transaction
transaction
expect [(asset_name, amount)] = expect [(asset_name, amount)] =
mint mint
|> from_minted_value |> value.from_minted_value
|> value.tokens(policy_id) |> value.tokens(policy_id)
|> dict.to_list() |> dict.to_list()

View File

@ -13,8 +13,7 @@ type Redeemer {
validator { validator {
fn spend(datum: Datum, redeemer: Redeemer, context: ScriptContext) -> Bool { fn spend(datum: Datum, redeemer: Redeemer, context: ScriptContext) -> Bool {
let must_say_hello = let must_say_hello = redeemer.msg == "Hello, World!"
redeemer.msg == "Hello, World!"
let must_be_signed = let must_be_signed =
list.has(context.transaction.extra_signatories, datum.owner) list.has(context.transaction.extra_signatories, datum.owner)