Skip to content

Commit

Permalink
Merge branch '389-community-market-maker-1' into fix-issue-cmm
Browse files Browse the repository at this point in the history
  • Loading branch information
glottologist authored Oct 5, 2023
2 parents f7ffcd2 + 5696905 commit a90bd15
Show file tree
Hide file tree
Showing 8 changed files with 803 additions and 169 deletions.
4 changes: 2 additions & 2 deletions batcher-ui/src/config/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ module.exports = {
'https://storage.googleapis.com/marigold-public-bucket/batcher-logo.png',
NEXT_PUBLIC_TEZOS_NODE_URI: 'https://ghostnet.tezos.marigold.dev/',
NEXT_PUBLIC_TZKT_URI_API: 'https://api.ghostnet.tzkt.io',
NEXT_PUBLIC_BATCHER_CONTRACT_HASH: 'KT1UPMR3WkoFRJYmBBbvv4Z9bBhKYuhCx7Cq',
NEXT_PUBLIC_BATCHER_CONTRACT_HASH: 'KT1LhTpwSGcFAUUM3JYjW8XW74UHP82YzERy',
NEXT_PUBLIC_MARKETMAKER_CONTRACT_HASH:
'KT1TNX1YLCmPJN4rbwAUsUAdnqVYrZ5X5YNB',
'KT1XKvKiTTj8N6WKv3MhnZhFjZopFGQGBTdT',
NEXT_PUBLIC_LOCAL_STORAGE_KEY_STATE: 'batcher-state',
NEXT_PUBLIC_GA_TRACKING_ID: 'G-2K59PEELC8',
},
Expand Down
20 changes: 0 additions & 20 deletions batcher/batcher.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -1486,15 +1486,6 @@ let redeemable_holdings_available ((), storage : unit * storage) : bool =
| None -> false
| Some bots -> Map.fold find_non_zero_holding bots false

(* Mapping order type to total amount of placed orders *)
type ordertypes = (ordertype, nat) map

(* pairing of batch_id and ordertypes. *)
type batch_ordertypes = (nat, ordertypes) map

(* Associated user address to a given set of batches and ordertypes *)
type user_batch_ordertypes = (address, batch_ordertypes) big_map

[@view]
let get_current_batches ((),storage: unit * storage) : batch list=
let collect_batches (acc, (_s, i) : batch list * (string * nat)) : batch list =
Expand All @@ -1504,17 +1495,6 @@ let get_current_batches ((),storage: unit * storage) : batch list=
in
Map.fold collect_batches storage.batch_set.current_batch_indices []

[@view]
let get_current_batches_reduced ((), storage: unit * storage) : reduced_batch list =
let current_batches: batch list = get_current_batches ((),storage) in
let map_to_reduced (b: batch) : reduced_batch = {
status = b.status;
volumes = b.volumes;
market_vault_used = b.market_vault_used;
}
in
List.map map_to_reduced current_batches


let main
(action, storage : entrypoint * storage) : operation list * storage =
Expand Down
2 changes: 1 addition & 1 deletion batcher/errors.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
[@inline] let no_holdings_to_claim = 155n
[@inline] let incorrect_side_specified = 156n
[@inline] let entrypoint_does_not_exist = 157n
[@inline] let unable_to_get_reduced_batches_from_batcher = 158n
[@inline] let unable_to_get_batches_from_batcher = 158n
[@inline] let unable_to_get_oracle_price = 159n
[@inline] let contract_does_not_exist = 160n
[@inline] let unable_to_call_on_chain_view = 161n
491 changes: 479 additions & 12 deletions batcher/marketmaker-ghostnet.tz

Large diffs are not rendered by default.

160 changes: 108 additions & 52 deletions batcher/marketmaker.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ type metadata_update = Types.metadata_update
type swap_order = Types.swap_order
type batch = Types.batch
type batch_set = Types.batch_set
type reduced_batch = Types.reduced_batch
type external_swap_order = Types.external_swap_order

module Storage = struct
type t = {
Expand Down Expand Up @@ -86,48 +86,6 @@ let create_liq_order
redeemed = false;
}

(*
let inject_buy_side_liquidity
(non: nat)
(last_rate:exchange_rate)
(sell_volume: nat)
(batch:batch)
(batch_set: batch_set)
(storage: Storage.t): batch * Storage.t =
let (buy_token,sell_token) = batch.pair in
match Big_map.find_opt buy_token storage.vaults with
| None -> batch,storage
| Some v -> let liq_amount = find_liquidity_amount last_rate v.native_token.amount sell_volume in
let order = create_liq_order batch.batch_number non buy_token sell_token Buy liq_amount storage.valid_tokens in
Batch_Utils.update_storage_with_order order non batch.number batch batch_set storage
let inject_sell_side_liquidity
(non: nat)
(last_rate:exchange_rate)
(buy_volume: nat)
(batch:batch)
(batch_set: batch_set)
(storage: Storage.t): batch * Storage.t =
let (buy_token, sell_token) = batch.pair in
let mm = storage.market_maker in
match Big_map.find_opt sell_token mm.vaults with
| None -> batch,storage
| Some v -> let liq_amount = find_liquidity_amount last_rate v.native_token.amount buy_volume in
let order = create_liq_order batch.batch_number non sell_token buy_token Sell liq_amount storage.valid_tokens in
Batch_Utils.update_storage_with_order order non batch.number batch batch_set storage
let inject_jit_liquidity
(last_rate:exchange_rate)
(batch:batch)
(next_order_number:nat)
(storage: Storage.t): batch * Storage.t =
let batch_set = storage.batch_set in
let buy_volume = batch.volumes.buy_total_volume in
let sell_volume = batch.volumes.sell_total_volume in
if (buy_volume > 0n) && (sell_volume = 0n) then inject_sell_side_liquidity next_order_number last_rate buy_volume batch batch_set storage else
if (buy_volume = 0n) && (sell_volume > 0n) then inject_buy_side_liquidity next_order_number last_rate sell_volume batch batch_set storage else
batch,storage
*)

let create_or_update_market_vault_holding
(id: nat)
Expand Down Expand Up @@ -466,11 +424,109 @@ let redeem
(storage: Storage.t): (operation option * Storage.t) =
if Utils.has_redeemable_holdings storage.batcher then redeem_holdings storage else None,storage

[@inline]
let construct_order
(side:side)
(from_token:token)
(to_token:token)
(amount:nat) : external_swap_order =
let side_nat = Utils.side_to_nat side in
let tolerance = Utils.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 find_available_liquidity
(token:token)
(volume:nat)
(vaults:market_vaults): nat * market_vaults =
match Big_map.find_opt token.name vaults with
| None -> failwith Errors.no_market_vault_for_token
| Some v -> let nt = v.native_token in
let avail_liq = v.native_token.amount in
let liq = if volume < avail_liq then volume else avail_liq in
let nt = { nt with amount = abs(nt.amount - liq); } in
let v = { v with native_token = nt; } in
let uvs = Big_map.update token.name (Some v) vaults in
liq, uvs

[@inline]
let inject_buy_side_liq
(sell_side_volume:nat)
(batcher:address)
(batch:batch)
(storage:Storage.t) : (operation list * market_vaults) =
let (buy_token,sell_token) = batch.pair in
let buy_token_opt = Map.find_opt buy_token storage.valid_tokens in
let sell_token_opt = Map.find_opt sell_token storage.valid_tokens in
match (buy_token_opt,sell_token_opt) with
| Some bt, Some st -> let liq,vaults = find_available_liquidity bt sell_side_volume storage.vaults in
if liq = 0n then
([],vaults)
else
let order = construct_order Buy bt st liq in
let ops = Utils.execute_deposit order batcher in
(ops, vaults)
| _, _ -> failwith Errors.token_name_not_in_list_of_valid_tokens

[@inline]
let inject_sell_side_liq
(buy_side_volume:nat)
(batcher:address)
(batch:batch)
(storage:Storage.t) : (operation list * market_vaults) =
let (buy_token,sell_token) = batch.pair in
let buy_token_opt = Map.find_opt buy_token storage.valid_tokens in
let sell_token_opt = Map.find_opt sell_token storage.valid_tokens in
match (buy_token_opt,sell_token_opt) with
| Some bt, Some st -> let liq, vaults = find_available_liquidity st buy_side_volume storage.vaults in
if liq = 0n then
([],vaults)
else
let order = construct_order Sell st bt liq in
let ops = Utils.execute_deposit order batcher in
(ops, vaults)
| _, _ -> failwith Errors.token_name_not_in_list_of_valid_tokens

[@inline]
let inject_liquidity_if_required
(batcher:address)
(batch:batch)
(storage: Storage.t): (operation list * market_vaults) =
if batch.market_vault_used then ([], storage.vaults) else
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 _ -> ([], storage.vaults)
| Some bv, None -> inject_sell_side_liq bv batcher batch storage
| None, Some sv -> inject_buy_side_liq sv batcher batch storage
| None, None -> ([], storage.vaults)


[@inline]
let deposit
(_batcher:address)
(_batches: reduced_batch list)
(storage: Storage.t): (operation list * Storage.t) = ([]: operation list), storage
(batcher:address)
(batches: batch list)
(storage: Storage.t): (operation list * Storage.t) =
let inject = fun ((ol,s),rb:((operation list* Storage.t) * batch)) ->
let (iops,vaults) = inject_liquidity_if_required batcher rb s in
let s = {s with vaults = vaults; } in
(Utils.concatlo iops ol,s)
in
let (deposit_ops,storage) = List.fold inject batches ([],storage) in
deposit_ops, storage

end

Expand Down Expand Up @@ -533,21 +589,21 @@ let change_batcher_address
no_op storage

[@inline]
let get_reduced_batches
let get_batches
(failure_code: nat)
(batcher: address) : reduced_batch list =
match Tezos.call_view "get_current_batches_reduced" () batcher with
| Some rbl -> rbl
(batcher: address) : batch list =
match Tezos.call_view "get_current_batches" () batcher with
| Some bl -> bl
| None -> failwith failure_code

[@inline]
let tick
(storage: Storage.t) : result =
let () = Utils.reject_if_tez_supplied () in
let reduced_batches = get_reduced_batches Errors.unable_to_get_reduced_batches_from_batcher storage.batcher in
let batches = get_batches Errors.unable_to_get_batches_from_batcher storage.batcher in
let storage = TickUtils.rebalance_vaults storage in
let (redeem_op_opt, storage) = TickUtils.redeem storage in
let (deposit_ops, storage) = TickUtils.deposit storage.batcher reduced_batches storage in
let (deposit_ops, storage) = TickUtils.deposit storage.batcher batches storage in
let ops = match redeem_op_opt with
| Some op -> op :: deposit_ops
| None -> deposit_ops
Expand Down
Loading

0 comments on commit a90bd15

Please sign in to comment.