Move script context e2e tests under a nested 'v2' directory.
And rework scripts to run them in anticipation of new v3 contexts.
This commit is contained in:
@@ -0,0 +1,51 @@
|
||||
use aiken/option
|
||||
use aiken/transaction.{NoDatum,
|
||||
ScriptContext, Spend, Transaction, TransactionId}
|
||||
use aiken/transaction/credential.{VerificationKeyCredential}
|
||||
use aiken/transaction/value
|
||||
|
||||
validator {
|
||||
fn spend(_datum: Void, _redeemer: Void, ctx: ScriptContext) {
|
||||
and {
|
||||
assert_id(ctx.transaction),
|
||||
assert_purpose(ctx.purpose),
|
||||
assert_outputs(ctx.transaction),
|
||||
assert_fee(ctx.transaction),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_id(transaction: Transaction) {
|
||||
transaction.id != TransactionId(
|
||||
#"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
)
|
||||
}
|
||||
|
||||
fn assert_purpose(purpose) {
|
||||
when purpose is {
|
||||
Spend(ref) ->
|
||||
ref.transaction_id == TransactionId(
|
||||
#"0000000000000000000000000000000000000000000000000000000000000000",
|
||||
) && ref.output_index == 0
|
||||
_ -> fail @"script purpose isn't 'Spend'"
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_fee(transaction: Transaction) {
|
||||
transaction.fee == value.from_lovelace(42)
|
||||
}
|
||||
|
||||
fn assert_outputs(transaction: Transaction) {
|
||||
when transaction.outputs is {
|
||||
[output] -> and {
|
||||
output.value == value.from_lovelace(1000000000),
|
||||
output.address.payment_credential == VerificationKeyCredential(
|
||||
#"11111111111111111111111111111111111111111111111111111111",
|
||||
),
|
||||
option.is_none(output.address.stake_credential),
|
||||
output.datum == NoDatum,
|
||||
output.reference_script == None,
|
||||
}
|
||||
_ -> fail @"unexpected number of outputs"
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
use aiken/builtin
|
||||
use aiken/dict.{Dict}
|
||||
use aiken/hash.{blake2b_256}
|
||||
use aiken/transaction.{DatumHash, InlineDatum, Output, ScriptContext}
|
||||
use aiken/transaction/credential.{Inline, VerificationKeyCredential}
|
||||
|
||||
validator {
|
||||
fn spend(datum: Data, _redeemer: Data, ctx: ScriptContext) {
|
||||
and {
|
||||
assert_datum(datum),
|
||||
assert_datums(ctx.transaction.datums),
|
||||
assert_outputs(ctx.transaction.outputs),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_datum(datum) {
|
||||
let my_datum: Data = Void
|
||||
datum == my_datum
|
||||
}
|
||||
|
||||
type MyDatum {
|
||||
MyDatum(Int)
|
||||
}
|
||||
|
||||
fn assert_datums(datums: Dict<ByteArray, Data>) {
|
||||
let my_datum = MyDatum(42)
|
||||
|
||||
expect Some(datum) =
|
||||
dict.get(datums, blake2b_256(builtin.serialise_data(my_datum)))
|
||||
|
||||
expect datum: MyDatum = datum
|
||||
|
||||
my_datum == datum && dict.size(datums) == 2
|
||||
}
|
||||
|
||||
fn assert_outputs(outputs) {
|
||||
when outputs is {
|
||||
[output_1, output_2, ..] ->
|
||||
assert_first_output(output_1) && assert_second_output(output_2)
|
||||
_ -> fail @"expected transaction to have (at least) 2 outputs"
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_first_output(output: Output) {
|
||||
and {
|
||||
output.datum == DatumHash(
|
||||
#"fcaa61fb85676101d9e3398a484674e71c45c3fd41b492682f3b0054f4cf3273",
|
||||
),
|
||||
output.reference_script == Some(
|
||||
#"e37db487fbd58c45d059bcbf5cd6b1604d3bec16cf888f1395a4ebc4",
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_second_output(output: Output) {
|
||||
and {
|
||||
output.address.stake_credential == Some(
|
||||
Inline(
|
||||
VerificationKeyCredential(
|
||||
#"66666666666666666666666666666666666666666666666666666666",
|
||||
),
|
||||
),
|
||||
),
|
||||
when output.datum is {
|
||||
InlineDatum(_) -> True
|
||||
_ -> fail @"expected inline datum"
|
||||
},
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
use aiken/dict
|
||||
use aiken/list
|
||||
use aiken/transaction.{Mint, ScriptContext, Transaction}
|
||||
use aiken/transaction/value
|
||||
|
||||
validator {
|
||||
fn mint(redeemer: Data, ctx: ScriptContext) {
|
||||
and {
|
||||
assert_purpose(ctx),
|
||||
assert_mint(ctx.purpose, ctx.transaction),
|
||||
assert_redeemers(ctx, redeemer),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_purpose(ctx: ScriptContext) {
|
||||
expect [my_policy_id] =
|
||||
ctx.transaction.mint
|
||||
|> value.from_minted_value
|
||||
|> value.without_lovelace
|
||||
|> value.policies
|
||||
|
||||
expect Mint(policy_id) = ctx.purpose
|
||||
|
||||
my_policy_id == policy_id
|
||||
}
|
||||
|
||||
fn assert_mint(purpose, transaction: Transaction) {
|
||||
expect Mint(policy_id) = purpose
|
||||
let tokens =
|
||||
value.tokens(transaction.mint |> value.from_minted_value, policy_id)
|
||||
|
||||
when dict.get(tokens, #"666f6f") is {
|
||||
None -> fail @"token not found"
|
||||
Some(quantity) -> quantity == 1337
|
||||
}
|
||||
}
|
||||
|
||||
fn assert_redeemers(ctx: ScriptContext, my_redeemer) {
|
||||
expect Some(Pair(_, redeemer)) =
|
||||
list.find(ctx.transaction.redeemers, fn(kv) { kv.1st == ctx.purpose })
|
||||
my_redeemer == redeemer && list.length(ctx.transaction.redeemers) == 1
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
use aiken/list
|
||||
use aiken/transaction.{ScriptContext}
|
||||
use aiken/transaction/credential.{
|
||||
Inline, ScriptCredential, VerificationKeyCredential,
|
||||
}
|
||||
|
||||
validator {
|
||||
fn spend(_datum: Void, _redeemer: Void, ctx: ScriptContext) {
|
||||
let alice =
|
||||
Inline(
|
||||
VerificationKeyCredential(
|
||||
#"22222222222222222222222222222222222222222222222222222222",
|
||||
),
|
||||
)
|
||||
|
||||
let bob =
|
||||
Inline(
|
||||
ScriptCredential(
|
||||
#"afddc16c18e7d8de379fb9aad39b3d1b5afd27603e5ebac818432a72",
|
||||
),
|
||||
)
|
||||
|
||||
and {
|
||||
when
|
||||
list.find(ctx.transaction.withdrawals, fn(kv) { kv.1st == alice })
|
||||
is {
|
||||
None -> fail @"alice's withdrawal not found"
|
||||
Some(value) -> value.2nd == 42
|
||||
},
|
||||
when list.find(ctx.transaction.withdrawals, fn(kv) { kv.1st == bob }) is {
|
||||
None -> fail @"bob's withdrawal not found"
|
||||
Some(value) -> value.2nd == 14
|
||||
},
|
||||
list.map(ctx.transaction.withdrawals, fn(kv) { kv.1st }) == [alice, bob],
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user