diff --git a/examples/acceptance_tests/071/aiken.lock b/examples/acceptance_tests/071/aiken.lock new file mode 100644 index 00000000..0423f31b --- /dev/null +++ b/examples/acceptance_tests/071/aiken.lock @@ -0,0 +1,13 @@ +# This file was generated by Aiken +# You typically do not need to edit this file + +[[requirements]] +name = "aiken-lang/stdlib" +version = "main" +source = "github" + +[[packages]] +name = "aiken-lang/stdlib" +version = "main" +requirements = [] +source = "github" diff --git a/examples/acceptance_tests/071/aiken.toml b/examples/acceptance_tests/071/aiken.toml new file mode 100644 index 00000000..1acf73dc --- /dev/null +++ b/examples/acceptance_tests/071/aiken.toml @@ -0,0 +1,7 @@ +name = "aiken-lang/acceptance_test_071" +version = "0.0.0" + +[[dependencies]] +name = 'aiken-lang/stdlib' +version = 'main' +source = 'github' diff --git a/examples/acceptance_tests/071/plutus.json b/examples/acceptance_tests/071/plutus.json new file mode 100644 index 00000000..1cbf4f11 --- /dev/null +++ b/examples/acceptance_tests/071/plutus.json @@ -0,0 +1,167 @@ +{ + "preamble": { + "title": "aiken-lang/acceptance_test_071", + "version": "0.0.0", + "plutusVersion": "v2" + }, + "validators": [ + { + "title": "spend.pool_contract", + "datum": { + "title": "PoolDatum", + "anyOf": [ + { + "title": "PoolDatum", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "currency_symbol", + "anyOf": [ + { + "title": "CurrencySymbol", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "policy_id", + "dataType": "bytes" + }, + { + "title": "asset_name", + "dataType": "bytes" + } + ] + } + ] + }, + { + "title": "balance", + "dataType": "integer" + }, + { + "title": "lent_out", + "dataType": "integer" + } + ] + } + ] + }, + "redeemer": { + "title": "PoolRedeemer", + "anyOf": [ + { + "title": "PoolRedeemer", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "action", + "anyOf": [ + { + "title": "PoolWithdraw", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "dataType": "integer" + } + ] + }, + { + "title": "PoolDeposit", + "dataType": "constructor", + "index": 1, + "fields": [ + { + "title": "PoolDepositRedeemer", + "anyOf": [ + { + "title": "PoolDepositRedeemer", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "input_cs", + "anyOf": [ + { + "title": "CurrencySymbol", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "policy_id", + "dataType": "bytes" + }, + { + "title": "asset_name", + "dataType": "bytes" + } + ] + } + ] + }, + { + "title": "input_amount", + "dataType": "integer" + } + ] + } + ] + } + ] + }, + { + "title": "PoolBorrow", + "dataType": "constructor", + "index": 2, + "fields": [ + { + "title": "PoolBorrowRedeemer", + "anyOf": [ + { + "title": "PoolBorrowRedeemer", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "input_cs", + "anyOf": [ + { + "title": "CurrencySymbol", + "dataType": "constructor", + "index": 0, + "fields": [ + { + "title": "policy_id", + "dataType": "bytes" + }, + { + "title": "asset_name", + "dataType": "bytes" + } + ] + } + ] + }, + { + "title": "input_amount", + "dataType": "integer" + } + ] + } + ] + } + ] + } + ] + } + ] + } + ] + }, + "compiledCode": "59030501000032323232323232323232322225333006323232323232323232533300f3370e002900009925130090021533300f3370e0029001099191919299980999b87001480084c8c8cccc8888c8c8c8c8c9289812000980b19299980e99b8748000c080dd500088008a9980fa492a4173736572746564206f6e20696e636f727265637420636f6e7374727563746f722076617269616e742e0016330120070033022001301432533301b3370e9000180f1baa0011001153301d49012a4173736572746564206f6e20696e636f727265637420636f6e7374727563746f722076617269616e742e00163300f005001300d48901ff00010001012005301b001300d00214a0602a6ea8004cc028c02c03120023017001300900213232323253330133370e0029001099191999911119191919192513024001301632533301d3370e900018101baa0011001153301f49012a4173736572746564206f6e20696e636f727265637420636f6e7374727563746f722076617269616e742e0016330120070033022001301432533301b3370e9000180f1baa0011001153301d49012a4173736572746564206f6e20696e636f727265637420636f6e7374727563746f722076617269616e742e00163300f005001300d48901ff00010001012005301b001300d00214a0602a6ea8004cc028c02c031200230170013009002301137540026600c600e0129000119ba548000cc04ccdd2a4004660266ea40052f5c06602666e9520024bd7025eb8088cc010dd6198031803998031803801240009002119baf3300730080014800000888cc00cdd6198029803198029803001240009000119baf3300630073300630070014800920000023001001222533301000213374a900125eb804c8c94ccc034c00c0084cdd2a40006602600497ae013330050050010033014003301200222323330010014800000c888cccc030cdc3802001009919980200219b8000348008c0540040048c02cdd50008a4c2c6002002444a666012004293099802980098058011998018019806001000ab9a5736ae7155ceaab9e5573eae815d0aba201", + "hash": "c95b3842362b77afec21773b7c0b1f09e61bf5e4c58b685533e6d342" + } + ] +} \ No newline at end of file diff --git a/examples/acceptance_tests/071/validators/spend.ak b/examples/acceptance_tests/071/validators/spend.ak new file mode 100644 index 00000000..b8e7c39b --- /dev/null +++ b/examples/acceptance_tests/071/validators/spend.ak @@ -0,0 +1,112 @@ +use aiken/hash.{Blake2b_224, Hash} +use aiken/interval.{Finite} +use aiken/list +use aiken/option +use aiken/transaction.{ + Datum, InlineDatum, Input, Mint, Output, OutputReference, ScriptContext, Spend, + Transaction, TransactionId, ValidityRange, +} +use aiken/transaction/credential.{ + Address, Script, ScriptCredential, VerificationKey, VerificationKeyCredential, +} +use aiken/transaction/value.{ + AssetName, PolicyId, Value, add, flatten, from_asset, negate, quantity_of, +} + +// Datum/Redeemer pool +pub type PoolDatum { + currency_symbol: CurrencySymbol, + balance: Int, + lent_out: Int, +} + +type PoolRedeemer { + action: PoolRedeemerType, +} + +pub type PoolRedeemerType { + PoolWithdraw(Int) + PoolDeposit(PoolDepositRedeemer) + PoolBorrow(PoolBorrowRedeemer) +} + +pub type PoolDepositRedeemer { + input_cs: CurrencySymbol, + input_amount: Int, +} + +pub type PoolBorrowRedeemer { + input_cs: CurrencySymbol, + input_amount: Int, +} + +pub type CurrencySymbol { + policy_id: PolicyId, + asset_name: AssetName, +} + +pub fn get_output(ctx: ScriptContext, address: Address) -> Option { + list.find(ctx.transaction.outputs, fn(output) { output.address == address }) +} + +pub fn get_input(ctx: ScriptContext, address: Address) -> Option { + list.find( + ctx.transaction.inputs, + fn(input) { input.output.address == address }, + ) +} + +pub fn scripthash_address(scripthash: ByteArray) { + Address { + payment_credential: ScriptCredential(scripthash), + stake_credential: None, + } +} + +pub fn validate_pool_deposit( + ctx: ScriptContext, + output_reference: OutputReference, + datum: PoolDatum, + redeemer: PoolDepositRedeemer, +) -> Bool { + let validator_address = scripthash_address(#"ff") + + expect Some(pool_output) = get_output(ctx, validator_address) + expect Some(pool_input) = get_input(ctx, validator_address) + + True +} + +pub fn validate_pool_borrow( + ctx: ScriptContext, + output_reference: OutputReference, + datum: PoolDatum, + redeemer: PoolBorrowRedeemer, +) -> Bool { + let validator_address = scripthash_address(#"ff") + + // Uncommend bellow to make everything worse + expect Some(pool_output) = get_output(ctx, validator_address) + expect Some(pool_input) = get_input(ctx, validator_address) + True +} + +validator pool_contract { + fn(datum: PoolDatum, redeemer: PoolRedeemer, ctx: ScriptContext) { + when redeemer.action is { + PoolWithdraw(_) -> True + PoolDeposit(pool_deposit_redeemer) -> + when ctx.purpose is { + Spend(output_ref) -> + validate_pool_deposit(ctx, output_ref, datum, pool_deposit_redeemer) + _ -> False + } + PoolBorrow(pool_borrow_redeemer) -> + when ctx.purpose is { + Spend(output_ref) -> + validate_pool_borrow(ctx, output_ref, datum, pool_borrow_redeemer) + _ -> False + } + } + } +}