Skip to content

Commit a98bd8c

Browse files
authored
[Orderbook] Implement dead man's switch support for the orderbook (#18263)
1 parent fa8b0cc commit a98bd8c

28 files changed

Lines changed: 2965 additions & 556 deletions

aptos-move/framework/aptos-experimental/doc/bulk_order_book_types.md

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ ask_sizes
109109
- [Arguments:](#@Arguments:_21)
110110
- [Returns:](#@Returns:_22)
111111
- [Function `get_sequence_number`](#0x7_bulk_order_book_types_get_sequence_number)
112+
- [Function `get_creation_time_micros`](#0x7_bulk_order_book_types_get_creation_time_micros)
112113
- [Function `get_active_price`](#0x7_bulk_order_book_types_get_active_price)
113114
- [Arguments:](#@Arguments:_23)
114115
- [Returns:](#@Returns:_24)
@@ -129,6 +130,7 @@ ask_sizes
129130

130131

131132
<pre><code><b>use</b> <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option">0x1::option</a>;
133+
<b>use</b> <a href="../../aptos-framework/doc/timestamp.md#0x1_timestamp">0x1::timestamp</a>;
132134
<b>use</b> <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">0x1::vector</a>;
133135
<b>use</b> <a href="order_book_types.md#0x7_order_book_types">0x7::order_book_types</a>;
134136
</code></pre>
@@ -303,6 +305,12 @@ both original and remaining sizes for tracking purposes.
303305
</dt>
304306
<dd>
305307

308+
</dd>
309+
<dt>
310+
<code>creation_time_micros: u64</code>
311+
</dt>
312+
<dd>
313+
306314
</dd>
307315
<dt>
308316
<code>bid_prices: <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;</code>
@@ -587,6 +595,7 @@ A tuple containing:
587595
best_ask_price: Option&lt;u64&gt;,
588596
): (<a href="bulk_order_book_types.md#0x7_bulk_order_book_types_BulkOrder">BulkOrder</a>&lt;M&gt;, <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;, <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;, <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;, <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;) {
589597
<b>let</b> BulkOrderRequest::V1 { <a href="../../aptos-framework/doc/account.md#0x1_account">account</a>, order_sequence_number, bid_prices, bid_sizes, ask_prices, ask_sizes, metadata } = order_req;
598+
<b>let</b> creation_time_micros = <a href="../../aptos-framework/doc/timestamp.md#0x1_timestamp_now_microseconds">timestamp::now_microseconds</a>();
590599
<b>let</b> bid_price_crossing_idx = <a href="bulk_order_book_types.md#0x7_bulk_order_book_types_discard_price_crossing_levels">discard_price_crossing_levels</a>(&bid_prices, best_ask_price, <b>true</b>);
591600
<b>let</b> ask_price_crossing_idx = <a href="bulk_order_book_types.md#0x7_bulk_order_book_types_discard_price_crossing_levels">discard_price_crossing_levels</a>(&ask_prices, best_bid_price, <b>false</b>);
592601

@@ -610,6 +619,7 @@ A tuple containing:
610619
<a href="../../aptos-framework/doc/account.md#0x1_account">account</a>,
611620
unique_priority_idx,
612621
order_sequence_number,
622+
creation_time_micros,
613623
bid_prices: post_only_bid_prices,
614624
bid_sizes: post_only_bid_sizes,
615625
ask_prices: post_only_ask_prices,
@@ -1020,6 +1030,7 @@ Validates that prices are in the correct order (descending for bids, ascending f
10201030
remaining_size,
10211031
is_bid,
10221032
order.order_sequence_number,
1033+
order.creation_time_micros,
10231034
order.metadata,
10241035
),
10251036
matched_size
@@ -1209,6 +1220,32 @@ The account that placed the order.
12091220

12101221

12111222

1223+
</details>
1224+
1225+
<a id="0x7_bulk_order_book_types_get_creation_time_micros"></a>
1226+
1227+
## Function `get_creation_time_micros`
1228+
1229+
1230+
1231+
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="bulk_order_book_types.md#0x7_bulk_order_book_types_get_creation_time_micros">get_creation_time_micros</a>&lt;M: <b>copy</b>, drop, store&gt;(self: &<a href="bulk_order_book_types.md#0x7_bulk_order_book_types_BulkOrder">bulk_order_book_types::BulkOrder</a>&lt;M&gt;): u64
1232+
</code></pre>
1233+
1234+
1235+
1236+
<details>
1237+
<summary>Implementation</summary>
1238+
1239+
1240+
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="bulk_order_book_types.md#0x7_bulk_order_book_types_get_creation_time_micros">get_creation_time_micros</a>&lt;M: store + <b>copy</b> + drop&gt;(
1241+
self: &<a href="bulk_order_book_types.md#0x7_bulk_order_book_types_BulkOrder">BulkOrder</a>&lt;M&gt;,
1242+
): u64 {
1243+
self.creation_time_micros
1244+
}
1245+
</code></pre>
1246+
1247+
1248+
12121249
</details>
12131250

12141251
<a id="0x7_bulk_order_book_types_get_active_price"></a>
@@ -1544,7 +1581,7 @@ Sets the bulk order to empty state by clearing all sizes.
15441581

15451582

15461583

1547-
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="bulk_order_book_types.md#0x7_bulk_order_book_types_destroy_bulk_order">destroy_bulk_order</a>&lt;M: <b>copy</b>, drop, store&gt;(self: <a href="bulk_order_book_types.md#0x7_bulk_order_book_types_BulkOrder">bulk_order_book_types::BulkOrder</a>&lt;M&gt;): (<a href="order_book_types.md#0x7_order_book_types_OrderIdType">order_book_types::OrderIdType</a>, <b>address</b>, <a href="order_book_types.md#0x7_order_book_types_UniqueIdxType">order_book_types::UniqueIdxType</a>, u64, <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;, <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;, <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;, <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;, M)
1584+
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="bulk_order_book_types.md#0x7_bulk_order_book_types_destroy_bulk_order">destroy_bulk_order</a>&lt;M: <b>copy</b>, drop, store&gt;(self: <a href="bulk_order_book_types.md#0x7_bulk_order_book_types_BulkOrder">bulk_order_book_types::BulkOrder</a>&lt;M&gt;): (<a href="order_book_types.md#0x7_order_book_types_OrderIdType">order_book_types::OrderIdType</a>, <b>address</b>, <a href="order_book_types.md#0x7_order_book_types_UniqueIdxType">order_book_types::UniqueIdxType</a>, u64, u64, <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;, <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;, <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;, <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;, M)
15481585
</code></pre>
15491586

15501587

@@ -1560,6 +1597,7 @@ Sets the bulk order to empty state by clearing all sizes.
15601597
<b>address</b>,
15611598
UniqueIdxType,
15621599
u64,
1600+
u64,
15631601
<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;,
15641602
<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;,
15651603
<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/vector.md#0x1_vector">vector</a>&lt;u64&gt;,
@@ -1571,6 +1609,7 @@ Sets the bulk order to empty state by clearing all sizes.
15711609
<a href="../../aptos-framework/doc/account.md#0x1_account">account</a>,
15721610
unique_priority_idx,
15731611
order_sequence_number,
1612+
creation_time_micros,
15741613
bid_prices,
15751614
bid_sizes,
15761615
ask_prices,
@@ -1582,6 +1621,7 @@ Sets the bulk order to empty state by clearing all sizes.
15821621
<a href="../../aptos-framework/doc/account.md#0x1_account">account</a>,
15831622
unique_priority_idx,
15841623
order_sequence_number,
1624+
creation_time_micros,
15851625
bid_prices,
15861626
bid_sizes,
15871627
ask_prices,

aptos-move/framework/aptos-experimental/doc/market_bulk_order.md

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ Returns:
108108
);
109109
<b>let</b> response = market.get_order_book_mut().<a href="market_bulk_order.md#0x7_market_bulk_order_place_bulk_order">place_bulk_order</a>(request);
110110
<b>let</b> (bulk_order, cancelled_bid_prices, cancelled_bid_sizes, cancelled_ask_prices, cancelled_ask_sizes, previous_seq_num_option) = destroy_bulk_order_place_response(response);
111-
<b>let</b> (order_id, _, _, order_sequence_number, bid_prices, bid_sizes, ask_prices, ask_sizes, _ ) = bulk_order.destroy_bulk_order(); // We don't need <b>to</b> keep the bulk order <b>struct</b> after placement
111+
<b>let</b> (order_id, _, _, order_sequence_number, _, bid_prices, bid_sizes, ask_prices, ask_sizes, _ ) = bulk_order.destroy_bulk_order(); // We don't need <b>to</b> keep the bulk order <b>struct</b> after placement
112112
<b>assert</b>!(sequence_number == order_sequence_number, <a href="market_bulk_order.md#0x7_market_bulk_order_E_SEQUENCE_NUMBER_MISMATCH">E_SEQUENCE_NUMBER_MISMATCH</a>);
113113
// Extract previous_seq_num from <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/option.md#0x1_option">option</a>, defaulting <b>to</b> 0 <b>if</b> none
114114
<b>let</b> previous_seq_num = previous_seq_num_option.destroy_with_default(0);
@@ -146,10 +146,11 @@ for the specified user account.
146146
Parameters:
147147
- market: The market instance
148148
- user: The signer of the user whose bulk orders should be cancelled
149+
- cancellation_reason: The reason for cancelling the bulk order
149150
- callbacks: The market clearinghouse callbacks for cleanup operations
150151

151152

152-
<pre><code><b>public</b> <b>fun</b> <a href="market_bulk_order.md#0x7_market_bulk_order_cancel_bulk_order">cancel_bulk_order</a>&lt;M: <b>copy</b>, drop, store, R: <b>copy</b>, drop, store&gt;(market: &<b>mut</b> <a href="market_types.md#0x7_market_types_Market">market_types::Market</a>&lt;M&gt;, user: &<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, callbacks: &<a href="market_types.md#0x7_market_types_MarketClearinghouseCallbacks">market_types::MarketClearinghouseCallbacks</a>&lt;M, R&gt;)
153+
<pre><code><b>public</b> <b>fun</b> <a href="market_bulk_order.md#0x7_market_bulk_order_cancel_bulk_order">cancel_bulk_order</a>&lt;M: <b>copy</b>, drop, store, R: <b>copy</b>, drop, store&gt;(market: &<b>mut</b> <a href="market_types.md#0x7_market_types_Market">market_types::Market</a>&lt;M&gt;, user: &<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>, cancellation_reason: <a href="market_types.md#0x7_market_types_OrderCancellationReason">market_types::OrderCancellationReason</a>, callbacks: &<a href="market_types.md#0x7_market_types_MarketClearinghouseCallbacks">market_types::MarketClearinghouseCallbacks</a>&lt;M, R&gt;)
153154
</code></pre>
154155

155156

@@ -161,10 +162,11 @@ Parameters:
161162
<pre><code><b>public</b> <b>fun</b> <a href="market_bulk_order.md#0x7_market_bulk_order_cancel_bulk_order">cancel_bulk_order</a>&lt;M: store + <b>copy</b> + drop, R: store + <b>copy</b> + drop&gt;(
162163
market: &<b>mut</b> Market&lt;M&gt;,
163164
user: &<a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer">signer</a>,
165+
cancellation_reason: <a href="market_types.md#0x7_market_types_OrderCancellationReason">market_types::OrderCancellationReason</a>,
164166
callbacks: &MarketClearinghouseCallbacks&lt;M, R&gt;
165167
) {
166168
<b>let</b> <a href="../../aptos-framework/doc/account.md#0x1_account">account</a> = <a href="../../aptos-framework/../aptos-stdlib/../move-stdlib/doc/signer.md#0x1_signer_address_of">signer::address_of</a>(user);
167-
<a href="market_bulk_order.md#0x7_market_bulk_order_cancel_bulk_order_internal">cancel_bulk_order_internal</a>(market, <a href="../../aptos-framework/doc/account.md#0x1_account">account</a>, callbacks);
169+
<a href="market_bulk_order.md#0x7_market_bulk_order_cancel_bulk_order_internal">cancel_bulk_order_internal</a>(market, <a href="../../aptos-framework/doc/account.md#0x1_account">account</a>, cancellation_reason, callbacks);
168170
}
169171
</code></pre>
170172

@@ -178,7 +180,7 @@ Parameters:
178180

179181

180182

181-
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="market_bulk_order.md#0x7_market_bulk_order_cancel_bulk_order_internal">cancel_bulk_order_internal</a>&lt;M: <b>copy</b>, drop, store, R: <b>copy</b>, drop, store&gt;(market: &<b>mut</b> <a href="market_types.md#0x7_market_types_Market">market_types::Market</a>&lt;M&gt;, user: <b>address</b>, callbacks: &<a href="market_types.md#0x7_market_types_MarketClearinghouseCallbacks">market_types::MarketClearinghouseCallbacks</a>&lt;M, R&gt;)
183+
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="market_bulk_order.md#0x7_market_bulk_order_cancel_bulk_order_internal">cancel_bulk_order_internal</a>&lt;M: <b>copy</b>, drop, store, R: <b>copy</b>, drop, store&gt;(market: &<b>mut</b> <a href="market_types.md#0x7_market_types_Market">market_types::Market</a>&lt;M&gt;, user: <b>address</b>, cancellation_reason: <a href="market_types.md#0x7_market_types_OrderCancellationReason">market_types::OrderCancellationReason</a>, callbacks: &<a href="market_types.md#0x7_market_types_MarketClearinghouseCallbacks">market_types::MarketClearinghouseCallbacks</a>&lt;M, R&gt;)
182184
</code></pre>
183185

184186

@@ -190,10 +192,11 @@ Parameters:
190192
<pre><code><b>public</b>(<b>friend</b>) <b>fun</b> <a href="market_bulk_order.md#0x7_market_bulk_order_cancel_bulk_order_internal">cancel_bulk_order_internal</a>&lt;M: store + <b>copy</b> + drop, R: store + <b>copy</b> + drop&gt;(
191193
market: &<b>mut</b> Market&lt;M&gt;,
192194
user: <b>address</b>,
195+
cancellation_reason: <a href="market_types.md#0x7_market_types_OrderCancellationReason">market_types::OrderCancellationReason</a>,
193196
callbacks: &MarketClearinghouseCallbacks&lt;M, R&gt;
194197
) {
195198
<b>let</b> cancelled_bulk_order = market.get_order_book_mut().<a href="market_bulk_order.md#0x7_market_bulk_order_cancel_bulk_order">cancel_bulk_order</a>(user);
196-
<b>let</b> (order_id, _, _, sequence_number, bid_prices, bid_sizes, ask_prices, ask_sizes, _ ) = cancelled_bulk_order.destroy_bulk_order();
199+
<b>let</b> (order_id, _, _, sequence_number, _, bid_prices, bid_sizes, ask_prices, ask_sizes, _ ) = cancelled_bulk_order.destroy_bulk_order();
197200
<b>let</b> i = 0;
198201
<b>while</b> (i &lt; bid_sizes.length()) {
199202
callbacks.cleanup_bulk_order_at_price(user, order_id, <b>true</b>, bid_prices[i], bid_sizes[i]);
@@ -211,7 +214,8 @@ Parameters:
211214
bid_prices,
212215
bid_sizes,
213216
ask_prices,
214-
ask_sizes
217+
ask_sizes,
218+
std::option::some(cancellation_reason)
215219
);
216220
}
217221
</code></pre>

0 commit comments

Comments
 (0)