Skip to content

Commit

Permalink
optimize collect order
Browse files Browse the repository at this point in the history
  • Loading branch information
m1n999999 committed Jun 19, 2024
1 parent 7099161 commit c111ba4
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 42 deletions.
26 changes: 13 additions & 13 deletions lib/lb_v2/order_validation.ak
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
use aiken/list
use aiken/transaction.{InlineDatum, Input, Output}
use aiken/transaction/value.{
AssetName, PolicyId, Value, ada_asset_name, ada_policy_id,
}
use aiken/transaction/value.{AssetName, Value, ada_asset_name, ada_policy_id}
use lb_v2/types.{Asset, OrderDatum, TreasuryDatum, ValidatorHash}
use lb_v2/utils.{
amm_authen_policy_id, compute_asset_name_from_base_and_raise, fee_ada,
amm_authen_policy_id, compute_asset_name_from_base_and_raise,
minimum_order_collected, minimum_order_redeemed, order_auth_an,
order_minimum_ada,
}
use lb_v2/validation.{get_order_inputs, get_order_outputs, get_treasury_output}
use lb_v2/validation.{
build_default_collected_order_value, get_order_inputs, get_order_outputs,
get_treasury_output,
}

// Assertions:
// - All Sellers, Manager have been collected.
Expand Down Expand Up @@ -49,7 +50,7 @@ pub fn validate_collect_orders(
factory_policy_id: factory_policy_id,
order_hash: order_hash,
)
let order_outputs = get_order_outputs(outputs, factory_policy_id, order_hash)
let order_outputs = get_order_outputs(outputs, order_hash)
let treasury_output =
get_treasury_output(outputs, factory_policy_id, treasury_hash)
expect Output {
Expand All @@ -64,8 +65,10 @@ pub fn validate_collect_orders(
order_outputs: order_outputs,
base_asset: base_asset,
raise_asset: raise_asset,
factory_policy_id: factory_policy_id,
acc_collect_amount: 0,
default_order_output_value: build_default_collected_order_value(
factory_policy_id,
),
)
let Asset { policy_id: raise_asset_pid, asset_name: raise_asset_an } =
raise_asset
Expand Down Expand Up @@ -101,8 +104,8 @@ pub fn apply_collecting_orders(
order_outputs: List<Output>,
base_asset: Asset,
raise_asset: Asset,
factory_policy_id: PolicyId,
acc_collect_amount: Int,
default_order_output_value: Value,
) -> Int {
expect [input, ..inputs] = order_inputs
expect [output, ..outputs] = order_outputs
Expand All @@ -122,10 +125,7 @@ pub fn apply_collecting_orders(
// Out Datum only changes: {is_collected: True } compare to In Datum
out_datum == OrderDatum { ..in_datum, is_collected: True },
// Out Value should be correct!
out_value == (
value.from_asset(factory_policy_id, order_auth_an, 1)
|> value.add(ada_policy_id, ada_asset_name, order_minimum_ada + fee_ada)
),
out_value == default_order_output_value,
}
let OrderDatum { amount, penalty_amount, .. } = in_datum
let collect_amount = acc_collect_amount + amount + penalty_amount
Expand All @@ -141,8 +141,8 @@ pub fn apply_collecting_orders(
order_outputs: outputs,
base_asset: base_asset,
raise_asset: raise_asset,
factory_policy_id: factory_policy_id,
acc_collect_amount: collect_amount,
default_order_output_value: default_order_output_value,
)
}
}
Expand Down
17 changes: 13 additions & 4 deletions lib/lb_v2/test_suits.ak
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use lb_v2/utils.{
calculate_penalty, fee_ada, make_wrapper_redeemer, order_auth_an,
order_minimum_ada, sort_two_consecutive_factory_datum,
}
use lb_v2/validation.{build_default_collected_order_value}

type DummyObject {
owner: Address,
Expand Down Expand Up @@ -181,8 +182,10 @@ test test_apply_collecting_orders_1() fail {
order_outputs: [],
base_asset: dummy.base_asset,
raise_asset: dummy.raise_asset,
factory_policy_id: dummy.factory_policy_id,
acc_collect_amount: 0,
default_order_output_value: build_default_collected_order_value(
dummy.factory_policy_id,
),
) == 0
}

Expand All @@ -194,8 +197,10 @@ test test_apply_collecting_orders_2() fail {
order_outputs: [],
base_asset: dummy.base_asset,
raise_asset: dummy.raise_asset,
factory_policy_id: dummy.factory_policy_id,
acc_collect_amount: 0,
default_order_output_value: build_default_collected_order_value(
dummy.factory_policy_id,
),
) == 0
}

Expand All @@ -220,8 +225,10 @@ test test_apply_collecting_orders_3() {
order_outputs: [order_out],
base_asset: dummy.base_asset,
raise_asset: dummy.raise_asset,
factory_policy_id: dummy.factory_policy_id,
acc_collect_amount: 0,
default_order_output_value: build_default_collected_order_value(
dummy.factory_policy_id,
),
) == collect_amount
}

Expand All @@ -245,8 +252,10 @@ test test_apply_collecting_orders_4() {
order_outputs: [order_out, order_out, order_out],
base_asset: dummy.base_asset,
raise_asset: dummy.raise_asset,
factory_policy_id: dummy.factory_policy_id,
acc_collect_amount: 0,
default_order_output_value: build_default_collected_order_value(
dummy.factory_policy_id,
),
) == collect_amount * 3
}

Expand Down
16 changes: 8 additions & 8 deletions lib/lb_v2/validation.ak
Original file line number Diff line number Diff line change
Expand Up @@ -314,21 +314,18 @@ pub fn get_manager_output(

pub fn get_order_outputs(
outputs: List<Output>,
factory_policy_id: PolicyId,
order_hash: ValidatorHash,
) -> List<Output> {
list.filter(
outputs,
fn(output) {
let Output {
address: Address { payment_credential, .. },
value: out_value,
reference_script,
..
} = output
and {
payment_credential == ScriptCredential(order_hash),
value.quantity_of(out_value, factory_policy_id, order_auth_an) == 1,
reference_script == None,
}
},
Expand Down Expand Up @@ -502,11 +499,7 @@ pub fn validate_using_seller(
order_hash: order_hash,
)
let order_outputs =
get_order_outputs(
outputs: outputs,
factory_policy_id: factory_policy_id,
order_hash: order_hash,
)
get_order_outputs(outputs: outputs, order_hash: order_hash)
let (delta_amount, delta_penalty) =
apply_order(
order_inputs: order_inputs,
Expand Down Expand Up @@ -712,6 +705,13 @@ pub fn build_default_seller_output(
}
}

pub fn build_default_collected_order_value(
factory_policy_id: ValidatorHash,
) -> Value {
value.from_asset(factory_policy_id, order_auth_an, 1)
|> value.add(ada_policy_id, ada_asset_name, order_minimum_ada + fee_ada)
}

// Check if the owner authorizes the transaction
// If Owner is Script -> Transaction must contains at least 1 Script Input
// Else Transaction must be signed by Owner.
Expand Down
Loading

0 comments on commit c111ba4

Please sign in to comment.