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

Revert "Add quantity_min_tick to DeepBook. Quantity must be divisible… #17936

Merged
merged 4 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Binary file not shown.
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/sui-framework-snapshot/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@
]
},
"47": {
"git_revision": "5a7780b95d20",
"git_revision": "b0138f3e7f21",
"package_ids": [
"0x0000000000000000000000000000000000000000000000000000000000000001",
"0x0000000000000000000000000000000000000000000000000000000000000002",
Expand Down
69 changes: 29 additions & 40 deletions crates/sui-framework/docs/deepbook/clob_v2.md

Large diffs are not rendered by default.

70 changes: 33 additions & 37 deletions crates/sui-framework/packages/deepbook/sources/clob_v2.move
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ module deepbook::clob_v2 {
const EInvalidPair: u64 = 16;
const EInvalidFee: u64 = 18;
const EInvalidExpireTimestamp: u64 = 19;
const EInvalidTickSizeMinSize: u64 = 20;
const EInvalidTickSizeLotSize: u64 = 20;
const EInvalidSelfMatchingPreventionArg: u64 = 21;

// <<<<<<<<<<<<<<<<<<<<<<<< Error codes <<<<<<<<<<<<<<<<<<<<<<<<
Expand All @@ -58,8 +58,6 @@ module deepbook::clob_v2 {
const MIN_ASK_ORDER_ID: u64 = 1 << 63;
const MIN_PRICE: u64 = 0;
const MAX_PRICE: u64 = (1u128 << 64 - 1) as u64;
// Trade quantities must be in multiples of 1000. The lot_size in the pool structs is used as min_size.
const LOT_SIZE: u64 = 1000;
#[test_only]
const TIMESTAMP_INF: u64 = (1u128 << 64 - 1) as u64;
const REFERENCE_TAKER_FEE_RATE: u64 = 2_500_000;
Expand All @@ -82,7 +80,7 @@ module deepbook::clob_v2 {
// 10^9 scaling
maker_rebate_rate: u64,
tick_size: u64,
lot_size: u64, // lot_size in this context is the minimum trade size.
lot_size: u64,
}

/// Emitted when a maker order is injected into the order book.
Expand Down Expand Up @@ -253,7 +251,7 @@ module deepbook::clob_v2 {
// 10^9 scaling
maker_rebate_rate: u64,
tick_size: u64,
lot_size: u64, // lot_size in this context is the minimum trade size.
lot_size: u64,
// other pool info
base_custodian: Custodian<BaseAsset>,
quote_custodian: Custodian<QuoteAsset>,
Expand Down Expand Up @@ -344,15 +342,15 @@ module deepbook::clob_v2 {
taker_fee_rate: u64,
maker_rebate_rate: u64,
tick_size: u64,
min_size: u64,
lot_size: u64,
creation_fee: Balance<SUI>,
ctx: &mut TxContext,
) {
let (pool, pool_owner_cap) = create_pool_with_return_<BaseAsset, QuoteAsset>(
taker_fee_rate,
maker_rebate_rate,
tick_size,
min_size,
lot_size,
creation_fee,
ctx
);
Expand All @@ -363,13 +361,13 @@ module deepbook::clob_v2 {

public fun create_pool<BaseAsset, QuoteAsset>(
tick_size: u64,
min_size: u64,
lot_size: u64,
creation_fee: Coin<SUI>,
ctx: &mut TxContext,
) {
create_customized_pool<BaseAsset, QuoteAsset>(
tick_size,
min_size,
lot_size,
REFERENCE_TAKER_FEE_RATE,
REFERENCE_MAKER_REBATE_RATE,
creation_fee,
Expand All @@ -382,7 +380,7 @@ module deepbook::clob_v2 {
/// Taker_fee_rate of 0.25% should be 2_500_000 for example
public fun create_customized_pool<BaseAsset, QuoteAsset>(
tick_size: u64,
min_size: u64,
lot_size: u64,
taker_fee_rate: u64,
maker_rebate_rate: u64,
creation_fee: Coin<SUI>,
Expand All @@ -392,7 +390,7 @@ module deepbook::clob_v2 {
taker_fee_rate,
maker_rebate_rate,
tick_size,
min_size,
lot_size,
coin::into_balance(creation_fee),
ctx
)
Expand All @@ -403,7 +401,7 @@ module deepbook::clob_v2 {
taker_fee_rate: u64,
maker_rebate_rate: u64,
tick_size: u64,
min_size: u64,
lot_size: u64,
creation_fee: Balance<SUI>,
ctx: &mut TxContext,
): (Pool<BaseAsset, QuoteAsset>, PoolOwnerCap) {
Expand All @@ -412,7 +410,7 @@ module deepbook::clob_v2 {
let base_type_name = type_name::get<BaseAsset>();
let quote_type_name = type_name::get<QuoteAsset>();

assert!(clob_math::unsafe_mul(min_size, tick_size) > 0, EInvalidTickSizeMinSize);
assert!(clob_math::unsafe_mul(lot_size, tick_size) > 0, EInvalidTickSizeLotSize);
assert!(base_type_name != quote_type_name, EInvalidPair);
assert!(taker_fee_rate >= maker_rebate_rate, EInvalidFeeRateRebateRate);

Expand All @@ -431,7 +429,7 @@ module deepbook::clob_v2 {
taker_fee_rate,
maker_rebate_rate,
tick_size,
lot_size: min_size,
lot_size,
});
(Pool<BaseAsset, QuoteAsset> {
id: pool_uid,
Expand All @@ -443,7 +441,7 @@ module deepbook::clob_v2 {
taker_fee_rate,
maker_rebate_rate,
tick_size,
lot_size: min_size,
lot_size,
base_custodian: custodian::new<BaseAsset>(ctx),
quote_custodian: custodian::new<QuoteAsset>(ctx),
creation_fee,
Expand All @@ -455,13 +453,13 @@ module deepbook::clob_v2 {
/// Function for creating an external pool. This API can be used to wrap deepbook pools into other objects.
public fun create_pool_with_return<BaseAsset, QuoteAsset>(
tick_size: u64,
min_size: u64,
lot_size: u64,
creation_fee: Coin<SUI>,
ctx: &mut TxContext,
): Pool<BaseAsset, QuoteAsset> {
create_customized_pool_with_return<BaseAsset, QuoteAsset>(
tick_size,
min_size,
lot_size,
REFERENCE_TAKER_FEE_RATE,
REFERENCE_MAKER_REBATE_RATE,
creation_fee,
Expand All @@ -475,7 +473,7 @@ module deepbook::clob_v2 {
/// Taker_fee_rate of 0.25% should be 2_500_000 for example
public fun create_customized_pool_with_return<BaseAsset, QuoteAsset>(
tick_size: u64,
min_size: u64,
lot_size: u64,
taker_fee_rate: u64,
maker_rebate_rate: u64,
creation_fee: Coin<SUI>,
Expand All @@ -485,7 +483,7 @@ module deepbook::clob_v2 {
taker_fee_rate,
maker_rebate_rate,
tick_size,
min_size,
lot_size,
coin::into_balance(creation_fee),
ctx
);
Expand All @@ -498,7 +496,7 @@ module deepbook::clob_v2 {
/// so with this function.
public fun create_customized_pool_v2<BaseAsset, QuoteAsset>(
tick_size: u64,
min_size: u64,
lot_size: u64,
taker_fee_rate: u64,
maker_rebate_rate: u64,
creation_fee: Coin<SUI>,
Expand All @@ -508,7 +506,7 @@ module deepbook::clob_v2 {
taker_fee_rate,
maker_rebate_rate,
tick_size,
min_size,
lot_size,
coin::into_balance(creation_fee),
ctx
)
Expand Down Expand Up @@ -776,8 +774,8 @@ module deepbook::clob_v2 {
filled_quote_quantity_without_commission,
maker_order.price
);
let filled_base_lot = filled_base_quantity / LOT_SIZE;
filled_base_quantity = filled_base_lot * LOT_SIZE;
let filled_base_lot = filled_base_quantity / pool.lot_size;
filled_base_quantity = filled_base_lot * pool.lot_size;
// filled_quote_quantity_without_commission = 0 is permitted here since filled_base_quantity could be 0
filled_quote_quantity_without_commission = clob_math::unsafe_mul(
filled_base_quantity,
Expand Down Expand Up @@ -1300,8 +1298,7 @@ module deepbook::clob_v2 {
// We start with the bid PriceLevel with the highest price by calling max_leaf on the bids Critbit Tree.
// The inner loop for iterating over the open orders in ascending orders of order id is the same as above.
// Then iterate over the price levels in descending order until the market order is completely filled.
let min_size = pool.lot_size;
assert!(quantity >= min_size && quantity % LOT_SIZE == 0, EInvalidQuantity);
assert!(quantity % pool.lot_size == 0, EInvalidQuantity);
assert!(quantity != 0, EInvalidQuantity);
let metadata;
if (is_bid) {
Expand Down Expand Up @@ -1512,8 +1509,7 @@ module deepbook::clob_v2 {
assert!(quantity > 0, EInvalidQuantity);
assert!(price > 0, EInvalidPrice);
assert!(price % pool.tick_size == 0, EInvalidPrice);
let min_size = pool.lot_size;
assert!(quantity >= min_size && quantity % LOT_SIZE == 0, EInvalidQuantity);
assert!(quantity % pool.lot_size == 0, EInvalidQuantity);
assert!(expire_timestamp > clock::timestamp_ms(clock), EInvalidExpireTimestamp);
let owner = account_owner(account_cap);
let original_quantity = quantity;
Expand Down Expand Up @@ -2244,12 +2240,12 @@ module deepbook::clob_v2 {
#[test_only] public struct USD {}

#[test_only]
public fun setup_test_with_tick_min(
public fun setup_test_with_tick_lot(
taker_fee_rate: u64,
maker_rebate_rate: u64,
// tick size with scaling
tick_size: u64,
min_size: u64,
lot_size: u64,
scenario: &mut Scenario,
sender: address,
) {
Expand All @@ -2264,7 +2260,7 @@ module deepbook::clob_v2 {
taker_fee_rate,
maker_rebate_rate,
tick_size,
min_size,
lot_size,
balance::create_for_testing(FEE_AMOUNT_FOR_CREATE_POOL),
test_scenario::ctx(scenario)
);
Expand All @@ -2286,12 +2282,12 @@ module deepbook::clob_v2 {
}

#[test_only]
public fun setup_test_with_tick_min_and_wrapped_pool(
public fun setup_test_with_tick_lot_and_wrapped_pool(
taker_fee_rate: u64,
maker_rebate_rate: u64,
// tick size with scaling
tick_size: u64,
min_size: u64,
lot_size: u64,
scenario: &mut Scenario,
sender: address,
) {
Expand All @@ -2306,7 +2302,7 @@ module deepbook::clob_v2 {
taker_fee_rate,
maker_rebate_rate,
tick_size,
min_size,
lot_size,
balance::create_for_testing(FEE_AMOUNT_FOR_CREATE_POOL),
test_scenario::ctx(scenario)
);
Expand All @@ -2325,7 +2321,7 @@ module deepbook::clob_v2 {
scenario: &mut Scenario,
sender: address,
) {
setup_test_with_tick_min(
setup_test_with_tick_lot(
taker_fee_rate,
maker_rebate_rate,
1 * FLOAT_SCALING,
Expand All @@ -2342,7 +2338,7 @@ module deepbook::clob_v2 {
scenario: &mut Scenario,
sender: address,
) {
setup_test_with_tick_min_and_wrapped_pool(
setup_test_with_tick_lot_and_wrapped_pool(
taker_fee_rate,
maker_rebate_rate,
1 * FLOAT_SCALING,
Expand Down Expand Up @@ -3173,8 +3169,8 @@ module deepbook::clob_v2 {
}

#[test]
#[expected_failure(abort_code = EInvalidTickSizeMinSize)]
fun test_create_pool_invalid_tick_size_min_size() {
#[expected_failure(abort_code = EInvalidTickSizeLotSize)]
fun test_create_pool_invalid_tick_size_lot_size() {
let owner: address = @0xAAAA;
let mut test = test_scenario::begin(owner);
test_scenario::next_tx(&mut test, owner);
Expand Down
Loading
Loading