Skip to content

Commit

Permalink
cherry-pick of d06b0dd
Browse files Browse the repository at this point in the history
  • Loading branch information
sblackshear committed Jun 13, 2023
1 parent b1eb6ef commit 434eb19
Show file tree
Hide file tree
Showing 6 changed files with 180 additions and 42 deletions.
16 changes: 8 additions & 8 deletions crates/sui-framework/docs/clob_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Emitted when a maker order is injected into the order book.
<code>owner: <b>address</b></code>
</dt>
<dd>
object ID of the <code>AccountCap</code> that placed the order
owner ID of the <code>AccountCap</code> that placed the order
</dd>
<dt>
<code>original_quantity: u64</code>
Expand Down Expand Up @@ -251,7 +251,7 @@ Emitted when a maker order is canceled.
<code>owner: <b>address</b></code>
</dt>
<dd>
owner address of the <code>AccountCap</code> that placed the order
owner ID of the <code>AccountCap</code> that canceled the order
</dd>
<dt>
<code>original_quantity: u64</code>
Expand Down Expand Up @@ -327,13 +327,13 @@ Emitted only when a maker order is filled.
<code>taker_address: <b>address</b></code>
</dt>
<dd>
address of <code>AccountCap</code> that filled the order
owner ID of the <code>AccountCap</code> that filled the order
</dd>
<dt>
<code>maker_address: <b>address</b></code>
</dt>
<dd>
address of <code>AccountCap</code> that placed the order, also as "maker_address"
owner ID of the <code>AccountCap</code> that placed the order
</dd>
<dt>
<code>original_quantity: u64</code>
Expand Down Expand Up @@ -449,7 +449,7 @@ Emitted when user withdraw asset from custodian
<code>owner: <b>address</b></code>
</dt>
<dd>
owner address of the <code>AccountCap</code> that withdraw the asset
owner ID of the <code>AccountCap</code> that withdrew the asset
</dd>
</dl>

Expand Down Expand Up @@ -512,7 +512,7 @@ Emitted when user withdraw asset from custodian
<code>owner: <b>address</b></code>
</dt>
<dd>

Order can only be canceled by the <code>AccountCap</code> with this owner ID
</dd>
<dt>
<code>expire_timestamp: u64</code>
Expand Down Expand Up @@ -2126,15 +2126,15 @@ So please check that boolean value first before using the order id.
<b>assert</b>!(price % pool.tick_size == 0, <a href="clob_v2.md#0xdee9_clob_v2_EInvalidPrice">EInvalidPrice</a>);
<b>assert</b>!(quantity % pool.lot_size == 0, <a href="clob_v2.md#0xdee9_clob_v2_EInvalidQuantity">EInvalidQuantity</a>);
<b>assert</b>!(expire_timestamp &gt; <a href="../../../.././build/Sui/docs/clock.md#0x2_clock_timestamp_ms">clock::timestamp_ms</a>(<a href="../../../.././build/Sui/docs/clock.md#0x2_clock">clock</a>), <a href="clob_v2.md#0xdee9_clob_v2_EInvalidExpireTimestamp">EInvalidExpireTimestamp</a>);
<b>let</b> owner = sender(ctx);
<b>let</b> owner = account_owner(account_cap);
<b>let</b> original_quantity = quantity;
<b>let</b> base_quantity_filled;
<b>let</b> quote_quantity_filled;

<b>if</b> (is_bid) {
<b>let</b> quote_quantity_original = <a href="custodian.md#0xdee9_custodian_account_available_balance">custodian::account_available_balance</a>&lt;QuoteAsset&gt;(
&pool.quote_custodian,
owner,
owner
);
<b>let</b> quote_balance = <a href="custodian.md#0xdee9_custodian_decrease_user_available_balance">custodian::decrease_user_available_balance</a>&lt;QuoteAsset&gt;(
&<b>mut</b> pool.quote_custodian,
Expand Down
83 changes: 79 additions & 4 deletions crates/sui-framework/docs/custodian_v2.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
- [Resource `Custodian`](#0xdee9_custodian_v2_Custodian)
- [Constants](#@Constants_0)
- [Function `mint_account_cap`](#0xdee9_custodian_v2_mint_account_cap)
- [Function `create_child_account_cap`](#0xdee9_custodian_v2_create_child_account_cap)
- [Function `delete_account_cap`](#0xdee9_custodian_v2_delete_account_cap)
- [Function `account_owner`](#0xdee9_custodian_v2_account_owner)
- [Function `account_balance`](#0xdee9_custodian_v2_account_balance)
- [Function `new`](#0xdee9_custodian_v2_new)
Expand Down Expand Up @@ -72,6 +74,11 @@

## Resource `AccountCap`

Capability granting permission to access an entry in <code><a href="custodian_v2.md#0xdee9_custodian_v2_Custodian">Custodian</a>.account_balances</code>.
Calling <code>mint_account_cap</code> creates an "admin account cap" such that id == owner with
the permission to both access funds and create new <code><a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">AccountCap</a></code>s.
Calling <code>create_child_account_cap</code> creates a "child account cap" such that id != owner
that can access funds, but cannot create new <code><a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">AccountCap</a></code>s.


<pre><code><b>struct</b> <a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">AccountCap</a> <b>has</b> store, key
Expand All @@ -94,7 +101,8 @@
<code>owner: <b>address</b></code>
</dt>
<dd>

The owner of this AccountCap. Note: this is
derived from an object ID, not a user address
</dd>
</dl>

Expand Down Expand Up @@ -148,11 +156,21 @@



<a name="0xdee9_custodian_v2_EAdminAccountCapRequired"></a>



<pre><code><b>const</b> <a href="custodian_v2.md#0xdee9_custodian_v2_EAdminAccountCapRequired">EAdminAccountCapRequired</a>: u64 = 2;
</code></pre>



<a name="0xdee9_custodian_v2_mint_account_cap"></a>

## Function `mint_account_cap`

Create an <code><a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">AccountCap</a></code> that can be used across all DeepBook pool
Create an admin <code><a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">AccountCap</a></code> that can be used across all DeepBook pools, and has
the permission to create new <code><a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">AccountCap</a></code>s that can access the same source of funds


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="custodian_v2.md#0xdee9_custodian_v2_mint_account_cap">mint_account_cap</a>(ctx: &<b>mut</b> <a href="../../../.././build/Sui/docs/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>): <a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">custodian_v2::AccountCap</a>
Expand All @@ -165,22 +183,79 @@ Create an <code><a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">Account


<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="custodian_v2.md#0xdee9_custodian_v2_mint_account_cap">mint_account_cap</a>(ctx: &<b>mut</b> TxContext): <a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">AccountCap</a> {
<b>let</b> id = <a href="../../../.././build/Sui/docs/object.md#0x2_object_new">object::new</a>(ctx);
<b>let</b> owner = <a href="../../../.././build/Sui/docs/object.md#0x2_object_uid_to_address">object::uid_to_address</a>(&id);
<a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">AccountCap</a> { id, owner }
}
</code></pre>



</details>

<a name="0xdee9_custodian_v2_create_child_account_cap"></a>

## Function `create_child_account_cap`

Create a "child account cap" such that id != owner
that can access funds, but cannot create new <code><a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">AccountCap</a></code>s.


<pre><code><b>public</b> <b>fun</b> <a href="custodian_v2.md#0xdee9_custodian_v2_create_child_account_cap">create_child_account_cap</a>(admin_account_cap: &<a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">custodian_v2::AccountCap</a>, ctx: &<b>mut</b> <a href="../../../.././build/Sui/docs/tx_context.md#0x2_tx_context_TxContext">tx_context::TxContext</a>): <a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">custodian_v2::AccountCap</a>
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="custodian_v2.md#0xdee9_custodian_v2_create_child_account_cap">create_child_account_cap</a>(admin_account_cap: &<a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">AccountCap</a>, ctx: &<b>mut</b> TxContext): <a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">AccountCap</a> {
// Only the admin account cap can create new account caps
<b>assert</b>!(<a href="../../../.././build/Sui/docs/object.md#0x2_object_uid_to_address">object::uid_to_address</a>(&admin_account_cap.id) == admin_account_cap.owner, <a href="custodian_v2.md#0xdee9_custodian_v2_EAdminAccountCapRequired">EAdminAccountCapRequired</a>);

<a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">AccountCap</a> {
id: <a href="../../../.././build/Sui/docs/object.md#0x2_object_new">object::new</a>(ctx),
owner: sender(ctx)
owner: admin_account_cap.owner
}
}
</code></pre>



</details>

<a name="0xdee9_custodian_v2_delete_account_cap"></a>

## Function `delete_account_cap`

Destroy the given <code>account_cap</code> object


<pre><code><b>public</b> <b>fun</b> <a href="custodian_v2.md#0xdee9_custodian_v2_delete_account_cap">delete_account_cap</a>(account_cap: <a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">custodian_v2::AccountCap</a>)
</code></pre>



<details>
<summary>Implementation</summary>


<pre><code><b>public</b> <b>fun</b> <a href="custodian_v2.md#0xdee9_custodian_v2_delete_account_cap">delete_account_cap</a>(account_cap: <a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">AccountCap</a>) {
<b>let</b> <a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">AccountCap</a> { id, owner: _ } = account_cap;
<a href="../../../.././build/Sui/docs/object.md#0x2_object_delete">object::delete</a>(id)
}
</code></pre>



</details>

<a name="0xdee9_custodian_v2_account_owner"></a>

## Function `account_owner`

Return the owner address of an AccountCap
Return the owner of an AccountCap


<pre><code><b>public</b> <b>fun</b> <a href="custodian_v2.md#0xdee9_custodian_v2_account_owner">account_owner</a>(account_cap: &<a href="custodian_v2.md#0xdee9_custodian_v2_AccountCap">custodian_v2::AccountCap</a>): <b>address</b>
Expand Down
20 changes: 10 additions & 10 deletions crates/sui-framework/packages/deepbook/sources/clob_v2.move
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module deepbook::clob_v2 {
use sui::sui::SUI;
use sui::table::{Self, Table, contains, add, borrow_mut};
use sui::transfer;
use sui::tx_context::{TxContext, sender};
use sui::tx_context::TxContext;

use deepbook::critbit::{Self, CritbitTree, is_empty, borrow_mut_leaf_by_index, min_leaf, remove_leaf_by_index, max_leaf, next_leaf, previous_leaf, borrow_leaf_by_index, borrow_leaf_by_key, find_leaf, insert_leaf};
use deepbook::custodian_v2::{Self as custodian, Custodian, AccountCap, mint_account_cap, account_owner};
Expand Down Expand Up @@ -104,7 +104,7 @@ module deepbook::clob_v2 {
/// ID of the order defined by client
client_order_id: u64,
is_bid: bool,
/// object ID of the `AccountCap` that placed the order
/// owner ID of the `AccountCap` that placed the order
owner: address,
original_quantity: u64,
base_asset_quantity_placed: u64,
Expand All @@ -121,7 +121,7 @@ module deepbook::clob_v2 {
/// ID of the order defined by client
client_order_id: u64,
is_bid: bool,
/// owner address of the `AccountCap` that placed the order
/// owner ID of the `AccountCap` that canceled the order
owner: address,
original_quantity: u64,
base_asset_quantity_canceled: u64,
Expand All @@ -139,9 +139,9 @@ module deepbook::clob_v2 {
/// ID of the order defined by maker client
maker_client_order_id: u64,
is_bid: bool,
/// address of `AccountCap` that filled the order
/// owner ID of the `AccountCap` that filled the order
taker_address: address,
/// address of `AccountCap` that placed the order, also as "maker_address"
/// owner ID of the `AccountCap` that placed the order
maker_address: address,
original_quantity: u64,
base_asset_quantity_filled: u64,
Expand All @@ -167,7 +167,7 @@ module deepbook::clob_v2 {
pool_id: ID,
/// quantity of the asset user withdrew
quantity: u64,
/// owner address of the `AccountCap` that withdraw the asset
/// owner ID of the `AccountCap` that withdrew the asset
owner: address
}
// <<<<<<<<<<<<<<<<<<<<<<<< Events <<<<<<<<<<<<<<<<<<<<<<<<
Expand All @@ -187,7 +187,7 @@ module deepbook::clob_v2 {
// quantity of the order currently held
quantity: u64,
is_bid: bool,
// Order can only be cancelled by the owner.
/// Order can only be canceled by the `AccountCap` with this owner ID
owner: address,
// Expiration timestamp in ms.
expire_timestamp: u64,
Expand All @@ -212,7 +212,7 @@ module deepbook::clob_v2 {
next_bid_order_id: u64,
// Order id of the next ask order, starting from 1<<63.
next_ask_order_id: u64,
// Map from owner of account_cap -> (map from order id -> order price)
// Map from AccountCap owner ID -> (map from order id -> order price)
usr_open_orders: Table<address, LinkedTable<u64, u64>>,
// taker_fee_rate should be strictly greater than maker_rebate_rate.
// The difference between taker_fee_rate and maker_rabate_rate goes to the protocol.
Expand Down Expand Up @@ -1015,15 +1015,15 @@ module deepbook::clob_v2 {
assert!(price % pool.tick_size == 0, EInvalidPrice);
assert!(quantity % pool.lot_size == 0, EInvalidQuantity);
assert!(expire_timestamp > clock::timestamp_ms(clock), EInvalidExpireTimestamp);
let owner = sender(ctx);
let owner = account_owner(account_cap);
let original_quantity = quantity;
let base_quantity_filled;
let quote_quantity_filled;

if (is_bid) {
let quote_quantity_original = custodian::account_available_balance<QuoteAsset>(
&pool.quote_custodian,
owner,
owner
);
let quote_balance = custodian::decrease_user_available_balance<QuoteAsset>(
&mut pool.quote_custodian,
Expand Down
Loading

0 comments on commit 434eb19

Please sign in to comment.