Skip to content

Commit

Permalink
Added liquidity injection to Vault
Browse files Browse the repository at this point in the history
  • Loading branch information
glottologist committed Oct 17, 2023
1 parent 2f6ea0c commit 97dc418
Show file tree
Hide file tree
Showing 10 changed files with 688 additions and 358 deletions.
70 changes: 24 additions & 46 deletions batcher/batcher.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -818,16 +818,6 @@ type entrypoint =
| Remove_metadata of string
| Change_deposit_time_window of nat


[@inline]
let get_oracle_price
(failure_code: nat)
(valid_swap: valid_swap_reduced) : orace_price_update =
match Tezos.call_view "getPrice" valid_swap.oracle_asset_name valid_swap.oracle_address with
| Some opu -> opu
| None -> failwith failure_code


[@inline]
let admin_and_fee_recipient_address_are_different
(admin : address)
Expand Down Expand Up @@ -933,7 +923,7 @@ let refund_orders
let mutez_to_ref = mutez_to_ref + fee in
tam, mutez_to_ref
in
let token_refunds, tez_refunds= Map.fold collect_refunds ots ((Map.empty: token_amount_map), 0mutez) in
let token_refunds, tez_refunds = Map.fold collect_refunds ots ((Map.empty: token_amount_map), 0mutez) in
let treasury_vault = get_vault () in
let operations = Treasury_Utils.transfer_holdings treasury_vault refund_address token_refunds in
let operations = if tez_refunds > 0mutez then Treasury_Utils.transfer_fee storage.fee_recipient tez_refunds :: operations else operations in
Expand Down Expand Up @@ -970,24 +960,6 @@ let cancel
let vswp = valid_swap_reduced_to_valid_swap vswpr 1n valid_tokens in
cancel_order pair sender vswp storage

[@inline]
let oracle_price_is_not_stale
(deposit_time_window: nat)
(oracle_price_timestamp: timestamp) : unit =
let dtw_i = int deposit_time_window in
if (Tezos.get_now () - dtw_i) < oracle_price_timestamp then () else failwith oracle_price_is_stale

[@inline]
let is_oracle_price_newer_than_current
(rate_name: string)
(oracle_price_timestamp: timestamp)
(storage: storage): unit =
let rates = storage.rates_current in
match Big_map.find_opt rate_name rates with
| Some r -> if r.when >=oracle_price_timestamp then failwith oracle_price_is_not_timely
| None -> ()


[@inline]
let confirm_oracle_price_is_available_before_deposit
(pair:pair)
Expand All @@ -996,15 +968,14 @@ let confirm_oracle_price_is_available_before_deposit
if Batch_Utils.is_batch_open batch then () else
let pair_name = get_rate_name_from_pair pair in
let valid_swap_reduced = get_valid_swap_reduced pair_name storage in
let (lastupdated, _price) = get_oracle_price oracle_price_should_be_available_before_deposit valid_swap_reduced in
oracle_price_is_not_stale storage.deposit_time_window_in_seconds lastupdated
let (lastupdated, _price) = OracleUtils.get_oracle_price oracle_price_should_be_available_before_deposit valid_swap_reduced in
OracleUtils.oracle_price_is_not_stale storage.deposit_time_window_in_seconds lastupdated

[@inline]
let confirm_swap_pair_is_disabled_prior_to_removal
(valid_swap:valid_swap) : unit =
if valid_swap.is_disabled_for_deposits then () else failwith cannot_remove_swap_pair_that_is_not_disabled


[@inline]
let enforce_correct_side
(order:external_swap_order)
Expand Down Expand Up @@ -1073,11 +1044,11 @@ let tick_price
(valid_swap : valid_swap)
(storage : storage) : storage =
let valid_swap_reduced = valid_swap_to_valid_swap_reduced valid_swap in
let (lastupdated, price) = get_oracle_price unable_to_get_price_from_oracle valid_swap_reduced in
let () = is_oracle_price_newer_than_current rate_name lastupdated storage in
let () = oracle_price_is_not_stale storage.deposit_time_window_in_seconds lastupdated in
let (lastupdated, price) = OracleUtils.get_oracle_price unable_to_get_price_from_oracle valid_swap_reduced in
let () = OracleUtils.is_oracle_price_newer_than_current rate_name lastupdated storage.rates_current in
let () = OracleUtils.oracle_price_is_not_stale storage.deposit_time_window_in_seconds lastupdated in
let valid_tokens = TokenManagerUtils.get_valid_tokens storage.tokenmanager in
let oracle_rate = convert_oracle_price valid_swap.oracle_precision valid_swap.swap lastupdated price valid_tokens in
let oracle_rate = OracleUtils.convert_oracle_price valid_swap.oracle_precision valid_swap.swap lastupdated price valid_tokens in
let rates_current = update_current_rate (rate_name) (oracle_rate) (storage.rates_current) in
let storage = { storage with rates_current = rates_current; } in
let pair = pair_of_rate oracle_rate in
Expand Down Expand Up @@ -1108,7 +1079,7 @@ let tick
let change_fee
(new_fee: tez)
(storage: storage) : result =
let () = is_administrator storage.administrator in
let () = is_known_sender storage.administrator sender_not_administrator in
let () = reject_if_tez_supplied () in
let storage = { storage with fee_in_mutez = new_fee; } in
no_op storage
Expand All @@ -1117,7 +1088,7 @@ let change_fee
let change_admin_address
(new_admin_address: address)
(storage: storage) : result =
let () = is_administrator storage.administrator in
let () = is_known_sender storage.administrator sender_not_administrator in
let () = reject_if_tez_supplied () in
let () = admin_and_fee_recipient_address_are_different new_admin_address storage.fee_recipient in
let storage = { storage with administrator = new_admin_address; } in
Expand All @@ -1127,7 +1098,7 @@ let change_admin_address
let change_mm_address
(new_marketmaker_address: address)
(storage: storage) : result =
let () = is_administrator storage.administrator in
let () = is_known_sender storage.administrator sender_not_administrator in
let () = reject_if_tez_supplied () in
let storage = { storage with marketmaker = new_marketmaker_address; } in
no_op storage
Expand All @@ -1136,7 +1107,7 @@ let change_mm_address
let change_tm_address
(new_tokenmanager_address: address)
(storage: storage) : result =
let () = is_administrator storage.administrator in
let () = is_known_sender storage.administrator sender_not_administrator in
let () = reject_if_tez_supplied () in
let storage = { storage with tokenmanager = new_tokenmanager_address; } in
no_op storage
Expand All @@ -1145,7 +1116,7 @@ let change_tm_address
let change_fee_recipient_address
(new_fee_recipient_address: address)
(storage: storage) : result =
let () = is_administrator storage.administrator in
let () = is_known_sender storage.administrator sender_not_administrator in
let () = reject_if_tez_supplied () in
let () = admin_and_fee_recipient_address_are_different new_fee_recipient_address storage.administrator in
let storage = { storage with fee_recipient = new_fee_recipient_address; } in
Expand All @@ -1156,7 +1127,7 @@ let change_fee_recipient_address
let add_or_update_metadata
(metadata_update: metadata_update)
(storage:storage) : result =
let () = is_administrator storage.administrator in
let () = is_known_sender storage.administrator sender_not_administrator in
let () = reject_if_tez_supplied () in
let updated_metadata = match Big_map.find_opt metadata_update.key storage.metadata with
| None -> Big_map.add metadata_update.key metadata_update.value storage.metadata
Expand All @@ -1169,7 +1140,7 @@ let add_or_update_metadata
let remove_metadata
(key: string)
(storage:storage) : result =
let () = is_administrator storage.administrator in
let () = is_known_sender storage.administrator sender_not_administrator in
let () = reject_if_tez_supplied () in
let updated_metadata = Big_map.remove key storage.metadata in
let storage = {storage with metadata = updated_metadata } in
Expand All @@ -1180,7 +1151,7 @@ let remove_metadata
let change_deposit_time_window
(new_window: nat)
(storage: storage) : result =
let () = is_administrator storage.administrator in
let () = is_known_sender storage.administrator sender_not_administrator in
let () = reject_if_tez_supplied () in
if new_window < minimum_deposit_time_in_seconds then failwith cannot_update_deposit_window_to_less_than_the_minimum else
if new_window > maximum_deposit_time_in_seconds then failwith cannot_update_deposit_window_to_more_than_the_maximum else
Expand All @@ -1202,12 +1173,19 @@ let redeemable_holdings_available ((), storage : unit * storage) : bool =
| None -> false
| Some bots -> Map.fold find_non_zero_holding bots false

(* TODO put liq clause in *)
[@inline]
let does_batch_need_liquidity
(batch: batch): batch option = Some batch

[@view]
let get_current_batches ((),storage: unit * storage) : batch list=
let get_batches_needing_liquidity ((),storage: unit * storage) : batch list=
let collect_batches (acc, (_s, i) : batch list * (string * nat)) : batch list =
match Big_map.find_opt i storage.batch_set.batches with
| None -> acc
| Some b -> b :: acc
| Some b -> (match does_batch_need_liquidity b with
| None -> acc
| Some bl -> bl :: acc)
in
Map.fold collect_batches storage.batch_set.current_batch_indices []

Expand Down
3 changes: 3 additions & 0 deletions batcher/errors.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,6 @@
[@inline] let unable_to_get_native_token_from_vault = 164n
[@inline] let unable_to_get_swaps_from_token_manager = 165n
[@inline] let unable_to_get_vaults_from_marketmaker = 166n
[@inline] let unable_to_get_current_batches_from_batcher = 167n
[@inline] let sender_not_marketmaker : nat = 168n

65 changes: 63 additions & 2 deletions batcher/marketmaker.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ let add_vault
(vault_name: string)
(vault_address: address)
(storage: storage) : operation list * storage =
let () = is_administrator storage.administrator in
let () = is_known_sender storage.administrator sender_not_administrator in
let () = reject_if_tez_supplied () in
let nt = get_native_token_from_vault vault_address in
if not (nt.name = vault_name) then failwith vault_name_is_incorrect else
Expand All @@ -66,7 +66,7 @@ let add_vault
let remove_vault
(vault_name: string)
(storage: storage) : operation list * storage =
let () = is_administrator storage.administrator in
let () = is_known_sender storage.administrator sender_not_administrator in
let () = reject_if_tez_supplied () in
match Vaults.find_opt vault_name storage.vaults with
| None -> failwith vault_name_is_incorrect
Expand All @@ -76,9 +76,70 @@ let remove_vault
let storage = { storage with vaults = vaults; } in
no_op storage




[@inline]
let make_liquidity_request
(liq_token: string)
(opposing_token: string)
(opposing_volume: nat)
(ops: operation list)
(valid_tokens:ValidTokens.t_map)
(valid_swaps:ValidSwaps.t_map)
(vaults: Vaults.t) : operation list
let liq_token_opt = Map.find_opt liq_token valid_tokens in
let opposing_token_opt = Map.find_opt sell_token valid_tokens in
match (liq_token_opt,opposing_token_opt) with
| Some lt, Some ot -> (let pair_name = find_lexicographical_pair_name lt.name ot.name in
match Map.find_opt pair_name valid_swaps with
| None -> failwith swap_does_not_exist
| Some vs -> let (lastupdated, price) = OracleUtils.get_oracle_price unable_to_get_price_from_oracle vs in
let oracle_rate = OracleUtils.convert_oracle_price valid_swap.oracle_precision valid_swap.swap lastupdated price valid_tokens in
let rat_opposing_volume = Rational.new (int opposing_volume) in
if lt.token.name = valid_swap.swap.to then
let rat_vol_required = Rational.div rat_opposing_volume oracle_rate.rate in
let vol_required = get_rounded_number_lower_bound rat_vol_required in

else



(* Create a Rational version of the native token in the opposing vault i.e a rational version of the tzBTC held in the USDT vault *)
let rat_opposing_vault_native_amount = Rational.new (int opposing_vault_native_token_amount.amount) in
(* Use the rate to create a foreign equivalent of the native token in the opposing vault this is the tzBTC held in the USDT vault but converted into its USDT value for comparison *)
let opposite_native_equivalent = Rational.mul rate.rate rat_opposing_vault_native_amount in
(* Create a Rational version of the foreign token amount in the native vault i.e a rational version of the USDT held in the tzBTC vault *)
let rat_foreign_token_amount = Rational.new (int foreign_token_amount.amount) in
(* We are comparing USDT equivalent of the tzBTC in the USDT vault with the USDT held in the tzBTC vault as a foreign token *)
let rat_foreign_amount_to_move = if opposite_native_equivalent > rat_foreign_token_amount then rat_foreign_token_amount else opposite_native_equivalent in
let rat_native_amount_to_move = Rational.div rat_foreign_amount_to_move rate.rate in
let int_native_amount_to_move = Utils.get_rounded_number_lower_bound rat_native_amount_to_move in
let int_foreign_amount_to_move = Utils.get_rounded_number_lower_bound rat_foreign_amount_to_move in
let native_amount_to_move = { native_token_amount with amount = int_native_amount_to_move; } in
let foreign_amount_to_move = { foreign_token_amount with amount = int_foreign_amount_to_move; } in
exchange_amount native_amount_to_move foreign_amount_to_move native_token_amount foreign_token_amount opposing_vault_foreign_token_amount opposing_vault_native_token_amount


| _, _ -> failwith token_name_not_in_list_of_valid_tokens

[@inline]
let tick
(storage: storage) : result =
let batches_needing_liquidity = BatcherUtils.get_batches_needing_liquidity storage.batcher in
let vaults = storage.vaults in
let valid_tokens = TokenManagerUtils.get_valid_tokens storage.tokenmanager in
let valid_swaps = TokenManagerUtils.get_valid_swaps storage.tokenmanager in
let request_liquidity (liq_ops,batch):(operation list * batch) : operation list =
let (bt,st) = batch.pair in
let buy_vol_opt = if batch.volumes.sell_total_volume = 0n then None else Some batch.volumes.sell_total_volume in
let sell_vol_opt = if batch.volumes.buy_total_volume = 0n then None else Some batch.volumes.buy_total_volume in
match (buy_vol_opt, sell_vol_opt) with
| Some _,Some _ -> liq_ops
| Some bv, None -> make_liquidity_request st bt bv liq_ops vaults
| None, Some sv -> make_liquidity_request bt st sv liq_ops vaults
| None, None -> liq_ops

no_op storage

end
Expand Down
Loading

0 comments on commit 97dc418

Please sign in to comment.