Skip to content

Commit

Permalink
Run migration scripts on sui framework
Browse files Browse the repository at this point in the history
  • Loading branch information
cgswords committed Mar 12, 2024
1 parent 667d265 commit cafb075
Show file tree
Hide file tree
Showing 138 changed files with 1,754 additions and 1,750 deletions.
1 change: 1 addition & 0 deletions crates/sui-framework/packages/deepbook/Move.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
name = "DeepBook"
version = "0.0.1"
published-at = "0xdee9"
edition = "2024.beta"

[dependencies]
MoveStdlib = { local = "../move-stdlib" }
Expand Down
128 changes: 64 additions & 64 deletions crates/sui-framework/packages/deepbook/sources/clob.move

Large diffs are not rendered by default.

202 changes: 101 additions & 101 deletions crates/sui-framework/packages/deepbook/sources/clob_v2.move

Large diffs are not rendered by default.

34 changes: 17 additions & 17 deletions crates/sui-framework/packages/deepbook/sources/critbit.move
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ module deepbook::critbit {
const MAX_CAPACITY: u64 = 0x7FFFFFFFFFFFFFFF;
// <<<<<<<<<<<<<<<<<<<<<<<< Constants <<<<<<<<<<<<<<<<<<<<<<<<

struct Leaf<V> has store, drop {
public struct Leaf<V> has store, drop {
key: u64,
value: V,
parent: u64,
}

struct InternalNode has store, drop {
public struct InternalNode has store, drop {
mask: u64,
left_child: u64,
right_child: u64,
Expand All @@ -40,7 +40,7 @@ module deepbook::critbit {
}

// Leaves of the Critbit Tree are sorted in ascending order.
struct CritbitTree<V: store> has store {
public struct CritbitTree<V: store> has store {
root: u64,
internal_nodes: Table<u64, InternalNode>,
// A leaf contains orders at that price level.
Expand Down Expand Up @@ -91,10 +91,10 @@ module deepbook::critbit {
// Market sell orders consume liquidities by iterating through the leaves in descending order starting from the max leaf of the asks Critbit Tree.
// This function provides the iterator for this procedure.
public fun previous_leaf<V: store>(tree: &CritbitTree<V>, key: u64): (u64, u64) {
let (_, index) = find_leaf(tree, key);
let (_, mut index) = find_leaf(tree, key);
assert!(index != PARTITION_INDEX, ELeafNotExist);
let ptr = MAX_U64 - index;
let parent = table::borrow(&tree.leaves, index).parent;
let mut ptr = MAX_U64 - index;
let mut parent = table::borrow(&tree.leaves, index).parent;
while (parent != PARTITION_INDEX && is_left_child(tree, parent, ptr)){
ptr = parent;
parent = table::borrow(&tree.internal_nodes, ptr).parent;
Expand All @@ -111,10 +111,10 @@ module deepbook::critbit {
// Market buy orders consume liquidities by iterating through the leaves in ascending order starting from the min leaf of the asks Critbit Tree.
// This function provides the iterator for this procedure.
public fun next_leaf<V: store>(tree: &CritbitTree<V>, key: u64): (u64, u64) {
let (_, index) = find_leaf(tree, key);
let (_, mut index) = find_leaf(tree, key);
assert!(index != PARTITION_INDEX, ELeafNotExist);
let ptr = MAX_U64 - index;
let parent = table::borrow(&tree.leaves, index).parent;
let mut ptr = MAX_U64 - index;
let mut parent = table::borrow(&tree.leaves, index).parent;
while (parent != PARTITION_INDEX && !is_left_child(tree, parent, ptr)){
ptr = parent;
parent = table::borrow(&tree.internal_nodes, ptr).parent;
Expand All @@ -128,15 +128,15 @@ module deepbook::critbit {
}

fun left_most_leaf<V: store>(tree: &CritbitTree<V>, root: u64): u64 {
let ptr = root;
let mut ptr = root;
while (ptr < PARTITION_INDEX){
ptr = table::borrow(& tree.internal_nodes, ptr).left_child;
};
ptr
}

fun right_most_leaf<V: store>(tree: &CritbitTree<V>, root: u64): u64 {
let ptr = root;
let mut ptr = root;
while (ptr < PARTITION_INDEX){
ptr = table::borrow(& tree.internal_nodes, ptr).right_child;
};
Expand Down Expand Up @@ -185,8 +185,8 @@ module deepbook::critbit {
tree.next_internal_node_index = tree.next_internal_node_index + 1;
table::add(&mut tree.internal_nodes, new_internal_node_index, new_internal_node);

let ptr = tree.root;
let new_internal_node_parent_index = PARTITION_INDEX;
let mut ptr = tree.root;
let mut new_internal_node_parent_index = PARTITION_INDEX;
// Search position of the new internal node
while (ptr < PARTITION_INDEX) {
let internal_node = table::borrow(&tree.internal_nodes, ptr);
Expand Down Expand Up @@ -261,7 +261,7 @@ module deepbook::critbit {
tree.max_leaf = index;
};

let is_left_child_;
let mut is_left_child_;
let Leaf<V> {key: _, value, parent: removed_leaf_parent_index} = table::remove(&mut tree.leaves, index);

if (size(tree) == 0) {
Expand Down Expand Up @@ -352,7 +352,7 @@ module deepbook::critbit {

// function for internal usage
fun get_closest_leaf_index_by_key<V: store>(tree: &CritbitTree<V>, key: u64): u64 {
let ptr = tree.root;
let mut ptr = tree.root;
// if tree is empty, return the patrition index
if(ptr == PARTITION_INDEX) return PARTITION_INDEX;
while (ptr < PARTITION_INDEX){
Expand Down Expand Up @@ -427,8 +427,8 @@ module deepbook::critbit {
};
let internal_node_from_tree = &tree.internal_nodes;
let leaves_from_tree = &tree.leaves;
let i = 0;
let is_equal = true;
let mut i = 0;
let mut is_equal = true;
while (i < vector::length(internal_node_keys)) {
let key = *vector::borrow(internal_node_keys, i);
if (table::borrow(internal_node_from_tree, key) != vector::borrow(internal_node, i)) {
Expand Down
16 changes: 8 additions & 8 deletions crates/sui-framework/packages/deepbook/sources/custodian.move
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@ module deepbook::custodian {
const EUserBalanceDoesNotExist: u64 = 1;
// <<<<<<<<<<<<<<<<<<<<<<<< Error codes <<<<<<<<<<<<<<<<<<<<<<<<

struct Account<phantom T> has store {
public struct Account<phantom T> has store {
available_balance: Balance<T>,
locked_balance: Balance<T>,
}

struct AccountCap has key, store { id: UID }
public struct AccountCap has key, store { id: UID }

// Custodian for limit orders.
struct Custodian<phantom T> has key, store {
public struct Custodian<phantom T> has key, store {
id: UID,
/// Map from an AccountCap object ID to an Account object
account_balances: Table<ID, Account<T>>,
Expand Down Expand Up @@ -174,7 +174,7 @@ module deepbook::custodian {
const ENull: u64 = 0;

#[test_only]
struct USD {}
public struct USD {}

#[test_only]
public(friend) fun assert_user_balance<T>(
Expand Down Expand Up @@ -218,7 +218,7 @@ module deepbook::custodian {
fun test_user_balance_does_not_exist(){
let owner: address = @0xAAAA;
let bob: address = @0xBBBB;
let test = test_scenario::begin(owner);
let mut test = test_scenario::begin(owner);
test_scenario::next_tx(&mut test, owner);
{
setup_test(&mut test);
Expand All @@ -241,7 +241,7 @@ module deepbook::custodian {
fun test_account_balance() {
let owner: address = @0xAAAA;
let bob: address = @0xBBBB;
let test = test_scenario::begin(owner);
let mut test = test_scenario::begin(owner);
test_scenario::next_tx(&mut test, owner);
{
setup_test(&mut test);
Expand All @@ -261,11 +261,11 @@ module deepbook::custodian {
};
test_scenario::next_tx(&mut test, bob);
{
let custodian = take_shared<Custodian<USD>>(&test);
let mut custodian = take_shared<Custodian<USD>>(&test);
let account_cap = take_from_sender<AccountCap>(&test);
let account_cap_user = object::id(&account_cap);
deposit(&mut custodian, mint_for_testing<USD>(10000, ctx(&mut test)), account_cap_user);
let (asset_available, asset_locked) = account_balance(&custodian, account_cap_user);
let (asset_available, mut asset_locked) = account_balance(&custodian, account_cap_user);
assert_eq(asset_available, 10000);
assert_eq(asset_locked, 0);
asset_locked = account_locked_balance(&custodian, account_cap_user);
Expand Down
22 changes: 11 additions & 11 deletions crates/sui-framework/packages/deepbook/sources/custodian_v2.move
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module deepbook::custodian_v2 {
const EAdminAccountCapRequired: u64 = 2;
// <<<<<<<<<<<<<<<<<<<<<<<< Error codes <<<<<<<<<<<<<<<<<<<<<<<<

struct Account<phantom T> has store {
public struct Account<phantom T> has store {
available_balance: Balance<T>,
locked_balance: Balance<T>,
}
Expand All @@ -26,15 +26,15 @@ module deepbook::custodian_v2 {
/// the permission to both access funds and create new `AccountCap`s.
/// Calling `create_child_account_cap` creates a "child account cap" such that id != owner
/// that can access funds, but cannot create new `AccountCap`s.
struct AccountCap has key, store {
public struct AccountCap has key, store {
id: UID,
/// The owner of this AccountCap. Note: this is
/// derived from an object ID, not a user address
owner: address
}

// Custodian for limit orders.
struct Custodian<phantom T> has key, store {
public struct Custodian<phantom T> has key, store {
id: UID,
/// Map from the owner address of AccountCap object to an Account object
account_balances: Table<address, Account<T>>,
Expand Down Expand Up @@ -216,7 +216,7 @@ module deepbook::custodian_v2 {
const ENull: u64 = 0;

#[test_only]
struct USD {}
public struct USD {}

#[test_only]
public(friend) fun assert_user_balance<T>(
Expand Down Expand Up @@ -260,7 +260,7 @@ module deepbook::custodian_v2 {
fun test_user_balance_does_not_exist(){
let owner: address = @0xAAAA;
let bob: address = @0xBBBB;
let test = test_scenario::begin(owner);
let mut test = test_scenario::begin(owner);
test_scenario::next_tx(&mut test, owner);
{
setup_test(&mut test);
Expand All @@ -282,7 +282,7 @@ module deepbook::custodian_v2 {
fun test_account_balance() {
let owner: address = @0xAAAA;
let bob: address = @0xBBBB;
let test = test_scenario::begin(owner);
let mut test = test_scenario::begin(owner);
test_scenario::next_tx(&mut test, owner);
{
setup_test(&mut test);
Expand All @@ -301,10 +301,10 @@ module deepbook::custodian_v2 {
};
test_scenario::next_tx(&mut test, bob);
{
let custodian = take_shared<Custodian<USD>>(&test);
let mut custodian = take_shared<Custodian<USD>>(&test);
let account_cap = take_from_sender<AccountCap>(&test);
deposit(&mut custodian, mint_for_testing<USD>(10000, ctx(&mut test)), bob);
let (asset_available, asset_locked) = account_balance(&custodian, bob);
let (asset_available, mut asset_locked) = account_balance(&custodian, bob);
assert_eq(asset_available, 10000);
assert_eq(asset_locked, 0);
asset_locked = account_locked_balance(&custodian, bob);
Expand All @@ -317,15 +317,15 @@ module deepbook::custodian_v2 {

#[test]
fun test_create_child_account_cap() {
let ctx = tx_context::dummy();
let mut ctx = tx_context::dummy();
let admin_cap = mint_account_cap(&mut ctx);
// check that we can duplicate child cap, and don't get another admin cap
let child_cap = create_child_account_cap(&admin_cap, &mut ctx);
assert_eq(child_cap.owner, admin_cap.owner);
assert!(&child_cap.id != &admin_cap.id, 0);

// check that both child and admin cap can access the funds
let custodian = new<USD>(&mut ctx);
let mut custodian = new<USD>(&mut ctx);
increase_user_available_balance(&mut custodian, account_owner(&admin_cap), balance::create_for_testing(10000));
let coin = decrease_user_available_balance(&mut custodian, &child_cap, 10000);

Expand All @@ -339,7 +339,7 @@ module deepbook::custodian_v2 {
#[test]
fun test_cant_create_with_child() {
// a child cap cannot create an account cap
let ctx = tx_context::dummy();
let mut ctx = tx_context::dummy();
let admin_cap = mint_account_cap(&mut ctx);
// check that we can duplicate child cap, and don't get another admin cap
let child_cap1 = create_child_account_cap(&admin_cap, &mut ctx);
Expand Down
20 changes: 10 additions & 10 deletions crates/sui-framework/packages/deepbook/sources/math.move
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module deepbook::math {
public(friend) fun unsafe_mul_round(x: u64, y: u64): (bool, u64) {
let x = (x as u128);
let y = (y as u128);
let is_round_down = true;
let mut is_round_down = true;
if ((x * y) % FLOAT_SCALING_U128 == 0) is_round_down = false;
(is_round_down, ((x * y / FLOAT_SCALING_U128) as u64))
}
Expand Down Expand Up @@ -57,7 +57,7 @@ module deepbook::math {
public(friend) fun unsafe_div_round(x: u64, y: u64): (bool, u64) {
let x = (x as u128);
let y = (y as u128);
let is_round_down = true;
let mut is_round_down = true;
if ((x * (FLOAT_SCALING as u128) % y) == 0) is_round_down = false;
(is_round_down, ((x * (FLOAT_SCALING as u128) / y) as u64))
}
Expand All @@ -70,11 +70,11 @@ module deepbook::math {
(is_round_down, result)
}

public(friend) fun count_leading_zeros(x: u128): u8 {
public(friend) fun count_leading_zeros(mut x: u128): u8 {
if (x == 0) {
128
} else {
let n: u8 = 0;
let mut n: u8 = 0;
if (x & 0xFFFFFFFFFFFFFFFF0000000000000000 == 0) {
// x's higher 64 is all zero, shift the lower part over
x = x << 64;
Expand Down Expand Up @@ -116,8 +116,8 @@ module deepbook::math {
#[test_only] use sui::test_utils::assert_eq;

#[test_only]
fun pow(base: u128, exponent: u8): u128 {
let res: u128 = 1;
fun pow(mut base: u128, mut exponent: u8): u128 {
let mut res: u128 = 1;
while (exponent >= 1) {
if (exponent % 2 == 0) {
base = base * base;
Expand All @@ -132,7 +132,7 @@ module deepbook::math {

#[test]
fun test_count_leading_zeros() {
let i: u8 = 0;
let mut i: u8 = 0;
while (i <= 127) {
assert_eq(count_leading_zeros((pow(2, i) as u128)), 128 - i - 1);
i = i + 1;
Expand Down Expand Up @@ -166,7 +166,7 @@ module deepbook::math {

#[test]
fun test_mul_round() {
let (is_round, result) = unsafe_mul_round(1_000_000_000, 1);
let (mut is_round, mut result) = unsafe_mul_round(1_000_000_000, 1);
assert_eq(is_round, false);
assert_eq(result, 1);
(is_round, result) = unsafe_mul_round(9_999_999_999, 1);
Expand All @@ -179,7 +179,7 @@ module deepbook::math {

#[test]
fun test_div() {
let (is_round, result) = unsafe_div_round(1, 1_000_000_000);
let (mut is_round, mut result) = unsafe_div_round(1, 1_000_000_000);
assert_eq(is_round, false);
assert_eq(result, 1);
(is_round, result) = unsafe_div_round(1, 9_999_999_999);
Expand All @@ -192,7 +192,7 @@ module deepbook::math {

#[test]
fun test_div_round() {
let (is_round, result) = unsafe_div_round(1, 1_000_000_000);
let (mut is_round, mut result) = unsafe_div_round(1, 1_000_000_000);
assert_eq(is_round, false);
assert_eq(result, 1);
(is_round, result) = unsafe_div_round(1, 9_999_999_999);
Expand Down
Loading

0 comments on commit cafb075

Please sign in to comment.