Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add more safety check on Factory Validator and SwapRouting Redeemer of Pool Validation, add AMM V2 documentation and add code comments #14

Merged
merged 1 commit into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
340 changes: 340 additions & 0 deletions amm-v2-docs/amm-v2-specs.md

Large diffs are not rendered by default.

41 changes: 21 additions & 20 deletions lib/amm_dex_v2/order_validation.ak
Original file line number Diff line number Diff line change
Expand Up @@ -677,9 +677,9 @@ pub fn validate_swap_multi_routing_order(
batcher_fee: Int,
output_ada: Int,
) -> Bool {
let first_routing = utils.list_at_index(routings, 0)
let first_routing = routings |> builtin.head_list
let last_routing = utils.list_at_index(routings, list.length(routings) - 1)
let first_pool = utils.list_at_index(pools, 0)
let first_pool = pools |> builtin.head_list
let last_pool = utils.list_at_index(pools, list.length(pools) - 1)
let SwapRouting { direction: first_routing_direction, .. } = first_routing
let SwapRouting { direction: last_routing_direction, .. } = last_routing
Expand Down Expand Up @@ -723,23 +723,14 @@ pub fn validate_swap_multi_routing_order(
all_pools: pools,
all_routings: routings,
)
expect amount_out >= minimum_receive
let actual_amount_out =
value.quantity_of(
order_out_value,
asset_out_policy_id,
asset_out_asset_name,
)
let is_valid_amount_out =
if utils.is_ada_asset(asset_out_policy_id, asset_out_asset_name) {
amount_out + output_ada == actual_amount_out
} else {
let ada_amount =
value.quantity_of(order_out_value, ada_policy_id, ada_asset_name)
amount_out == actual_amount_out && ada_amount == output_ada
}
expect is_valid_amount_out
True
let expect_order_value_out =
value.zero()
|> value.add(ada_policy_id, ada_asset_name, output_ada)
|> value.add(asset_out_policy_id, asset_out_asset_name, amount_out)
and {
amount_out >= minimum_receive,
expect_order_value_out == order_out_value,
}
}

pub fn validate_order_receiver(
Expand All @@ -766,7 +757,6 @@ pub fn validate_order_receiver(
}
}

// TODO: validate order input value size
pub fn apply_orders(
datum_map: DatumMap,
asset_a: Asset,
Expand Down Expand Up @@ -810,15 +800,18 @@ pub fn apply_orders(
lp_asset: order_lp_asset,
} = order_in_datum
expect and {
// batcher_fee and output_ada must be positive
batcher_fee > 0,
output_ada > 0,
// lp_asset must be the same with processing Liquidity Pool
lp_asset == order_lp_asset,
}
let new_state =
when order_step is {
SwapExactIn(direction, minimum_receive) -> {
expect and {
minimum_receive > 0,
// Order Output must be returned to receiver and might have receiver_datum_hash_opt
validate_order_receiver(
receiver: receiver,
receiver_datum_hash_opt: receiver_datum_hash_opt,
Expand Down Expand Up @@ -850,6 +843,7 @@ pub fn apply_orders(
StopLoss(direction, stop_loss_receive) -> {
expect and {
stop_loss_receive > 0,
// Order Output must be returned to receiver and might have receiver_datum_hash_opt
validate_order_receiver(
receiver: receiver,
receiver_datum_hash_opt: receiver_datum_hash_opt,
Expand Down Expand Up @@ -882,6 +876,7 @@ pub fn apply_orders(
expect and {
minimum_receive > 0,
stop_loss_receive > 0,
// Order Output must be returned to receiver and might have receiver_datum_hash_opt
validate_order_receiver(
receiver: receiver,
receiver_datum_hash_opt: receiver_datum_hash_opt,
Expand Down Expand Up @@ -913,6 +908,7 @@ pub fn apply_orders(
SwapExactOut(direction, expected_receive) -> {
expect and {
expected_receive > 0,
// Order Output must be returned to receiver and might have receiver_datum_hash_opt
validate_order_receiver(
receiver: receiver,
receiver_datum_hash_opt: receiver_datum_hash_opt,
Expand Down Expand Up @@ -942,6 +938,7 @@ pub fn apply_orders(
Deposit(minimum_lp) -> {
expect and {
minimum_lp > 0,
// Order Output must be returned to receiver and might have receiver_datum_hash_opt
validate_order_receiver(
receiver: receiver,
receiver_datum_hash_opt: receiver_datum_hash_opt,
Expand All @@ -967,6 +964,7 @@ pub fn apply_orders(
expect and {
minimum_asset_a > 0,
minimum_asset_b > 0,
// Order Output must be returned to receiver and might have receiver_datum_hash_opt
validate_order_receiver(
receiver: receiver,
receiver_datum_hash_opt: receiver_datum_hash_opt,
Expand All @@ -988,6 +986,7 @@ pub fn apply_orders(
ZapOut(direction, minimum_receive) -> {
expect and {
minimum_receive > 0,
// Order Output must be returned to receiver and might have receiver_datum_hash_opt
validate_order_receiver(
receiver: receiver,
receiver_datum_hash_opt: receiver_datum_hash_opt,
Expand Down Expand Up @@ -1103,6 +1102,7 @@ pub fn apply_orders(
minimum_swap_amount_required == order_out_minimum_swap_amount_required,
}
} else {
// Order Output must be returned to receiver and might have receiver_datum_hash_opt
validate_order_receiver(
receiver: receiver,
receiver_datum_hash_opt: receiver_datum_hash_opt,
Expand All @@ -1122,6 +1122,7 @@ pub fn apply_orders(
ratio_asset_a > 0,
ratio_asset_b > 0,
minimum_asset_a > 0,
// Order Output must be returned to receiver and might have receiver_datum_hash_opt
validate_order_receiver(
receiver: receiver,
receiver_datum_hash_opt: receiver_datum_hash_opt,
Expand Down
Loading