From 97dc4185cddfce18a13ef8c468b0fc6382560b5a Mon Sep 17 00:00:00 2001 From: Jason Ridgway-Taylor Date: Tue, 17 Oct 2023 20:20:19 +0530 Subject: [PATCH] Added liquidity injection to Vault --- batcher/batcher.mligo | 70 +-- batcher/errors.mligo | 3 + batcher/marketmaker.mligo | 65 ++- batcher/michelson/batcher-ghostnet.tz | 70 +-- batcher/michelson/tokenmanager-ghostnet.tz | 84 ++- batcher/michelson/tzbtc-vault-ghostnet.tz | 585 ++++++++++++++------- batcher/tokenmanager.mligo | 12 +- batcher/types.mligo | 7 + batcher/utils.mligo | 107 ++-- batcher/vault.mligo | 43 +- 10 files changed, 688 insertions(+), 358 deletions(-) diff --git a/batcher/batcher.mligo b/batcher/batcher.mligo index b3f6dcd0..1314e42e 100644 --- a/batcher/batcher.mligo +++ b/batcher/batcher.mligo @@ -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) @@ -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 @@ -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) @@ -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) @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 @@ -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 [] diff --git a/batcher/errors.mligo b/batcher/errors.mligo index c6e7c863..db5da42f 100644 --- a/batcher/errors.mligo +++ b/batcher/errors.mligo @@ -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 + diff --git a/batcher/marketmaker.mligo b/batcher/marketmaker.mligo index f20951d0..a03f6937 100644 --- a/batcher/marketmaker.mligo +++ b/batcher/marketmaker.mligo @@ -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 @@ -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 @@ -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 diff --git a/batcher/michelson/batcher-ghostnet.tz b/batcher/michelson/batcher-ghostnet.tz index e9494207..fb553442 100644 --- a/batcher/michelson/batcher-ghostnet.tz +++ b/batcher/michelson/batcher-ghostnet.tz @@ -326,22 +326,8 @@ CDR ; GET ; IF_NONE { PUSH nat 111 ; FAILWITH } {} ; - DUP 3 ; - GET 8 ; - DUP 4 ; - GET 7 ; - DUP 5 ; - GET 5 ; - DIG 5 ; - GET 3 ; - DIG 4 ; - PUSH nat 1 ; - DIG 6 ; - PAIR ; - PAIR ; - PAIR 5 ; NOW ; - DUP 5 ; + DUP 7 ; CAR ; CAR ; CAR ; @@ -350,7 +336,7 @@ CDR ; DUP 2 ; CAR ; - DUP 7 ; + DUP 9 ; UNPAIR ; DUP ; DUP 3 ; @@ -370,7 +356,7 @@ ADD ; NONE address ; PUSH nat 0 ; - DIG 7 ; + DIG 9 ; PUSH nat 0 ; PUSH nat 0 ; PUSH nat 0 ; @@ -391,7 +377,7 @@ (pair timestamp timestamp)) ; DIG 5 ; PAIR 6 ; - DUP 5 ; + DUP 7 ; DUP 3 ; DUP 4 ; CDR ; @@ -417,7 +403,7 @@ { SWAP ; PUSH string "/" ; CONCAT ; SWAP ; CONCAT } ; UPDATE ; UPDATE 1 } - { DUP 7 ; + { DUP 9 ; CAR ; CAR ; CDR ; @@ -437,7 +423,7 @@ ADD ; NONE address ; PUSH nat 0 ; - DIG 7 ; + DIG 9 ; PUSH nat 0 ; PUSH nat 0 ; PUSH nat 0 ; @@ -458,7 +444,7 @@ (pair timestamp timestamp)) ; DIG 5 ; PAIR 6 ; - DUP 5 ; + DUP 7 ; DUP 3 ; DUP 4 ; CDR ; @@ -484,8 +470,8 @@ { SWAP ; PUSH string "/" ; CONCAT ; SWAP ; CONCAT } ; UPDATE ; UPDATE 1 } - { DIG 3 ; DIG 6 ; DROP 3 ; DUP 5 ; DIG 2 } } - { DIG 7 ; + { DIG 3 ; DIG 8 ; DROP 3 ; DUP 7 ; DIG 2 } } + { DIG 9 ; DROP ; DUP 2 ; INT ; @@ -516,7 +502,7 @@ (pair (pair string string) (pair int int) timestamp)) ; LEFT timestamp ; UPDATE 3 ; - DUP 5 ; + DUP 7 ; PAIR } ; UNPAIR ; DUP 3 ; @@ -544,7 +530,7 @@ { SWAP ; PUSH string "/" ; CONCAT ; SWAP ; CONCAT } ; UPDATE ; UPDATE 1 } - { DROP ; DUP 5 ; DIG 2 } } } ; + { DROP ; DUP 7 ; DIG 2 } } } ; DIG 2 ; DUP ; GET 3 ; @@ -553,14 +539,14 @@ { DROP ; PUSH bool True } ; NOT ; IF { PUSH nat 140 ; FAILWITH } {} ; - DIG 5 ; + DIG 7 ; CDR ; CDR ; CDR ; - DUP 6 ; + DUP 8 ; GET ; IF_NONE - { DROP 6 ; PUSH nat 139 ; FAILWITH } + { DROP 8 ; PUSH nat 139 ; FAILWITH } { DUP 2 ; CAR ; DUP 2 ; @@ -583,7 +569,7 @@ SWAP ; UPDATE ; SOME ; - DUP 10 ; + DUP 12 ; UPDATE ; UPDATE 2 ; UPDATE 2 ; @@ -705,23 +691,37 @@ UPDATE 1 ; UPDATE 1 ; UPDATE 1 ; + DUP 5 ; + GET 8 ; + DUP 6 ; + GET 7 ; + DUP 7 ; + GET 5 ; + DIG 7 ; + GET 3 ; + DIG 6 ; + PUSH nat 1 ; + DIG 8 ; + PAIR ; + PAIR ; + PAIR 5 ; PUSH mutez 0 ; EMPTY_MAP string (pair (pair nat string (option address) nat (option string)) nat) ; PAIR ; - DIG 2 ; + DIG 3 ; ITER { UNPAIR ; DIG 2 ; UNPAIR ; DIG 3 ; DIG 3 ; CAR ; - IF_LEFT { DROP ; DUP 5 ; CAR ; CAR ; CAR } { DROP ; DUP 5 ; CAR ; CDR } ; + IF_LEFT { DROP ; DUP 4 ; CAR ; CAR ; CAR } { DROP ; DUP 4 ; CAR ; CDR } ; PAIR ; PAIR ; DUP 6 ; SWAP ; EXEC ; - DUP 3 ; + DUP 4 ; CAR ; CAR ; CDR ; @@ -730,7 +730,7 @@ ADD ; SWAP ; PAIR } ; - DIG 2 ; + SWAP ; DIG 4 ; DROP 2 ; UNPAIR ; @@ -4320,7 +4320,7 @@ SWAP ; ITER { CDR ; PUSH nat 0 ; SWAP ; COMPARE ; GT ; OR } ; OR } } } ; - view "get_current_batches" + view "get_batches_needing_liquidity" unit (list (pair (nat %batch_number) (or %status @@ -4385,7 +4385,7 @@ CDR ; SWAP ; GET ; - IF_NONE {} { CONS } } ; + IF_NONE {} { SOME ; IF_NONE {} { CONS } } } ; SWAP ; DROP } } diff --git a/batcher/michelson/tokenmanager-ghostnet.tz b/batcher/michelson/tokenmanager-ghostnet.tz index e9846040..259f6ec9 100644 --- a/batcher/michelson/tokenmanager-ghostnet.tz +++ b/batcher/michelson/tokenmanager-ghostnet.tz @@ -112,17 +112,17 @@ DUP 3 ; CDR ; CAR ; - DUP 3 ; + DUP 4 ; + CAR ; + CDR ; + DUP 4 ; CAR ; DUP ; CAR ; CAR ; DUP 2 ; CDR ; - DUP 7 ; - CAR ; - CDR ; - DUP ; + DUP 4 ; PUSH nat 1 ; DUP 7 ; CAR ; @@ -130,7 +130,7 @@ ADD ; COMPARE ; GT ; - IF { DROP ; PUSH nat 127 ; FAILWITH } + IF { DIG 3 ; DROP ; PUSH nat 127 ; FAILWITH } { EMPTY_MAP string (pair nat string (option address) nat (option string)) ; DUP 7 ; CAR ; @@ -146,31 +146,31 @@ SWAP ; DUP 2 ; GET 8 ; - DUP 6 ; + DUP 5 ; GET 8 ; COMPARE ; EQ ; DUP 3 ; GET 7 ; - DUP 7 ; + DUP 6 ; GET 7 ; COMPARE ; EQ ; DUP 4 ; GET 5 ; - DUP 8 ; + DUP 7 ; GET 5 ; COMPARE ; EQ ; DUP 5 ; GET 3 ; - DUP 9 ; + DUP 8 ; GET 3 ; COMPARE ; EQ ; DIG 5 ; CAR ; - DUP 9 ; + DUP 8 ; CAR ; COMPARE ; EQ ; @@ -194,31 +194,31 @@ SWAP ; DUP 2 ; GET 8 ; - DUP 6 ; + DUP 5 ; GET 8 ; COMPARE ; EQ ; DUP 3 ; GET 7 ; - DUP 7 ; + DUP 6 ; GET 7 ; COMPARE ; EQ ; DUP 4 ; GET 5 ; - DUP 8 ; + DUP 7 ; GET 5 ; COMPARE ; EQ ; DUP 5 ; GET 3 ; - DUP 9 ; + DUP 8 ; GET 3 ; COMPARE ; EQ ; DIG 5 ; CAR ; - DUP 9 ; + DUP 8 ; CAR ; COMPARE ; EQ ; @@ -244,31 +244,31 @@ SWAP ; DUP 2 ; GET 8 ; - DUP 6 ; + DUP 5 ; GET 8 ; COMPARE ; EQ ; DUP 3 ; GET 7 ; - DUP 7 ; + DUP 6 ; GET 7 ; COMPARE ; EQ ; DUP 4 ; GET 5 ; - DUP 8 ; + DUP 7 ; GET 5 ; COMPARE ; EQ ; DUP 5 ; GET 3 ; - DUP 9 ; + DUP 8 ; GET 3 ; COMPARE ; EQ ; DIG 5 ; CAR ; - DUP 9 ; + DUP 8 ; CAR ; COMPARE ; EQ ; @@ -292,31 +292,31 @@ SWAP ; DUP 2 ; GET 8 ; - DUP 6 ; + DUP 5 ; GET 8 ; COMPARE ; EQ ; DUP 3 ; GET 7 ; - DUP 7 ; + DUP 6 ; GET 7 ; COMPARE ; EQ ; DUP 4 ; GET 5 ; - DUP 8 ; + DUP 7 ; GET 5 ; COMPARE ; EQ ; DUP 5 ; GET 3 ; - DUP 9 ; + DUP 8 ; GET 3 ; COMPARE ; EQ ; DIG 5 ; CAR ; - DUP 9 ; + DUP 8 ; CAR ; COMPARE ; EQ ; @@ -327,6 +327,8 @@ OR } ; OR ; IF { PUSH nat 1 } { PUSH nat 2 } } ; + DIG 4 ; + SWAP ; DUP 7 ; CAR ; SIZE ; @@ -495,38 +497,34 @@ IF {} { DROP ; PUSH nat 115 ; FAILWITH } } ; DUP 3 ; CAR ; - DUP ; - CDR ; - GET 3 ; SWAP ; - CAR ; - CAR ; - GET 3 ; - PAIR ; DUP 4 ; - CAR ; - DIG 2 ; - DUP 5 ; GET 8 ; - DUP 6 ; + DUP 5 ; GET 7 ; - DUP 7 ; + DUP 6 ; GET 5 ; - DIG 7 ; + DUP 7 ; GET 3 ; - DUP 6 ; + DIG 7 ; + CAR ; + DUP ; CDR ; GET 3 ; - DIG 6 ; + SWAP ; CAR ; CAR ; GET 3 ; PAIR ; PAIR 5 ; DUP 3 ; - CAR ; - DIG 3 ; CDR ; + GET 3 ; + DIG 3 ; + CAR ; + CAR ; + GET 3 ; + SWAP ; DUP 2 ; DUP 2 ; COMPARE ; diff --git a/batcher/michelson/tzbtc-vault-ghostnet.tz b/batcher/michelson/tzbtc-vault-ghostnet.tz index 9ab4e07c..859e6e0c 100644 --- a/batcher/michelson/tzbtc-vault-ghostnet.tz +++ b/batcher/michelson/tzbtc-vault-ghostnet.tz @@ -1,7 +1,23 @@ { parameter (or (or (or (nat %addLiquidity) (address %change_admin_address)) (or (address %change_batcher_address) (address %change_marketmaker_address))) - (or (or (address %change_tokenmanager_address) (unit %claim)) (unit %removeLiquidity))) ; + (or (or (address %change_tokenmanager_address) (unit %claim)) + (or (pair %injectLiquidity + (pair (nat %amount) + (pair %from_token + (nat %token_id) + (string %name) + (option %address address) + (nat %decimals) + (option %standard string))) + (or %side (unit %buy) (unit %sell)) + (pair %to_token + (nat %token_id) + (string %name) + (option %address address) + (nat %decimals) + (option %standard string))) + (unit %removeLiquidity)))) ; storage (pair (pair (pair (address %administrator) (address %batcher)) (map %foreign_tokens @@ -506,213 +522,400 @@ DIG 2 ; CONS ; PAIR } } } } - { DROP ; - PUSH mutez 1 ; - AMOUNT ; - COMPARE ; - LT ; - IF {} { PUSH nat 118 ; FAILWITH } ; - SENDER ; - DUP 2 ; - CDR ; - CDR ; - CDR ; - DUP 2 ; - GET ; - IF_NONE - { DROP 5 ; PUSH nat 151 ; FAILWITH } - { UNPAIR ; - CDR ; - DUP 4 ; + { IF_LEFT + { DIG 2 ; + DIG 3 ; + DIG 4 ; + DROP 3 ; + DUP 2 ; + CAR ; CDR ; CDR ; - CAR ; - INT ; - PUSH int 1 ; - SWAP ; - PAIR ; - DUP 2 ; - INT ; - PUSH int 1 ; - SWAP ; - PAIR ; + SENDER ; + COMPARE ; + EQ ; + IF {} { PUSH nat 168 ; FAILWITH } ; + PUSH mutez 1 ; + AMOUNT ; + COMPARE ; + LT ; + IF {} { PUSH nat 118 ; FAILWITH } ; + UNIT ; + LEFT unit ; + LEFT unit ; + IF_LEFT + { IF_LEFT { DROP ; PUSH nat 1 } { DROP ; PUSH nat 0 } } + { DROP ; PUSH nat 2 } ; DUP 2 ; + CDR ; CAR ; - DUP 2 ; + IF_LEFT { DROP ; PUSH nat 0 } { DROP ; PUSH nat 1 } ; + NOW ; + DUP 4 ; CDR ; - MUL ; - DIG 2 ; CDR ; - DIG 2 ; + DUP 5 ; CAR ; - MUL ; - PAIR ; - DUP 8 ; - DUP 6 ; - CDR ; CAR ; + DIG 5 ; CAR ; - DUP 3 ; - PAIR 3 ; - DUP 7 ; - SWAP ; - EXEC ; - UNPAIR ; - SWAP ; - DIG 9 ; - DIG 3 ; + CDR ; PAIR ; PAIR ; - DUP 6 ; + PAIR 4 ; + DUP 2 ; CAR ; - CDR ; CAR ; - ITER { CDR ; - SWAP ; - UNPAIR ; - UNPAIR ; - DIG 2 ; - DIG 3 ; - DUP 3 ; - PAIR 3 ; - DUP 9 ; - SWAP ; - EXEC ; - UNPAIR ; - DIG 3 ; - SWAP ; - PAIR ; - DUP 10 ; - SWAP ; - EXEC ; - DIG 2 ; - PAIR ; - PAIR } ; - DIG 6 ; - DIG 7 ; - DROP 2 ; - UNPAIR ; - CDR ; - DUP 7 ; - CDR ; CDR ; + DUP 2 ; CAR ; - DUP 5 ; + CAR ; + CAR ; + GET 5 ; + IF_NONE + { DROP 2 ; PUSH nat 109 ; FAILWITH } + { DUP 3 ; + CAR ; + CAR ; + CAR ; + GET 8 ; + IF_NONE + { PUSH bool False } + { PUSH string "FA1.2 token" ; SWAP ; COMPARE ; EQ } ; + IF { DUP ; + CONTRACT %approve (pair (address %spender) (nat %value)) ; + IF_NONE { PUSH nat 157 ; FAILWITH } {} ; + PUSH mutez 0 ; + DUP 5 ; + CAR ; + CAR ; + CDR ; + DUP 5 ; + PAIR ; + TRANSFER_TOKENS } + { DUP 3 ; + CAR ; + CAR ; + CAR ; + GET 8 ; + IF_NONE + { PUSH bool False } + { PUSH string "FA2 token" ; SWAP ; COMPARE ; EQ } ; + IF { SELF_ADDRESS ; + DUP 2 ; + CONTRACT %add_operator + (pair (pair (address %operator) (address %owner)) (nat %token_id)) ; + IF_NONE { PUSH nat 157 ; FAILWITH } {} ; + PUSH mutez 0 ; + DUP 6 ; + CAR ; + CAR ; + CAR ; + CAR ; + DIG 3 ; + DUP 6 ; + PAIR ; + PAIR ; + TRANSFER_TOKENS } + { PUSH nat 108 ; FAILWITH } } ; + DUP 3 ; + CONTRACT %deposit + (pair (pair %swap + (pair %from + (pair %token + (nat %token_id) + (string %name) + (option %address address) + (nat %decimals) + (option %standard string)) + (nat %amount)) + (pair %to + (nat %token_id) + (string %name) + (option %address address) + (nat %decimals) + (option %standard string))) + (timestamp %created_at) + (nat %side) + (nat %tolerance)) ; + IF_NONE { PUSH nat 157 ; FAILWITH } {} ; + PUSH mutez 0 ; + DUP 6 ; + TRANSFER_TOKENS ; + DUP 5 ; + CAR ; + CAR ; + CAR ; + GET 8 ; + IF_NONE + { PUSH bool False } + { PUSH string "FA1.2 token" ; SWAP ; COMPARE ; EQ } ; + IF { DIG 2 ; DIG 3 ; DIG 4 ; DROP 3 ; NONE operation } + { DUP 5 ; + CAR ; + CAR ; + CAR ; + GET 8 ; + IF_NONE + { PUSH bool False } + { PUSH string "FA2 token" ; SWAP ; COMPARE ; EQ } ; + IF { SELF_ADDRESS ; + DIG 3 ; + CONTRACT %remove_operator + (pair (pair (address %operator) (address %owner)) (nat %token_id)) ; + IF_NONE { PUSH nat 157 ; FAILWITH } {} ; + PUSH mutez 0 ; + DIG 6 ; + CAR ; + CAR ; + CAR ; + CAR ; + DIG 3 ; + DIG 6 ; + PAIR ; + PAIR ; + TRANSFER_TOKENS ; + SOME } + { DIG 2 ; DIG 3 ; DIG 4 ; DROP 3 ; PUSH nat 108 ; FAILWITH } } ; + IF_NONE { NIL operation } { NIL operation ; SWAP ; CONS } ; + SWAP ; + CONS ; + SWAP ; + CONS } ; + PAIR } + { DROP ; + PUSH mutez 1 ; + AMOUNT ; COMPARE ; - GT ; - IF { SWAP ; DIG 2 ; DIG 3 ; DIG 6 ; DROP 5 ; PUSH nat 154 ; FAILWITH } - { DIG 3 ; - DUP 7 ; - CDR ; - CDR ; - CAR ; - SUB ; - ABS ; - DIG 2 ; - DUP 7 ; - DIG 7 ; - CDR ; - DUP ; - CAR ; - DIG 6 ; - UPDATE 1 ; - UPDATE 1 ; - UPDATE 2 ; - DUP ; - CAR ; - DUP ; - CDR ; - DIG 5 ; - UPDATE 1 ; - UPDATE 2 ; - UPDATE 1 ; - DUP ; - CDR ; - DUP ; - CDR ; - DIG 4 ; - UPDATE 1 ; - UPDATE 2 ; - UPDATE 2 ; - PAIR } ; - UNPAIR ; - DUP 4 ; - CONTRACT unit ; - IF_NONE { PUSH nat 102 ; FAILWITH } { DUP 4 ; UNIT ; TRANSFER_TOKENS } ; - SELF_ADDRESS ; - NIL operation ; - DIG 4 ; - ITER { CDR ; - DUP ; - CAR ; - GET 5 ; - IF_NONE - { DROP ; PUSH nat 109 ; FAILWITH } - { DUP 2 ; - CAR ; - GET 8 ; - IF_NONE - { DROP 2 ; PUSH nat 108 ; FAILWITH } - { PUSH string "FA1.2 token" ; - DUP 2 ; - COMPARE ; - EQ ; - IF { DROP ; - CONTRACT %transfer (pair (address %from) (address %to) (nat %value)) ; - IF_NONE { PUSH nat 101 ; FAILWITH } {} ; - PUSH mutez 0 ; - DIG 2 ; - CDR ; - DUP 9 ; - DUP 6 ; - PAIR 3 ; - TRANSFER_TOKENS } - { PUSH string "FA2 token" ; - SWAP ; - COMPARE ; - EQ ; - IF { CONTRACT %transfer - (list (pair (address %from_) (list %tx (pair (address %to_) (nat %token_id) (nat %amount))))) ; - IF_NONE { PUSH nat 101 ; FAILWITH } {} ; - PUSH mutez 0 ; - NIL (pair address (list (pair address nat nat))) ; - NIL (pair address nat nat) ; - DUP 5 ; - CDR ; - DIG 5 ; - CAR ; - CAR ; - DUP 12 ; - PAIR 3 ; - CONS ; - DUP 6 ; - PAIR ; - CONS ; - TRANSFER_TOKENS } - { DROP 2 ; PUSH nat 108 ; FAILWITH } } } } ; - CONS } ; - SWAP ; - DROP ; - DUP 3 ; - DUP 4 ; - CDR ; - DUP ; - CDR ; - DIG 5 ; + LT ; + IF {} { PUSH nat 118 ; FAILWITH } ; + SENDER ; + DUP 2 ; CDR ; CDR ; CDR ; - DIG 7 ; - NONE (pair (pair address nat) mutez) ; - SWAP ; - UPDATE ; - UPDATE 2 ; - UPDATE 2 ; - UPDATE 2 ; - PUSH mutez 0 ; - DIG 4 ; - COMPARE ; - GT ; - IF { SWAP ; DIG 2 ; CONS } { DIG 2 ; DROP ; SWAP } ; - PAIR } } } } } + DUP 2 ; + GET ; + IF_NONE + { DROP 5 ; PUSH nat 151 ; FAILWITH } + { UNPAIR ; + CDR ; + DUP 4 ; + CDR ; + CDR ; + CAR ; + INT ; + PUSH int 1 ; + SWAP ; + PAIR ; + DUP 2 ; + INT ; + PUSH int 1 ; + SWAP ; + PAIR ; + DUP 2 ; + CAR ; + DUP 2 ; + CDR ; + MUL ; + DIG 2 ; + CDR ; + DIG 2 ; + CAR ; + MUL ; + PAIR ; + DUP 8 ; + DUP 6 ; + CDR ; + CAR ; + CAR ; + DUP 3 ; + PAIR 3 ; + DUP 7 ; + SWAP ; + EXEC ; + UNPAIR ; + SWAP ; + DIG 9 ; + DIG 3 ; + PAIR ; + PAIR ; + DUP 6 ; + CAR ; + CDR ; + CAR ; + ITER { CDR ; + SWAP ; + UNPAIR ; + UNPAIR ; + DIG 2 ; + DIG 3 ; + DUP 3 ; + PAIR 3 ; + DUP 9 ; + SWAP ; + EXEC ; + UNPAIR ; + DIG 3 ; + SWAP ; + PAIR ; + DUP 10 ; + SWAP ; + EXEC ; + DIG 2 ; + PAIR ; + PAIR } ; + DIG 6 ; + DIG 7 ; + DROP 2 ; + UNPAIR ; + CDR ; + DUP 7 ; + CDR ; + CDR ; + CAR ; + DUP 5 ; + COMPARE ; + GT ; + IF { SWAP ; DIG 2 ; DIG 3 ; DIG 6 ; DROP 5 ; PUSH nat 154 ; FAILWITH } + { DIG 3 ; + DUP 7 ; + CDR ; + CDR ; + CAR ; + SUB ; + ABS ; + DIG 2 ; + DUP 7 ; + DIG 7 ; + CDR ; + DUP ; + CAR ; + DIG 6 ; + UPDATE 1 ; + UPDATE 1 ; + UPDATE 2 ; + DUP ; + CAR ; + DUP ; + CDR ; + DIG 5 ; + UPDATE 1 ; + UPDATE 2 ; + UPDATE 1 ; + DUP ; + CDR ; + DUP ; + CDR ; + DIG 4 ; + UPDATE 1 ; + UPDATE 2 ; + UPDATE 2 ; + PAIR } ; + UNPAIR ; + DUP 4 ; + CONTRACT unit ; + IF_NONE { PUSH nat 102 ; FAILWITH } { DUP 4 ; UNIT ; TRANSFER_TOKENS } ; + SELF_ADDRESS ; + NIL operation ; + DIG 4 ; + ITER { CDR ; + DUP ; + CAR ; + GET 5 ; + IF_NONE + { DROP ; PUSH nat 109 ; FAILWITH } + { DUP 2 ; + CAR ; + GET 8 ; + IF_NONE + { DROP 2 ; PUSH nat 108 ; FAILWITH } + { PUSH string "FA1.2 token" ; + DUP 2 ; + COMPARE ; + EQ ; + IF { DROP ; + CONTRACT %transfer (pair (address %from) (address %to) (nat %value)) ; + IF_NONE { PUSH nat 101 ; FAILWITH } {} ; + PUSH mutez 0 ; + DIG 2 ; + CDR ; + DUP 9 ; + DUP 6 ; + PAIR 3 ; + TRANSFER_TOKENS } + { PUSH string "FA2 token" ; + SWAP ; + COMPARE ; + EQ ; + IF { CONTRACT %transfer + (list (pair (address %from_) (list %tx (pair (address %to_) (nat %token_id) (nat %amount))))) ; + IF_NONE { PUSH nat 101 ; FAILWITH } {} ; + PUSH mutez 0 ; + NIL (pair address (list (pair address nat nat))) ; + NIL (pair address nat nat) ; + DUP 5 ; + CDR ; + DIG 5 ; + CAR ; + CAR ; + DUP 12 ; + PAIR 3 ; + CONS ; + DUP 6 ; + PAIR ; + CONS ; + TRANSFER_TOKENS } + { DROP 2 ; PUSH nat 108 ; FAILWITH } } } } ; + CONS } ; + SWAP ; + DROP ; + DUP 3 ; + DUP 4 ; + CDR ; + DUP ; + CDR ; + DIG 5 ; + CDR ; + CDR ; + CDR ; + DIG 7 ; + NONE (pair (pair address nat) mutez) ; + SWAP ; + UPDATE ; + UPDATE 2 ; + UPDATE 2 ; + UPDATE 2 ; + PUSH mutez 0 ; + DIG 4 ; + COMPARE ; + GT ; + IF { SWAP ; DIG 2 ; CONS } { DIG 2 ; DROP ; SWAP } ; + PAIR } } } } } ; + view "get_native_token_of_vault" + unit + (pair (nat %token_id) + (string %name) + (option %address address) + (nat %decimals) + (option %standard string)) + { CDR ; CDR ; CAR ; CAR ; CAR } ; + view "get_vault_balances" + unit + (pair (pair (pair %token + (nat %token_id) + (string %name) + (option %address address) + (nat %decimals) + (option %standard string)) + (nat %amount)) + (map string + (pair (pair %token + (nat %token_id) + (string %name) + (option %address address) + (nat %decimals) + (option %standard string)) + (nat %amount)))) + { CDR ; DUP ; CAR ; CDR ; CAR ; SWAP ; CDR ; CAR ; CAR ; PAIR } } diff --git a/batcher/tokenmanager.mligo b/batcher/tokenmanager.mligo index d7544d05..172a8bd6 100644 --- a/batcher/tokenmanager.mligo +++ b/batcher/tokenmanager.mligo @@ -25,7 +25,7 @@ let confirm_swap_pair_is_disabled_prior_to_removal 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 @@ -36,7 +36,7 @@ let set_deposit_status (pair_name: string) (disabled: bool) (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 valid_swap = get_valid_swap_reduced pair_name storage.valid_swaps in let valid_swap = { valid_swap with is_disabled_for_deposits = disabled; } in @@ -49,7 +49,7 @@ let set_deposit_status let amend_token_and_pair_limit (limit: 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 let token_count = ValidTokens.size storage.valid_tokens in let pair_count = ValidSwaps.size storage.valid_swaps in @@ -63,7 +63,7 @@ let amend_token_and_pair_limit let add_token_swap_pair (valid_swap: valid_swap) (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 valid_swap.swap.from.token.decimals < minimum_precision then failwith swap_precision_is_less_than_minimum else if valid_swap.swap.to.decimals < minimum_precision then failwith swap_precision_is_less_than_minimum else @@ -77,7 +77,7 @@ let add_token_swap_pair let remove_token_swap_pair (swap: valid_swap) (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 () = confirm_swap_pair_is_disabled_prior_to_removal swap in let (u_swaps,u_tokens) = Tokens.remove_pair swap storage.valid_swaps storage.valid_tokens in @@ -90,7 +90,7 @@ let remove_token_swap_pair let change_oracle_price_source (source_change: oracle_source_change) (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 valid_swap_reduced = get_valid_swap_reduced source_change.pair_name storage.valid_swaps in let valid_swap = { valid_swap_reduced with oracle_address = source_change.oracle_address; oracle_asset_name = source_change.oracle_asset_name; oracle_precision = source_change.oracle_precision; } in diff --git a/batcher/types.mligo b/batcher/types.mligo index c6a9f602..92b1951f 100644 --- a/batcher/types.mligo +++ b/batcher/types.mligo @@ -511,3 +511,10 @@ let mem_map Map.fold find m false end + +type liquidity_injection_request = { + side:side; + from_token:token; + to_token:token; + amount:nat; +} diff --git a/batcher/utils.mligo b/batcher/utils.mligo index ebd5165e..e6afa80f 100644 --- a/batcher/utils.mligo +++ b/batcher/utils.mligo @@ -404,10 +404,7 @@ let are_equivalent_tokens let reject_if_tez_supplied(): unit = if Tezos.get_amount () < 1mutez then () else failwith endpoint_does_not_accept_tez -[@inline] -let is_administrator - (administrator : address) : unit = - if Tezos.get_sender () = administrator then () else failwith sender_not_administrator + type orace_price_update = oracle_price_update @@ -587,33 +584,6 @@ let transfer_fee (receiver : address) (amount : tez) : operation = end -[@inline] -let convert_oracle_price - (precision: nat) - (swap: swap) - (lastupdated: timestamp) - (price: nat) - (tokens: ValidTokens.t_map): exchange_rate = - let prc,den : nat * int = if swap.from.token.decimals > precision then - let diff:int = swap.from.token.decimals - precision in - let diff_pow = pow 10 diff in - let adj_price = to_nat (price * diff_pow) in - let denom = pow 10 (int swap.from.token.decimals) in - (adj_price, denom) - else - let denom = pow 10 (int precision) in - (price, denom) - in - let rational_price = Rational.new (int prc) in - let rational_denom = Rational.new den in - let rational_rate: Rational.t = Rational.div rational_price rational_denom in - let swap_reduced: swap_reduced = swap_to_swap_reduced swap in - let rate = { - swap = swap_reduced; - rate = rational_rate; - when = lastupdated; - } in - scale_on_receive_for_token_precision_difference rate tokens [@inline] let add_token_amounts @@ -983,9 +953,84 @@ let get_current_vaults end +module BatcherUtils = struct + +[@inline] +let get_batches_needing_liquidity + (batcher:address): batch list = + match Tezos.call_view "get_current_batches" () batcher with + | Some batches -> batches + | None -> failwith unable_to_get_current_batches_from_batcher + +end + + +module OracleUtils = struct + +[@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 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) + (rates: rates_current): unit = + 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 convert_oracle_price + (precision: nat) + (swap: swap) + (lastupdated: timestamp) + (price: nat) + (tokens: ValidTokens.t_map): exchange_rate = + let prc,den : nat * int = if swap.from.token.decimals > precision then + let diff:int = swap.from.token.decimals - precision in + let diff_pow = pow 10 diff in + let adj_price = to_nat (price * diff_pow) in + let denom = pow 10 (int swap.from.token.decimals) in + (adj_price, denom) + else + let denom = pow 10 (int precision) in + (price, denom) + in + let rational_price = Rational.new (int prc) in + let rational_denom = Rational.new den in + let rational_rate: Rational.t = Rational.div rational_price rational_denom in + let swap_reduced: swap_reduced = swap_to_swap_reduced swap in + let rate = { + swap = swap_reduced; + rate = rational_rate; + when = lastupdated; + } in + scale_on_receive_for_token_precision_difference rate tokens + +end + + [@inline] let get_native_token_from_vault (vault_address: address) : token = match Tezos.call_view "get_native_token_from_vault" () vault_address with | Some tokn -> tokn | None -> failwith unable_to_get_native_token_from_vault + +[@inline] +let is_known_sender + (addr : address) + (error: nat): unit = + if Tezos.get_sender () = addr then () else failwith error diff --git a/batcher/vault.mligo b/batcher/vault.mligo index 1cf8f7b9..b5d72bbf 100644 --- a/batcher/vault.mligo +++ b/batcher/vault.mligo @@ -189,7 +189,7 @@ let remove_liquidity 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 storage = { storage with administrator = new_admin_address; } in no_op storage @@ -198,16 +198,48 @@ let change_admin_address let change_batcher_address (new_batcher_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 batcher = new_batcher_address; } in no_op storage +[@inline] +let construct_order + (side:side) + (from_token:token) + (to_token:token) + (amount:nat) : external_swap_order = + let side_nat = side_to_nat side in + let tolerance = tolerance_to_nat Exact in + let swap = { + from= { + token = from_token; + amount= amount + }; + to = to_token; + } in + { + swap = swap; + created_at = Tezos.get_now (); + side = side_nat; + tolerance = tolerance; + } + +[@inline] +let inject_liquidity + (lir: liquidity_injection_request) + (storage: storage) : result = + let () = is_known_sender storage.marketmaker sender_not_marketmaker in + let () = reject_if_tez_supplied () in + let o = construct_order lir.side lir.from_token lir.to_token lir.amount in + let ops = execute_deposit o storage.batcher in + ops, storage + [@inline] let change_marketmaker_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 @@ -216,7 +248,7 @@ let change_marketmaker_address let change_tokenmanager_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 @@ -235,6 +267,7 @@ type entrypoint = | AddLiquidity of nat | RemoveLiquidity | Claim + | InjectLiquidity of liquidity_injection_request | Change_admin_address of address | Change_batcher_address of address | Change_marketmaker_address of address @@ -247,6 +280,8 @@ let main | AddLiquidity a -> Vault.add_liquidity a storage | RemoveLiquidity -> Vault.remove_liquidity storage | Claim -> Vault.claim storage + (* MarketMaker endpoints *) + | InjectLiquidity lir -> Vault.inject_liquidity lir storage (* Admin endpoints *) | Change_admin_address new_admin_address -> Vault.change_admin_address new_admin_address storage | Change_batcher_address new_batcher_address -> Vault.change_batcher_address new_batcher_address storage