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 {
list.foldl(
self,
0,
fn(item, total) {
if predicate(item) {
total + 1
@ -15,7 +16,6 @@ pub fn count(self: List<a>, predicate: fn(a) -> Bool) -> Int {
total
}
},
0,
)
}
@ -25,9 +25,9 @@ test foldl_value_test1() {
let foo =
fn(i: Value, acc: (Value, Int)) {
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),
2,
)
@ -39,9 +39,9 @@ test foldl_value_test2() {
let foo =
fn(i: Value, acc: (Value, Int)) {
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),
2,
)

View File

@ -28,8 +28,7 @@ fn assert_purpose(purpose) {
ref.transaction_id == TransactionId(
#"0000000000000000000000000000000000000000000000000000000000000000",
) && 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,
]
|> list.and
_ ->
error @"unexpected number of outputs"
_ -> error @"unexpected number of outputs"
}
}

View File

@ -17,8 +17,7 @@ validator {
}
fn assert_datum(datum) {
let my_datum: Data =
Void
let my_datum: Data = Void
datum == my_datum
}
@ -27,14 +26,12 @@ type MyDatum {
}
fn assert_datums(datums) {
let my_datum =
MyDatum(42)
let my_datum = MyDatum(42)
expect Some(datum) =
dict.get(datums, blake2b_256(builtin.serialise_data(my_datum)))
expect datum: MyDatum =
datum
expect datum: MyDatum = datum
my_datum == datum && dict.size(datums) == 2
}
@ -43,8 +40,7 @@ fn assert_outputs(outputs) {
when outputs is {
[output_1, 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 {
InlineDatum(_) ->
True
_ ->
error @"expected inline datum"
InlineDatum(_) -> True
_ -> error @"expected inline datum"
},
]
|> list.and

View File

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

View File

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

View File

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

View File

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

View File

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