Skip to content

Commit

Permalink
Revert "Add quantity_min_tick to DeepBook. Quantity must be divisible… (
Browse files Browse the repository at this point in the history
#17937)

…… (#17927)

… by this amount, rather than lot_size. (#17335)"

This reverts commit d2425d6.

Describe the changes or additions included in this PR.

How did you test the new or updated feature?

---

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol:
- [ ] Nodes (Validators and Full nodes):
- [ ] Indexer:
- [ ] JSON-RPC:
- [ ] GraphQL:
- [ ] CLI:
- [ ] Rust SDK:

---------

## Description 

Describe the changes or additions included in this PR.

## Test plan 

How did you test the new or updated feature?

---

## Release notes

Check each box that your changes affect. If none of the boxes relate to
your changes, release notes aren't required.

For each box you select, include information after the relevant heading
that describes the impact of your changes that a user might notice and
any actions they must take to implement updates.

- [ ] Protocol: 
- [ ] Nodes (Validators and Full nodes): 
- [ ] Indexer: 
- [ ] JSON-RPC: 
- [ ] GraphQL: 
- [ ] CLI: 
- [ ] Rust SDK:

---------

Co-authored-by: Timothy Zakian <[email protected]>
Co-authored-by: Mark Logan <[email protected]>
  • Loading branch information
3 people authored May 27, 2024
1 parent e3ace09 commit b0138f3
Show file tree
Hide file tree
Showing 12 changed files with 1,166 additions and 506 deletions.
1 change: 1 addition & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ jobs:
runs-on: [ubuntu-ghcloud]
env:
MSIM_WATCHDOG_TIMEOUT_MS: 60000
MSIM_TEST_SEED: 2
steps:
- uses: actions/checkout@b4ffde65f46336ab88eb53be808477a3936bae11 # Pin v4.1.1
- uses: taiki-e/install-action@nextest
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 @@ -2246,12 +2242,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 @@ -2266,7 +2262,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 @@ -2288,12 +2284,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 @@ -2308,7 +2304,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 @@ -2327,7 +2323,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 @@ -2344,7 +2340,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 @@ -3175,8 +3171,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

0 comments on commit b0138f3

Please sign in to comment.