Skip to content

Commit

Permalink
refactor: extend datum structure
Browse files Browse the repository at this point in the history
  • Loading branch information
HarunJr committed Oct 1, 2024
1 parent 6020152 commit 456b9f6
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,9 +144,6 @@ pub fn validate_remove_account(
(value.quantity_of(minted_value, apolicy_id, ref_asset_name) == -1)?,
// Check that the user NFT is burned
(value.quantity_of(minted_value, upolicy_id, usr_asset_name) == -1)?,
// // One Script output leaving the script address.
// count.outputs_by_addr(tx.outputs, account_addr, 1)?,
// is_ref_nft_burned?,
// Ensure no output contains the reference NFT
list.all(
tx.outputs,
Expand Down
23 changes: 19 additions & 4 deletions lib/payment-subscription/payment-multi-validator/validation.ak
Original file line number Diff line number Diff line change
Expand Up @@ -180,21 +180,29 @@ pub fn validate_extend_subscription(
when datum is {
Payment(payment_datum) -> {
// Find the input being spent (current Payment Validator UTxO)
trace @"183"
expect [account_input, payment_input, ..] = tx.inputs
// Find the service reference input by searching for the token policy
trace @"185"
expect Some(service_ref_input) =
get_input_by_token(tx.reference_inputs, service_policy_id)
// Find the new payment output
trace @"186"
expect [_account_output, payment_output, ..] = tx.outputs

// The script credential is part of the address of the input being spent
let payment_script_credential =
payment_input.output.address.payment_credential
// Extract the datums
trace @"197"
expect service_datum: ServiceDatum =
data.output_datum(service_ref_input.output)
expect new_datum: PaymentDatum = data.output_datum(payment_output)

trace @"197"
expect Payment(new_datum): PaymentValidatorDatum =
data.output_datum(payment_output)

// expect new_datum: PaymentDatum = data.output_datum(payment_output)
let payment_addr = payment_input.output.address

// Extract the payment NFT from the input
Expand Down Expand Up @@ -332,9 +340,12 @@ pub fn validate_unsubscribe(
validate_payment_against_service(payment_datum, service_datum)

// Verify Penalty UTxO
expect penalty: PenaltyDatum = data.output_datum(penalty_output)
// expect penalty: PenaltyDatum = data.output_datum(penalty_output)
expect Penalty(penalty_datum): PaymentValidatorDatum =
data.output_datum(penalty_output)

let penalty_output_valid =
penalty.service_nft_tn == payment_datum.service_nft_tn && penalty.account_nft_tn == payment_datum.account_nft_tn && payment_datum.penalty_fee_qty == penalty.penalty_fee_qty && penalty.penalty_fee_qty == service_datum.penalty_fee_qty
penalty_datum.service_nft_tn == payment_datum.service_nft_tn && penalty_datum.account_nft_tn == payment_datum.account_nft_tn && payment_datum.penalty_fee_qty == penalty_datum.penalty_fee_qty && penalty_datum.penalty_fee_qty == service_datum.penalty_fee_qty

// Ensure token output goes back to the script
let payment_token_to_script =
Expand Down Expand Up @@ -394,7 +405,10 @@ pub fn validate_merchant_withdraw(
let current_time = get_minimum_current_time_estimate(tx.validity_range)

// Update the last_claimed field in the new datum
expect new_datum: PaymentDatum = data.output_datum(payment_output)
// expect new_datum: PaymentDatum = data.output_datum(payment_output)
expect Payment(new_datum): PaymentValidatorDatum =
data.output_datum(payment_output)

let last_claimed_updated = new_datum.last_claimed == current_time

// Verify that the Service NFT is preserved
Expand All @@ -413,6 +427,7 @@ pub fn validate_merchant_withdraw(
payment_datum.interval_length,
payment_datum.interval_amount,
)
// TODO: Add to the funciton + payment_datum.minimum_ada
// Verify that any remaining funds are returned to the payment validator
// This is the amount of funds that should be left in the payment script after the withdrawal.
let actual_remaining_amount =
Expand Down
10 changes: 4 additions & 6 deletions lib/payment-subscription/service-multi-validator/validation.ak
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,12 @@ pub fn validate_remove_service(
ref_nft: Value,
tx: Transaction,
) -> Bool {
let validating_output =
common_utils.output_by_addr_value(tx.outputs, service_addr, ref_nft)
// Validate that there are two script inputs
expect [user_input, service_input, ..] = tx.inputs
expect [(policy_id, ref_asset_name, _)] = value.flatten(ref_nft)

// Validate that there are two script inputs
expect [user_input, service_input] = tx.inputs
let validating_output =
common_utils.output_by_addr_value(tx.outputs, service_addr, ref_nft)

let user_nft =
user_input.output.value
Expand All @@ -148,8 +148,6 @@ pub fn validate_remove_service(
(value.quantity_of(user_input.output.value, upolicy_id, usr_asset_name) == 1)?,
// Check that service input has a service nft,
(value.quantity_of(service_input.output.value, policy_id, ref_asset_name) == 1)?,
// // Validate that two Script output Leaving the script address.
// count.outputs_by_addr(tx.outputs, service_addr, 2)?,
// Check that the reference NFT is burned
service_is_inactive?,
other_fields_unchanged?,
Expand Down
27 changes: 18 additions & 9 deletions lib/payment-subscription/tests/payment-multi-validator.ak
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use aiken/transaction.{
use aiken/transaction/value
use assist/types/cip68
use assist/values
use payment_subscription/common/types.{AssetClass, Payment, PenaltyDatum}
use payment_subscription/common/types.{
AssetClass, Payment, PaymentDatum, Penalty, PenaltyDatum,
}
use payment_subscription/payment_multi_validator/validation.{
validate_extend_subscription, validate_initial_subscription,
validate_merchant_withdraw, validate_subscriber_withdraw,
Expand Down Expand Up @@ -406,8 +408,9 @@ test succeed_extend_subscription() {
extend_output_ref,
own_cs,
initial_payment_value,
initial_payment_datum,
Payment(initial_payment_datum),
)

// Define extension parameters
// 30 more days
let extension_period = interval_length
Expand All @@ -416,7 +419,7 @@ test succeed_extend_subscription() {

// Same as initial fee for simplicity
// Create the new payment datum with extended period and increased fee
let new_payment_datum =
let new_payment_datum: PaymentDatum =
tests.test_datum_03(
service_tkn,
user_account_tkn,
Expand All @@ -429,6 +432,7 @@ test succeed_extend_subscription() {
last_claimed,
penalty_fee_qty,
)

// Create the new payment output
let new_payment_value =
value.add(
Expand All @@ -442,7 +446,7 @@ test succeed_extend_subscription() {
own_cs,
new_payment_value,
100_000_000,
new_payment_datum,
Payment(new_payment_datum),
)
// Create a mock subscriber input (to pay for the extension)
let subscriber_pkh = tests.test_224_04()
Expand Down Expand Up @@ -539,7 +543,7 @@ test succeed_extend_subscription() {
trace @"Original Payment Datum:"
trace cbor.diagnostic(initial_payment_datum)
trace @"Updated Payment Datum:"
trace cbor.diagnostic(new_payment_datum)
trace cbor.diagnostic(Payment(new_payment_datum))

trace @"Step 4: Verifying Transaction"
trace @"-------------------------------------"
Expand Down Expand Up @@ -625,7 +629,7 @@ test succeed_unsubscribe() {
script_output_ref,
own_cs,
payment_value,
payment_datum,
Payment(payment_datum),
)

// Create the account input
Expand Down Expand Up @@ -664,7 +668,12 @@ test succeed_unsubscribe() {
let penalty_datum =
tests.test_penalty_datum(service_tkn, user_account_tkn, penalty_fee_qty)
let penalty_output =
tests.test_utxo_02(own_cs, penalty_value, 100_000_000, penalty_datum)
tests.test_utxo_02(
own_cs,
penalty_value,
100_000_000,
Penalty(penalty_datum),
)

let lower_bound = current_time + time_elapsed
let upper_bound = lower_bound + 1
Expand Down Expand Up @@ -820,7 +829,7 @@ test succeed_merchant_withdraw() {
script_output_ref,
own_cs,
payment_value,
payment_datum,
Payment(payment_datum),
)

// Create the service input (to prove service ownership)
Expand Down Expand Up @@ -878,7 +887,7 @@ test succeed_merchant_withdraw() {
own_cs,
payment_output_value,
100_000_000,
new_payment_datum,
Payment(new_payment_datum),
)

let lower_bound = current_time
Expand Down
15 changes: 13 additions & 2 deletions lib/payment-subscription/tests/service-multi-validator.ak
Original file line number Diff line number Diff line change
Expand Up @@ -235,14 +235,25 @@ test success_remove_service() {
let minted_value = value.merge(ref_value, user_value)

let user_input =
tests.test_context_input_02(init_output_ref, own_cs, user_value, NoDatum)
tests.test_context_input_02(
init_output_ref,
tests.test_wallet_addr(),
user_value,
NoDatum,
)
let service_input =
tests.test_context_input_02(script_output_ref, own_cs, ref_value, datum)

let new_datum =
tests.test_service_datum(False, subscription_period, num_intervals)

let user_output = tests.test_utxo_02(own_cs, user_value, 100_000_000, NoDatum)
let user_output =
tests.test_utxo_02(
tests.test_wallet_addr(),
user_value,
100_000_000,
NoDatum,
)
let service_output =
tests.test_utxo_02(own_cs, ref_value, 100_000_000, new_datum)

Expand Down
4 changes: 4 additions & 0 deletions lib/payment-subscription/tests/tests.ak
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ pub fn test_payment_cs() {
bytearray.take(blake2b_256(#"07"), 28)
}

pub fn test_wallet_addr() {
bytearray.take(blake2b_256(#"08"), 28)
}

// Create a UTxO
pub fn test_utxo_01() {
Output {
Expand Down
Loading

0 comments on commit 456b9f6

Please sign in to comment.