From 8984d3d17f847506a42a943314dbc65b5609fafe Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Thu, 18 Apr 2024 11:10:30 -0400 Subject: [PATCH 1/7] refactor deposit --- deepbook/sources/pool/pool.move | 50 ++++++++++++++++++--------------- 1 file changed, 28 insertions(+), 22 deletions(-) diff --git a/deepbook/sources/pool/pool.move b/deepbook/sources/pool/pool.move index e4b13398..6c8e9949 100644 --- a/deepbook/sources/pool/pool.move +++ b/deepbook/sources/pool/pool.move @@ -179,20 +179,17 @@ module deepbook::pool { // TODO: make sure there is mul_down and mul_up for rounding let deep_quantity = mul(config.deep_per_base(), quantity); fee_quantity = mul(deep_quantity, maker_fee); - // Deposit the deepbook fees - self.deposit(account, fee_quantity, 2, ctx); + self.deposit_deep(account, fee_quantity, ctx); } - // If unverified pool + // If unverified pool, fees paid in base/quote assets else { fee_quantity = mul(quantity, maker_fee); // if q = 100, fee = 0.1, fee_q = 10 (in base assets) place_quantity = place_quantity - fee_quantity; // if q = 100, fee_q = 10, place_q = 90 (in base assets) if (is_bid) { fee_quantity = mul(fee_quantity, price); // if price = 5, fee_q = 50 (in quote assets) - // deposit quote asset fees - self.deposit(account, fee_quantity, 1, ctx); + self.deposit_quote(account, fee_quantity, ctx); } else { - // deposit base asset fees - self.deposit(account, fee_quantity, 0, ctx); + self.deposit_base(account, fee_quantity, ctx); }; }; @@ -535,26 +532,35 @@ module deepbook::pool { } /// This will be automatically called if not enough assets in settled_funds for a trade - /// User cannot manually deposit - /// Deposit BaseAsset, QuoteAsset, Deepbook Tokens - fun deposit( + /// User cannot manually deposit. Funds are withdrawn from user account and merged into pool balances. + fun deposit_base( self: &mut Pool, user_account: &mut Account, amount: u64, - coin_type: u64, // 0 for base, 1 for quote, 2 for deep. TODO: use enum ctx: &mut TxContext, ) { - // Withdraw from user account and merge into pool balances - if (coin_type == 0) { - let base = user_account.withdraw(amount, ctx); - self.base_balances.join(base.into_balance()); - } else if (coin_type == 1) { - let quote = user_account.withdraw(amount, ctx); - self.quote_balances.join(quote.into_balance()); - } else if (coin_type == 2){ - let coin = user_account.withdraw(amount, ctx); - self.deepbook_balance.join(coin.into_balance()); - } + let base = user_account.withdraw(amount, ctx); + self.base_balances.join(base.into_balance()); + } + + fun deposit_quote( + self: &mut Pool, + user_account: &mut Account, + amount: u64, + ctx: &mut TxContext, + ) { + let quote = user_account.withdraw(amount, ctx); + self.quote_balances.join(quote.into_balance()); + } + + fun deposit_deep( + self: &mut Pool, + user_account: &mut Account, + amount: u64, + ctx: &mut TxContext, + ) { + let coin = user_account.withdraw(amount, ctx); + self.deepbook_balance.join(coin.into_balance()); } fun withdraw_base( From 3ca10cb20632299c7ef32360a7a961a107a480c9 Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Thu, 18 Apr 2024 13:09:19 -0400 Subject: [PATCH 2/7] add in expire and clock partial --- deepbook/sources/deepbook.move | 14 ++++++++++++-- deepbook/sources/helper/utils.move | 6 +----- deepbook/sources/pool/pool.move | 16 +++++++++++----- 3 files changed, 24 insertions(+), 12 deletions(-) diff --git a/deepbook/sources/deepbook.move b/deepbook/sources/deepbook.move index 495fd4ac..277d5180 100644 --- a/deepbook/sources/deepbook.move +++ b/deepbook/sources/deepbook.move @@ -5,7 +5,8 @@ module deepbook::deepbook { use sui::{ balance::Balance, coin::Coin, - sui::SUI + sui::SUI, + clock::Clock, }; use deepbook::{ @@ -123,10 +124,19 @@ module deepbook::deepbook { price: u64, quantity: u64, is_bid: bool, + expire_timestamp: u64, // Expiration timestamp in ms + clock: &Clock, ctx: &mut TxContext, ) { pool.place_limit_order( - account, client_order_id, price, quantity, is_bid, ctx + account, + client_order_id, + price, + quantity, + is_bid, + expire_timestamp, + clock, + ctx, ); } diff --git a/deepbook/sources/helper/utils.move b/deepbook/sources/helper/utils.move index 37050c26..40d6972b 100644 --- a/deepbook/sources/helper/utils.move +++ b/deepbook/sources/helper/utils.move @@ -52,11 +52,7 @@ module deepbook::utils { i = i + 1 }; - if (len1 <= len2) { - return true - } else { - return false - } + (len1 <= len2) } /// Append two ASCII strings and return the result diff --git a/deepbook/sources/pool/pool.move b/deepbook/sources/pool/pool.move index 6c8e9949..7e9bd628 100644 --- a/deepbook/sources/pool/pool.move +++ b/deepbook/sources/pool/pool.move @@ -6,6 +6,7 @@ module deepbook::pool { balance::{Self,Balance}, table::{Self, Table}, coin::{Self, Coin, TreasuryCap}, + clock::Clock, sui::SUI, event, }; @@ -32,9 +33,10 @@ module deepbook::pool { const EInvalidLotSize: u64 = 4; const EInvalidMinSize: u64 = 5; const EUserNotFound: u64 = 6; - const EOrderInvalidTickSize: u64 = 7; + const EOrderInvalidPrice: u64 = 7; const EOrderBelowMinimumSize: u64 = 8; const EOrderInvalidLotSize: u64 = 9; + const EInvalidExpireTimestamp: u64 = 10; // <<<<<<<<<<<<<<<<<<<<<<<< Constants <<<<<<<<<<<<<<<<<<<<<<<< const POOL_CREATION_FEE: u64 = 100 * 1_000_000_000; // 100 SUI, can be updated @@ -150,7 +152,7 @@ module deepbook::pool { // <<<<<<<<<<<<<<<<<<<<<<<< Package Functions <<<<<<<<<<<<<<<<<<<<<<<< - /// Place a maker order + /// Place a limit order to the order book. public(package) fun place_limit_order( self: &mut Pool, account: &mut Account, @@ -158,15 +160,19 @@ module deepbook::pool { price: u64, quantity: u64, // in base asset is_bid: bool, // true for bid, false for ask + expire_timestamp: u64, // Expiration timestamp in ms + clock: &Clock, ctx: &mut TxContext, ): u64 { // Refresh state as necessary if first order of epoch self.refresh_state(ctx); - assert!(price % self.tick_size == 0, EOrderInvalidTickSize); + assert!(price > 0, EOrderInvalidPrice); + assert!(price % self.tick_size == 0, EOrderInvalidPrice); // Check quantity is above minimum quantity (in base asset) assert!(quantity >= self.min_size, EOrderBelowMinimumSize); assert!(quantity % self.lot_size == 0, EOrderInvalidLotSize); + assert!(expire_timestamp > clock.timestamp_ms(), EInvalidExpireTimestamp); let maker_fee = self.pool_state.maker_fee(); let mut fee_quantity; @@ -236,7 +242,7 @@ module deepbook::pool { }; }; - let order_id = self.place_maker_order_int(client_order_id, price, place_quantity, fee_quantity, is_bid, ctx); + let order_id = self.place_limit_order_int(client_order_id, price, place_quantity, fee_quantity, is_bid, ctx); event::emit(OrderPlaced { pool_id: *object::uid_as_inner(&self.id), order_id: 0, @@ -600,7 +606,7 @@ module deepbook::pool { } /// Balance accounting happens before this function is called - fun place_maker_order_int( + fun place_limit_order_int( self: &mut Pool, client_order_id: u64, price: u64, From 3740f8eb6c39bdde935d8ce804cf0ea4733b3d99 Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Thu, 18 Apr 2024 13:19:59 -0400 Subject: [PATCH 3/7] expire timestamp --- deepbook/sources/pool/pool.move | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/deepbook/sources/pool/pool.move b/deepbook/sources/pool/pool.move index 7e9bd628..b680fbfa 100644 --- a/deepbook/sources/pool/pool.move +++ b/deepbook/sources/pool/pool.move @@ -242,7 +242,7 @@ module deepbook::pool { }; }; - let order_id = self.place_limit_order_int(client_order_id, price, place_quantity, fee_quantity, is_bid, ctx); + let order_id = self.place_limit_order_int(client_order_id, price, place_quantity, fee_quantity, is_bid, expire_timestamp, ctx); event::emit(OrderPlaced { pool_id: *object::uid_as_inner(&self.id), order_id: 0, @@ -613,6 +613,7 @@ module deepbook::pool { quantity: u64, fee_quantity: u64, is_bid: bool, // true for bid, false for ask + expire_timestamp: u64, // Expiration timestamp in ms ctx: &TxContext, ): u64 { let order_id = self.next_bid_order_id; @@ -628,7 +629,7 @@ module deepbook::pool { fee_is_deep: self.fee_is_deep(), is_bid, owner: ctx.sender(), - expire_timestamp: 0, // TODO + expire_timestamp, self_matching_prevention: 0, // TODO }; From 1b09d5ddd26455856dd434e69746739fa52a0712 Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Thu, 18 Apr 2024 13:28:06 -0400 Subject: [PATCH 4/7] use clock --- deepbook/sources/deepbook.move | 4 ++-- deepbook/sources/state/state.move | 5 +++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/deepbook/sources/deepbook.move b/deepbook/sources/deepbook.move index 277d5180..ffc4505e 100644 --- a/deepbook/sources/deepbook.move +++ b/deepbook/sources/deepbook.move @@ -46,10 +46,10 @@ module deepbook::deepbook { state: &mut State, reference_pool: &Pool, pool: &mut Pool, - ctx: &TxContext, + clock: &Clock, ) { state.add_deep_price_point( - reference_pool, pool, ctx + reference_pool, pool, clock ); } diff --git a/deepbook/sources/state/state.move b/deepbook/sources/state/state.move index 85b04b23..46405d3b 100644 --- a/deepbook/sources/state/state.move +++ b/deepbook/sources/state/state.move @@ -9,6 +9,7 @@ module deepbook::state { table::Table, sui::SUI, coin::Coin, + clock::Clock, }; use deepbook::{ @@ -84,11 +85,11 @@ module deepbook::state { self: &State, reference_pool: &Pool, pool: &mut Pool, - ctx: &TxContext, + clock: &Clock, ) { let (base_conversion_rate, quote_conversion_rate) = self.deep_reference_pools .get_conversion_rates(reference_pool, pool); - let timestamp = ctx.epoch_timestamp_ms(); // TODO: Clock or Epoch? + let timestamp = clock.timestamp_ms(); pool.add_deep_price_point(base_conversion_rate, quote_conversion_rate, timestamp); } From d859e452bb07b115bfda43e5ecb6c22a2734516c Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Thu, 18 Apr 2024 15:02:24 -0400 Subject: [PATCH 5/7] updates --- deepbook/sources/pool/pool.move | 3 +++ 1 file changed, 3 insertions(+) diff --git a/deepbook/sources/pool/pool.move b/deepbook/sources/pool/pool.move index b680fbfa..90347a4c 100644 --- a/deepbook/sources/pool/pool.move +++ b/deepbook/sources/pool/pool.move @@ -8,6 +8,7 @@ module deepbook::pool { coin::{Self, Coin, TreasuryCap}, clock::Clock, sui::SUI, + linked_table::LinkedTable, event, }; @@ -135,6 +136,7 @@ module deepbook::pool { asks: BigVector, next_bid_order_id: u64, // increments for each bid order next_ask_order_id: u64, // increments for each ask order + user_open_orders: Table>, deep_config: Option, users: Table, @@ -415,6 +417,7 @@ module deepbook::pool { asks: big_vector::empty(10000, 1000, ctx), next_bid_order_id: 0, next_ask_order_id: 0, + user_open_orders: table::new(ctx), users: table::new(ctx), deep_config: option::none(), tick_size, From 540097fc56c51471ff65097fc4dcfae81250b30e Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Thu, 18 Apr 2024 15:09:10 -0400 Subject: [PATCH 6/7] small update --- benchmark/sources/pool.move | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/benchmark/sources/pool.move b/benchmark/sources/pool.move index b82393ba..abcb124b 100644 --- a/benchmark/sources/pool.move +++ b/benchmark/sources/pool.move @@ -40,7 +40,7 @@ module benchmark::pool { next_ask_order_id: 1000000, user_open_orders: table::new(ctx), }; - + transfer::share_object(pool); } @@ -105,18 +105,14 @@ module benchmark::pool { ctx: &mut TxContext, ): u128 { let owner = ctx.sender(); - let mut order_id: u128; + let order_id; let open_orders: &mut BigVector; if (is_bid) { - order_id = price as u128; - order_id = order_id << 64; - order_id = order_id + (pool.next_bid_order_id as u128); + order_id = encode_order_id(price, pool.next_bid_order_id); pool.next_bid_order_id = pool.next_bid_order_id + 1; open_orders = &mut pool.bids_bigvec; } else { - order_id = price as u128; - order_id = order_id << 64; - order_id = order_id + (pool.next_ask_order_id as u128); + order_id = encode_order_id(price, pool.next_ask_order_id); pool.next_ask_order_id = pool.next_ask_order_id + 1; open_orders = &mut pool.asks_bigvec; }; @@ -143,4 +139,11 @@ module benchmark::pool { order_id } -} \ No newline at end of file + + fun encode_order_id( + price: u64, + order_id: u64 + ): u128 { + ((price as u128) << 64)+ (order_id as u128) + } +} From 4c4843791c708e4bd2f63d74b95acd6732fffffd Mon Sep 17 00:00:00 2001 From: Tony Lee Date: Fri, 19 Apr 2024 08:38:46 -0400 Subject: [PATCH 7/7] small updates --- deepbook/sources/pool/pool.move | 3 --- 1 file changed, 3 deletions(-) diff --git a/deepbook/sources/pool/pool.move b/deepbook/sources/pool/pool.move index 90347a4c..b680fbfa 100644 --- a/deepbook/sources/pool/pool.move +++ b/deepbook/sources/pool/pool.move @@ -8,7 +8,6 @@ module deepbook::pool { coin::{Self, Coin, TreasuryCap}, clock::Clock, sui::SUI, - linked_table::LinkedTable, event, }; @@ -136,7 +135,6 @@ module deepbook::pool { asks: BigVector, next_bid_order_id: u64, // increments for each bid order next_ask_order_id: u64, // increments for each ask order - user_open_orders: Table>, deep_config: Option, users: Table, @@ -417,7 +415,6 @@ module deepbook::pool { asks: big_vector::empty(10000, 1000, ctx), next_bid_order_id: 0, next_ask_order_id: 0, - user_open_orders: table::new(ctx), users: table::new(ctx), deep_config: option::none(), tick_size,