Skip to content

Commit

Permalink
Added liquidity injection request into market maker
Browse files Browse the repository at this point in the history
  • Loading branch information
glottologist committed Oct 24, 2023
1 parent 5467257 commit 213dc1b
Show file tree
Hide file tree
Showing 11 changed files with 1,038 additions and 58 deletions.
101 changes: 60 additions & 41 deletions batcher/marketmaker.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ let no_op (s : storage) : result = (([] : operation list), s)
let change_admin_address
(new_admin_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 storage = { storage with administrator = new_admin_address; } in
no_op storage
Expand All @@ -32,7 +32,7 @@ let change_admin_address
let change_batcher_address
(new_batcher_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 storage = { storage with batcher = new_batcher_address; } in
no_op storage
Expand All @@ -42,7 +42,7 @@ let change_batcher_address
let change_tokenmanager_address
(new_tm_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 storage = { storage with tokenmanager = new_tm_address; } in
no_op storage
Expand Down Expand Up @@ -79,6 +79,50 @@ let remove_vault



[@inline]
let get_volume
(rate:exchange_rate)
(opposing_volume: nat) : nat =
let rat_opposing_volume = Rational.new (int opposing_volume) in
let rat_vol_required = Rational.mul rat_opposing_volume rate.rate in
get_rounded_number_lower_bound rat_vol_required

[@inline]
let get_inverse_volume
(rate:exchange_rate)
(opposing_volume: nat) : nat =
let rat_opposing_volume = Rational.new (int opposing_volume) in
let rat_vol_required = Rational.div rat_opposing_volume rate.rate in
get_rounded_number_lower_bound rat_vol_required

[@inline]
let execute_liquidity_request
(lt:token)
(ot:token)
(opposing_volume: nat)
(vault_address:address)
(valid_tokens:ValidTokens.t_map)
(valid_swaps:ValidSwaps.t_map) : operation =
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 swap:swap = swap_reduced_to_swap vs.swap 1n valid_tokens in
let oracle_rate = OracleUtils.convert_oracle_price vs.oracle_precision swap lastupdated price valid_tokens in
let (side,vol_req) = if lt.name = vs.swap.to then
(Sell,get_inverse_volume oracle_rate opposing_volume)
else
(Buy,get_volume oracle_rate opposing_volume)
in
let req = {
side = side;
from_token = lt;
to_token = ot;
amount = vol_req;
} in
send_liquidity_injection_request req vault_address


[@inline]
let make_liquidity_request
(liq_token: string)
Expand All @@ -87,41 +131,15 @@ let make_liquidity_request
(ops: operation list)
(valid_tokens:ValidTokens.t_map)
(valid_swaps:ValidSwaps.t_map)
(vaults: Vaults.t) : operation list
(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
let opposing_token_opt = Map.find_opt opposing_token valid_tokens in
if not (Vaults.mem liq_token vaults) then ops else
let vault_address = Option.unopt (Vaults.find_opt liq_token vaults) 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
| Some lt, Some ot -> execute_liquidity_request lt ot opposing_volume vault_address valid_tokens valid_swaps :: ops
| _,_ -> failwith token_name_not_in_list_of_valid_tokens


[@inline]
let tick
Expand All @@ -130,17 +148,18 @@ let tick
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 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
| Some bv, None -> make_liquidity_request st bt bv liq_ops valid_tokens valid_swaps vaults
| None, Some sv -> make_liquidity_request bt st sv liq_ops valid_tokens valid_swaps vaults
| None, None -> liq_ops

no_op storage
in
let ops = List.fold_left request_liquidity ([]:operation list) batches_needing_liquidity in
ops, storage

end

Expand Down
Loading

0 comments on commit 213dc1b

Please sign in to comment.