Skip to content

Commit

Permalink
Added redeem by batch
Browse files Browse the repository at this point in the history
  • Loading branch information
glottologist committed Oct 5, 2023
1 parent d6c815f commit c77cc8a
Show file tree
Hide file tree
Showing 7 changed files with 6,337 additions and 5,687 deletions.
9,669 changes: 6,280 additions & 3,389 deletions batcher/batcher-ghostnet.tz

Large diffs are not rendered by default.

45 changes: 17 additions & 28 deletions batcher/batcher-storage-ghostnet.tz
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
(Pair (Pair (Pair (Pair "tz1aSL2gjFnfh96Xf1Zp4T36LxbzKuzyvVJ4" {} {}) 600 10000)
(Pair (Pair (Pair (Pair "tz1ca4batAsNxMYab3mUK5H4QRjY8drV4ViL" {} {}) 600 10000)
(Pair "tz1burnburnburnburnburnburnburjAYjjX" 0)
10
{ Elt ""
0x68747470733a2f2f697066732e6763702e6d617269676f6c642e6465762f697066732f516d56375a534b6358324d4e75656938745a3268723555484d5a66737039476b375675345878766d6246734a4e45 })
(Pair (Pair {} 1)
"KT1XKvKiTTj8N6WKv3MhnZhFjZopFGQGBTdT")
(Pair (Pair {} {})
{}
{ Elt "tzBTC/EURL"
(Pair (Pair (Pair (Pair 0 "tzBTC" (Some "KT1P8RdJ5MfHMK5phKJ5JsfNfask5v2b2NQS") 8 (Some "FA1.2 token"))
1)
0
"EURL"
(Some "KT1RcHjqDWWycYQGrz4KBYoGZSMmMuVpkmuS")
6
(Some "FA2 token"))
"KT1DG2g5DPYWqyHKGpRL579YkYZwJxibwaAZ"
"BTC-EUR"
6
False) ;
{ Elt "BTCtz/USDT"
(Pair (Pair "BTCtz" "USDT") "KT1DG2g5DPYWqyHKGpRL579YkYZwJxibwaAZ" "BTC-USDT" 6 False) ;
Elt "BTCtz/USDtz"
(Pair (Pair "BTCtz" "USDtz") "KT1DG2g5DPYWqyHKGpRL579YkYZwJxibwaAZ" "BTC-USDT" 6 False) ;
Elt "tzBTC/EURL"
(Pair (Pair "tzBTC" "EURL") "KT1DG2g5DPYWqyHKGpRL579YkYZwJxibwaAZ" "BTC-EUR" 6 False) ;
Elt "tzBTC/USDT"
(Pair (Pair (Pair (Pair 0 "tzBTC" (Some "KT1P8RdJ5MfHMK5phKJ5JsfNfask5v2b2NQS") 8 (Some "FA1.2 token"))
1)
0
"USDT"
(Some "KT1WNrZ7pEbpmYBGPib1e7UVCeC6GA6TkJYR")
6
(Some "FA2 token"))
"KT1DG2g5DPYWqyHKGpRL579YkYZwJxibwaAZ"
"BTC-USDT"
6
False) })
{ Elt "EURL" (Pair 0 "EURL" (Some "KT1RcHjqDWWycYQGrz4KBYoGZSMmMuVpkmuS") 6 (Some "FA2 token")) ;
(Pair (Pair "tzBTC" "USDT") "KT1DG2g5DPYWqyHKGpRL579YkYZwJxibwaAZ" "BTC-USDT" 6 False) ;
Elt "tzBTC/USDtz"
(Pair (Pair "tzBTC" "USDtz") "KT1DG2g5DPYWqyHKGpRL579YkYZwJxibwaAZ" "BTC-USDT" 6 False) })
{ Elt "BTCtz"
(Pair 0 "BTCtz" (Some "KT1ErLEYVsxqHxLgLucXViq5DYrtSyDuSFTe") 8 (Some "FA2 token")) ;
Elt "EURL" (Pair 0 "EURL" (Some "KT1RcHjqDWWycYQGrz4KBYoGZSMmMuVpkmuS") 6 (Some "FA2 token")) ;
Elt "USDT" (Pair 0 "USDT" (Some "KT1WNrZ7pEbpmYBGPib1e7UVCeC6GA6TkJYR") 6 (Some "FA2 token")) ;
Elt "USDtz"
(Pair 0 "USDtz" (Some "KT1B8tP5Q8Cb7HctLfxt4MVk2cWouHFrnbjW") 6 (Some "FA1.2 token")) ;
Elt "tzBTC"
(Pair 0 "tzBTC" (Some "KT1P8RdJ5MfHMK5phKJ5JsfNfask5v2b2NQS") 8 (Some "FA1.2 token")) })

52 changes: 38 additions & 14 deletions batcher/batcher.mligo
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,7 @@ let collect_redemptions
let collect_redemption_payouts
(holder: address)
(fees: fees)
(specific_batch_opt:nat option)
(storage: Storage.t): (fees * user_batch_ordertypes * batch_set * token_amount_map) =
let fee_in_mutez = storage.fee_in_mutez in
let batch_set = storage.batch_set in
Expand All @@ -422,7 +423,12 @@ let collect_redemption_payouts
let empty_tam = (Map.empty : token_amount_map) in
match Big_map.find_opt holder ubots with
| None -> fees, ubots, batch_set, empty_tam
| Some bots -> let u_bots, u_tam, bs, _tkns, u_fees, _fim = Map.fold collect_redemptions bots (bots, empty_tam, batch_set, tokens, fees, fee_in_mutez) in
| Some bots -> let u_bots, u_tam, bs, _tkns, u_fees, _fim = match specific_batch_opt with
| Some sb -> (match Map.find_opt sb bots with
| None -> (bots, empty_tam, batch_set, tokens, fees, fee_in_mutez)
| Some opts -> collect_redemptions ((bots, empty_tam, batch_set, tokens, fees, fee_in_mutez),(sb,opts)))
| None -> Map.fold collect_redemptions bots (bots, empty_tam, batch_set, tokens, fees, fee_in_mutez)
in
let updated_ubots = Big_map.update holder (Some u_bots) ubots in
u_fees, updated_ubots, bs, u_tam

Expand Down Expand Up @@ -475,6 +481,25 @@ let deposit
let deposit_op = Utils.Treasury_Utils.handle_transfer deposit_address treasury_vault deposited_token in
[ deposit_op]

[@inline]
let redeem_by_batch
(redeem_address : address)
(batch_id: nat)
(storage : storage) : operation list * storage =
let treasury_vault = Utils.get_vault () in
let fees = {
to_send = 0mutez;
to_refund = 0mutez;
to_market_maker = 0mutez;
payer = redeem_address;
recipient = storage.fee_recipient;
market_maker = storage.marketmaker;
} in
let fees, updated_ubots, updated_batch_set, payout_token_map = Ubots.collect_redemption_payouts redeem_address fees (Some batch_id) storage in
let operations = Utils.Treasury_Utils.transfer_holdings treasury_vault redeem_address payout_token_map in
let operations = resolve_fees fees operations in
let updated_storage = { storage with user_batch_ordertypes = updated_ubots; batch_set = updated_batch_set; } in
(operations, updated_storage)

[@inline]
let redeem
Expand All @@ -489,7 +514,7 @@ let redeem
recipient = storage.fee_recipient;
market_maker = storage.marketmaker;
} in
let fees, updated_ubots, updated_batch_set, payout_token_map = Ubots.collect_redemption_payouts redeem_address fees storage in
let fees, updated_ubots, updated_batch_set, payout_token_map = Ubots.collect_redemption_payouts redeem_address fees None storage in
let operations = Utils.Treasury_Utils.transfer_holdings treasury_vault redeem_address payout_token_map in
let operations = resolve_fees fees operations in
let updated_storage = { storage with user_batch_ordertypes = updated_ubots; batch_set = updated_batch_set; } in
Expand Down Expand Up @@ -1025,6 +1050,7 @@ type entrypoint =
| Deposit of external_swap_order
| Tick of string
| Redeem
| RedeemByBatch of nat
| Cancel of pair
| Change_fee of tez
| Change_admin_address of address
Expand Down Expand Up @@ -1237,23 +1263,12 @@ let enforce_correct_side
| Sell ->
if swap.from.token.name = valid_swap.swap.to then () else failwith Errors.incorrect_side_specified

[@inline]
let enforce_correct_side
(order:external_swap_order)
(valid_swap:valid_swap) : unit =
let swap = order.swap in
let side = Utils.nat_to_side order.side in
match side with
| Buy ->
if swap.from.token.name = valid_swap.swap.from.token.name then () else failwith incorrect_side_specified
| Sell ->
if swap.from.token.name = valid_swap.swap.to.name then () else failwith incorrect_side_specified

(* Register a deposit during a valid (Open) deposit time; fails otherwise.
Updates the current_batch if the time is valid but the new batch was not initialized. *)
[@inline]
let deposit (external_order: external_swap_order) (storage : storage) : result =
let pair = Utils.pair_of_external_swap external_order storage.valid_tokens in
let pair = Utils.pair_of_external_swap external_order in
let current_time = Tezos.get_now () in
let pair_name = Utils.get_rate_name_from_pair pair in
let valid_swap = get_valid_swap_reduced pair_name storage in
Expand Down Expand Up @@ -1295,6 +1310,14 @@ let redeem
let (tokens_transfer_ops, new_storage) = Treasury.redeem holder storage in
(tokens_transfer_ops, new_storage)

[@inline]
let redeem_by_batch
(batch_id:nat)
(storage : storage) : result =
let holder = Tezos.get_sender () in
let () = Utils.reject_if_tez_supplied () in
let (tokens_transfer_ops, new_storage) = Treasury.redeem_by_batch holder batch_id storage in
(tokens_transfer_ops, new_storage)

[@inline]
let change_oracle_price_source
Expand Down Expand Up @@ -1502,6 +1525,7 @@ let main
(* User endpoints *)
| Deposit order -> deposit order storage
| Redeem -> redeem storage
| RedeemByBatch i -> redeem_by_batch i storage
| Cancel pair -> cancel pair storage
(* Maintenance endpoint *)
| Tick r -> tick r storage
Expand Down
Loading

0 comments on commit c77cc8a

Please sign in to comment.