Skip to content

Commit

Permalink
Added cancellation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
glottologist committed Aug 4, 2023
1 parent 5c916f7 commit 93857ab
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 3 deletions.
3 changes: 2 additions & 1 deletion batcher/batcher.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -1800,7 +1800,8 @@ let remove_orders_from_batch
(ots: ordertypes)
(batch: batch): batch =
let volumes = Batch_Utils.reduce_volumes ots batch.volumes in
{ batch with volumes = volumes}
let holdings = abs (batch.holdings - 1n) in
{ batch with volumes = volumes; holdings= holdings; }

[@inline]
let remove_orders
Expand Down
8 changes: 8 additions & 0 deletions batcher/test/common/helpers.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,11 @@ let get_source_update
oracle_precision = valid_swap.oracle_precision;
}


let get_current_batch
(pair: string)
(storage: Batcher.Storage.t): Batcher.batch option =
let batch_set = storage.batch_set in
match Map.find_opt pair batch_set.current_batch_indices with
| Some i -> Big_map.find_opt i batch_set.batches
| None -> None
77 changes: 77 additions & 0 deletions batcher/test/endpoints/user/test_cancellations.mligo
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#import "ligo-breathalyzer/lib/lib.mligo" "Breath"
#import "./../../common/helpers.mligo" "Helpers"
#import "./../../common/batch.mligo" "Batch"
#import "../../../batcher.mligo" "Batcher"

type skew = Batch.skew
type pressure = Batch.pressure

let cancellation_fail_if_batch_is_closed =
Breath.Model.case
"test cancellation"
"should fail if batch is closed"
(fun (level: Breath.Logger.level) ->
let pair = ("tzBTC","USDT") in
let tick_pair = "tzBTC/USDT" in
let (_expected_tolerance, batch) = Batch.prepare_batch pair Buy Balanced in
let context = Helpers.test_context_with_batch tick_pair batch None level in
let batcher = context.contracts.batcher in
let btc_trader = context.btc_trader in

let act_cancel = Breath.Context.act_as btc_trader (fun (_u:unit) -> (Breath.Contract.transfer_to batcher (Cancel pair) 0tez)) in

Breath.Result.reduce [
Breath.Expect.fail_with_value Batcher.cannot_cancel_orders_for_a_batch_that_is_not_open act_cancel
])

let cancellation_should_succeed =
Breath.Model.case
"test cancellation"
"should be successful"
(fun (level: Breath.Logger.level) ->
let pair = ("tzBTC","USDT") in
let tick_pair = "tzBTC/USDT" in
let context = Helpers.test_context level in
let batcher = context.contracts.batcher in
let btc_trader = context.btc_trader in

let bstorage = Breath.Contract.storage_of batcher in

let deposit_amount = 2000000n in
let allowance = {
spender = batcher.originated_address;
value = deposit_amount
} in
let act_allow_transfer = Breath.Context.act_as btc_trader (fun (_u:unit) -> (Breath.Contract.transfer_to context.contracts.tzbtc (Approve allowance) 0tez)) in
let act_deposit = Helpers.place_order btc_trader batcher bstorage.fee_in_mutez "tzBTC" "USDT" deposit_amount Buy Exact bstorage.valid_tokens in

let bstorage_after_desposit = Breath.Contract.storage_of batcher in
let batch_after_deposit = Option.unopt (Helpers.get_current_batch tick_pair bstorage_after_desposit) in
let total_volumes_after_deposit = batch_after_deposit.volumes.buy_total_volume + batch_after_deposit.volumes.sell_total_volume in
let holdings_after_deposit = batch_after_deposit.holdings in

let act_cancel = Breath.Context.act_as btc_trader (fun (_u:unit) -> (Breath.Contract.transfer_to batcher (Cancel pair) 0tez)) in

let bstorage_after_cancellation = Breath.Contract.storage_of batcher in
let batch_after_cancellation = Option.unopt (Helpers.get_current_batch tick_pair bstorage_after_cancellation) in
let total_volumes_after_cancellation = batch_after_cancellation.volumes.buy_total_volume + batch_after_deposit.volumes.sell_total_volume in
let holdings_after_cancellation = batch_after_cancellation.holdings in

Breath.Result.reduce [
act_allow_transfer
; act_deposit
; Helpers.expect_last_order_number bstorage_after_desposit 1n
; Breath.Assert.is_equal "holdings after deposit" holdings_after_deposit 1n
; Breath.Assert.is_equal "total volumes after deposit" total_volumes_after_deposit 2000000n
; act_cancel
; Breath.Assert.is_equal "holdings after cancellation" holdings_after_cancellation 0n
; Breath.Assert.is_equal "total volumes after cancellation" total_volumes_after_cancellation 0n
; Helpers.expect_last_order_number bstorage_after_cancellation 1n (* We do not decrement the last order number on cancellations *)
])

let test_suite =
Breath.Model.suite "Suite for Cancellations" [
cancellation_fail_if_batch_is_closed
; cancellation_should_succeed
]

6 changes: 4 additions & 2 deletions batcher/test/test.mligo
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#import "ligo-breathalyzer/lib/lib.mligo" "Breath"
#import "./common/helpers.mligo" "Helpers"
#import "./endpoints/user/test_deposits.mligo" "User_Deposits"
#import "./endpoints/user/test_cancellations.mligo" "User_Cancellations"
#import "./endpoints/user/test_redemptions.mligo" "User_Redemptions"
#import "./economics/test_clearing.mligo" "Economics_Clearing"
#import "./endpoints/admin/test_change_fee.mligo" "Admin_Change_Fee"
Expand Down Expand Up @@ -42,7 +43,7 @@ let () =
Breath.Model.run_suites Void
[
test_suite
; Admin_Change_Fee.test_suite
(* ; Admin_Change_Fee.test_suite
; Admin_Change_Admin_Address.test_suite
; Admin_Change_Fee_Recipient_Address.test_suite
; Admin_Change_Deposit_Time_Window.test_suite
Expand All @@ -52,8 +53,9 @@ let () =
; Admin_Change_Oracle_Source_Of_Pair.test_suite
; Admin_Add_Remove_Token_Swap_Pair.test_suite
; Maintenance_Tick.test_suite
; Economics_Clearing.test_suite
; Economics_Clearing.test_suite *)
; User_Deposits.test_suite
; User_Cancellations.test_suite
// ; User_Redemptions.test_suite
]

0 comments on commit 93857ab

Please sign in to comment.