Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor Improvements #33

Merged
merged 7 commits into from
Apr 19, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 12 additions & 9 deletions benchmark/sources/pool.move
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ module benchmark::pool {
next_ask_order_id: 1000000,
user_open_orders: table::new(ctx),
};

transfer::share_object(pool);
}

Expand Down Expand Up @@ -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<Order>;
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;
};
Expand All @@ -143,4 +139,11 @@ module benchmark::pool {

order_id
}
}

fun encode_order_id(
price: u64,
order_id: u64
): u128 {
leecchh marked this conversation as resolved.
Show resolved Hide resolved
((price as u128) << 64)+ (order_id as u128)
}
}
18 changes: 14 additions & 4 deletions deepbook/sources/deepbook.move
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ module deepbook::deepbook {
use sui::{
balance::Balance,
coin::Coin,
sui::SUI
sui::SUI,
clock::Clock,
};

use deepbook::{
Expand Down Expand Up @@ -45,10 +46,10 @@ module deepbook::deepbook {
state: &mut State,
reference_pool: &Pool<BaseAsset, QuoteAsset>,
pool: &mut Pool<BaseAsset, QuoteAsset>,
ctx: &TxContext,
clock: &Clock,
) {
state.add_deep_price_point<BaseAsset, QuoteAsset>(
reference_pool, pool, ctx
reference_pool, pool, clock
);
}

Expand Down Expand Up @@ -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,
);
}

Expand Down
6 changes: 1 addition & 5 deletions deepbook/sources/helper/utils.move
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
72 changes: 44 additions & 28 deletions deepbook/sources/pool/pool.move
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ module deepbook::pool {
balance::{Self,Balance},
table::{Self, Table},
coin::{Self, Coin, TreasuryCap},
clock::Clock,
sui::SUI,
linked_table::LinkedTable,
event,
};

Expand All @@ -32,9 +34,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
Expand Down Expand Up @@ -133,6 +136,7 @@ module deepbook::pool {
asks: BigVector<Order>,
next_bid_order_id: u64, // increments for each bid order
next_ask_order_id: u64, // increments for each ask order
user_open_orders: Table<address, LinkedTable<u128, u128>>,
leecchh marked this conversation as resolved.
Show resolved Hide resolved
deep_config: Option<DeepPrice>,
users: Table<address, User>,

Expand All @@ -150,23 +154,27 @@ module deepbook::pool {

// <<<<<<<<<<<<<<<<<<<<<<<< Package Functions <<<<<<<<<<<<<<<<<<<<<<<<

/// Place a maker order
/// Place a limit order to the order book.
public(package) fun place_limit_order<BaseAsset, QuoteAsset>(
self: &mut Pool<BaseAsset, QuoteAsset>,
account: &mut Account,
client_order_id: u64,
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;
Expand All @@ -179,20 +187,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);
};
};

Expand Down Expand Up @@ -239,7 +244,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, expire_timestamp, ctx);
event::emit(OrderPlaced<BaseAsset, QuoteAsset> {
pool_id: *object::uid_as_inner(&self.id),
order_id: 0,
Expand Down Expand Up @@ -412,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,
Expand Down Expand Up @@ -535,26 +541,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<BaseAsset, QuoteAsset>(
/// User cannot manually deposit. Funds are withdrawn from user account and merged into pool balances.
fun deposit_base<BaseAsset, QuoteAsset>(
self: &mut Pool<BaseAsset, QuoteAsset>,
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<BaseAsset, QuoteAsset>(
self: &mut Pool<BaseAsset, QuoteAsset>,
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<BaseAsset, QuoteAsset>(
self: &mut Pool<BaseAsset, QuoteAsset>,
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<BaseAsset, QuoteAsset>(
Expand Down Expand Up @@ -594,13 +609,14 @@ module deepbook::pool {
}

/// Balance accounting happens before this function is called
fun place_maker_order_int<BaseAsset, QuoteAsset>(
fun place_limit_order_int<BaseAsset, QuoteAsset>(
self: &mut Pool<BaseAsset, QuoteAsset>,
client_order_id: u64,
price: u64,
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;
Expand All @@ -616,7 +632,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
};

Expand Down
5 changes: 3 additions & 2 deletions deepbook/sources/state/state.move
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module deepbook::state {
table::Table,
sui::SUI,
coin::Coin,
clock::Clock,
};

use deepbook::{
Expand Down Expand Up @@ -84,11 +85,11 @@ module deepbook::state {
self: &State,
reference_pool: &Pool<BaseAsset, QuoteAsset>,
pool: &mut Pool<BaseAsset, QuoteAsset>,
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);
}

Expand Down
Loading