Skip to content

Commit

Permalink
test: add multi uxto indexer tests
Browse files Browse the repository at this point in the history
  • Loading branch information
hadelive committed May 4, 2024
1 parent 02fd6f3 commit 6a35120
Show file tree
Hide file tree
Showing 2 changed files with 582 additions and 6 deletions.
294 changes: 291 additions & 3 deletions validators/multi-utxo-indexer-example.ak
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
use aiken/transaction.{ScriptContext}
use aiken/transaction/value.{AssetName, PolicyId}
use aiken/dict
use aiken/fuzz
use aiken/hash.{Blake2b_224, Hash}
use aiken/interval
use aiken/transaction.{
Input, NoDatum, Output, OutputReference, ScriptContext, Spend, Transaction,
TransactionId, WithdrawFrom,
}
use aiken/transaction/credential.{Address, Inline, Script, ScriptCredential}
use aiken/transaction/value
use aiken_design_patterns/multi_utxo_indexer as multi_utxo_indexer
use aiken_design_patterns/stake_validator as stake_validator
use aiken_design_patterns/utils.{authentic_input_is_reproduced_unchanged}

validator(state_token_symbol: PolicyId, state_token_name: AssetName) {
validator(
state_token_symbol: value.PolicyId,
state_token_name: value.AssetName,
) {
fn spend(_datum, _redeemer, ctx: ScriptContext) {
stake_validator.spend(ctx)
}
Expand All @@ -24,3 +35,280 @@ validator(state_token_symbol: PolicyId, state_token_name: AssetName) {
)
}
}

test spend_validator() {
let own_ref =
OutputReference {
transaction_id: TransactionId {
hash: #"ee220ba9258ef159bb44f0d3522b840a80475eac2df7176f2c513d59a2ead71f",
},
output_index: 0,
}
let script_purpose = Spend(own_ref)
let own_val_hash: Hash<Blake2b_224, Script> =
#"99999999999999999999999999999999999999999999999999999999"
let input_utxo =
Input {
output_reference: own_ref,
output: Output {
address: Address {
payment_credential: ScriptCredential(own_val_hash),
stake_credential: None,
},
value: value.zero(),
datum: NoDatum,
reference_script: None,
},
}
let withdrawals =
dict.new()
|> dict.insert(Inline(ScriptCredential(own_val_hash)), 1, fn(_, _) { Less })
let tx =
Transaction {
inputs: [input_utxo],
reference_inputs: [],
outputs: [],
fee: value.zero(),
mint: value.zero() |> value.to_minted_value(),
certificates: [],
withdrawals,
validity_range: interval.everything(),
extra_signatories: [],
redeemers: dict.new(),
datums: dict.new(),
id: TransactionId {
hash: #"0000000000000000000000000000000000000000000000000000000000000000",
},
}
let ctx = ScriptContext { purpose: script_purpose, transaction: tx }
spend(#"", #"", 0, 0, ctx)
}

type SpendInput {
tx_id: ByteArray,
val_hash: ByteArray,
withdraw_amount: Int,
policy_id: ByteArray,
token_name: ByteArray,
}

fn random_spend_input() -> Fuzzer<SpendInput> {
fuzz.map5(
fuzz.bytearray_between(64, 64),
fuzz.bytearray_between(56, 56),
fuzz.int_at_least(1),
fuzz.bytearray_between(56, 56),
fuzz.bytearray_between(0, 32),
fn(tx_id, val_hash, withdraw_amount, policy_id, token_name) {
SpendInput(tx_id, val_hash, withdraw_amount, policy_id, token_name)
},
)
}

test prop_spend_validator(r via random_spend_input()) {
let own_ref =
OutputReference {
transaction_id: TransactionId { hash: r.tx_id },
output_index: 0,
}
let script_purpose = Spend(own_ref)
let own_val_hash: Hash<Blake2b_224, Script> = r.val_hash
let input_utxo =
Input {
output_reference: own_ref,
output: Output {
address: Address {
payment_credential: ScriptCredential(own_val_hash),
stake_credential: None,
},
value: value.zero(),
datum: NoDatum,
reference_script: None,
},
}
let withdrawals =
dict.new()
|> dict.insert(
Inline(ScriptCredential(own_val_hash)),
r.withdraw_amount,
fn(_, _) { Less },
)
let tx =
Transaction {
inputs: [input_utxo],
reference_inputs: [],
outputs: [],
fee: value.zero(),
mint: value.zero() |> value.to_minted_value(),
certificates: [],
withdrawals,
validity_range: interval.everything(),
extra_signatories: [],
redeemers: dict.new(),
datums: dict.new(),
id: TransactionId {
hash: #"0000000000000000000000000000000000000000000000000000000000000000",
},
}
let ctx = ScriptContext { purpose: script_purpose, transaction: tx }
let arbitrary_datum = #""
let arbitrary_redeemer = #""
spend(r.policy_id, r.token_name, arbitrary_datum, arbitrary_redeemer, ctx)
}

test withdraw_validator() {
let own_ref =
OutputReference {
transaction_id: TransactionId {
hash: #"ee220ba9258ef159bb44f0d3522b840a80475eac2df7176f2c513d59a2ead71f",
},
output_index: 0,
}
let own_val_hash = #"99999999999999999999999999999999999999999999999999999999"
let script_purpose = WithdrawFrom(Inline(ScriptCredential(own_val_hash)))
let state_token_symbol =
#"88888888888888888888888888888888888888888888888888888888"
let token_name = #"02"
let input_utxo =
Input {
output_reference: own_ref,
output: Output {
address: Address {
payment_credential: ScriptCredential(own_val_hash),
stake_credential: None,
},
value: value.add(
value.from_lovelace(2_000_000),
state_token_symbol,
token_name,
1,
),
datum: NoDatum,
reference_script: None,
},
}
let output =
Output {
address: Address {
payment_credential: ScriptCredential(own_val_hash),
stake_credential: None,
},
value: value.add(
value.from_lovelace(2_000_000),
state_token_symbol,
token_name,
1,
),
datum: NoDatum,
reference_script: None,
}
let redeemer =
[(0, 0)]
let withdrawals =
dict.new()
|> dict.insert(Inline(ScriptCredential(own_val_hash)), 1, fn(_, _) { Less })
let tx =
Transaction {
inputs: [input_utxo],
reference_inputs: [],
outputs: [output],
fee: value.zero(),
mint: value.zero() |> value.to_minted_value(),
certificates: [],
withdrawals,
validity_range: interval.everything(),
extra_signatories: [],
redeemers: dict.new(),
datums: dict.new(),
id: TransactionId {
hash: #"0000000000000000000000000000000000000000000000000000000000000000",
},
}
let ctx = ScriptContext { purpose: script_purpose, transaction: tx }
withdraw(state_token_symbol, token_name, redeemer, ctx)
}

type WithdrawInput {
state_token_symbol: ByteArray,
tx_id: ByteArray,
val_hash: ByteArray,
token_name: ByteArray,
}

fn random_withdraw_input() -> Fuzzer<WithdrawInput> {
fuzz.map4(
fuzz.bytearray_between(56, 56),
fuzz.bytearray_between(64, 64),
fuzz.bytearray_between(56, 56),
fuzz.bytearray_between(0, 32),
fn(state_token_symbol, tx_id, val_hash, token_name) {
WithdrawInput(state_token_symbol, tx_id, val_hash, token_name)
},
)
}

test prop_withdraw_validator(r via random_withdraw_input()) {
let own_ref =
OutputReference {
transaction_id: TransactionId { hash: r.tx_id },
output_index: 0,
}
let script_purpose = WithdrawFrom(Inline(ScriptCredential(r.val_hash)))
let input_utxo =
Input {
output_reference: own_ref,
output: Output {
address: Address {
payment_credential: ScriptCredential(r.val_hash),
stake_credential: None,
},
value: value.add(
value.from_lovelace(2_000_000),
r.state_token_symbol,
r.token_name,
1,
),
datum: NoDatum,
reference_script: None,
},
}
let output =
Output {
address: Address {
payment_credential: ScriptCredential(r.val_hash),
stake_credential: None,
},
value: value.add(
value.from_lovelace(2_000_000),
r.state_token_symbol,
r.token_name,
1,
),
datum: NoDatum,
reference_script: None,
}
let redeemer =
[(0, 0)]
let withdrawals =
dict.new()
|> dict.insert(Inline(ScriptCredential(r.val_hash)), 1, fn(_, _) { Less })
let tx =
Transaction {
inputs: [input_utxo],
reference_inputs: [],
outputs: [output],
fee: value.zero(),
mint: value.zero() |> value.to_minted_value(),
certificates: [],
withdrawals,
validity_range: interval.everything(),
extra_signatories: [],
redeemers: dict.new(),
datums: dict.new(),
id: TransactionId {
hash: #"0000000000000000000000000000000000000000000000000000000000000000",
},
}
let ctx = ScriptContext { purpose: script_purpose, transaction: tx }
withdraw(r.state_token_symbol, r.token_name, redeemer, ctx)
}
Loading

0 comments on commit 6a35120

Please sign in to comment.