Skip to content
Open
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
9 changes: 9 additions & 0 deletions crates/api/external-api/src/http/admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ pub const ADMIN_MATCHING_POOL_DESTROY_ROUTE: &str =
/// Route to create an order in a specific matching pool
pub const ADMIN_CREATE_ORDER_IN_POOL_ROUTE: &str =
"/v2/relayer-admin/account/:account_id/orders/create-order-in-pool";
/// Route to assign an order to a matching pool
pub const ADMIN_ASSIGN_ORDER_TO_POOL_ROUTE: &str = "/v2/relayer-admin/orders/:order_id/assign-pool";

// -------------------
// | Request/Response |
Expand All @@ -52,3 +54,10 @@ pub struct GetDisabledAssetsResponse {
/// The list of disabled asset tickers
pub disabled_assets: Vec<String>,
}

/// The request to assign an order to a matching pool
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct AssignOrderToPoolRequest {
/// The matching pool to assign the order to
pub matching_pool: String,
}
8 changes: 8 additions & 0 deletions crates/api/external-api/src/types/admin.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
//! API types for admin requests

use circuit_types::Amount;
use serde::{Deserialize, Serialize};
use uuid::Uuid;

use crate::serde_helpers;

use super::order::{ApiOrder, OrderAuth};

// -------------------
Expand All @@ -18,6 +21,11 @@ pub struct ApiAdminOrder {
pub account_id: Uuid,
/// The matching pool the order is in
pub matching_pool: String,
/// The matchable amount for this order: min(amount_in, backing_balance).
/// This represents how much of the order can actually be filled given
/// the account's current balance state.
#[serde(with = "serde_helpers::amount_as_string")]
pub matchable_amount: Amount,
}

/// Response for admin get orders request
Expand Down
57 changes: 38 additions & 19 deletions crates/state/src/applicator/account_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::HashSet;

use alloy::sol_types::SolValue;
use alloy_primitives::{Address, keccak256};
use circuit_types::Amount;
use matching_engine_core::MatchingEngine;
use renegade_solidity_abi::v2::IDarkpoolV2::PublicIntentPermit;
use system_bus::{
Expand Down Expand Up @@ -128,7 +129,13 @@ impl StateApplicator {
}

// Publish admin order update event
self.publish_admin_order_update(account_id, order, pool, AdminOrderUpdateType::Created);
self.publish_admin_order_update(
account_id,
order,
pool,
AdminOrderUpdateType::Created,
matchable_amount,
);
Ok(ApplicatorReturnType::None)
}

Expand Down Expand Up @@ -183,8 +190,14 @@ impl StateApplicator {
// Remove from the matching engine
self.matching_engine().cancel_order(&order, pool.clone());

// Publish admin order update event
self.publish_admin_order_update(account_id, &order, pool, AdminOrderUpdateType::Cancelled);
// Publish admin order update event (cancelled orders have zero matchable amount)
self.publish_admin_order_update(
account_id,
&order,
pool,
AdminOrderUpdateType::Cancelled,
0, // matchable_amount
);
Ok(ApplicatorReturnType::None)
}

Expand Down Expand Up @@ -219,7 +232,13 @@ impl StateApplicator {
}

// Publish admin order update event
self.publish_admin_order_update(account_id, order, pool, AdminOrderUpdateType::Updated);
self.publish_admin_order_update(
account_id,
order,
pool,
AdminOrderUpdateType::Updated,
matchable_amount,
);
Ok(ApplicatorReturnType::None)
}

Expand Down Expand Up @@ -302,30 +321,27 @@ impl StateApplicator {
// Check if the order exists
let order_exists = tx.get_order(&order.id)?.is_some();

// Write the order auth and assign to the matching pool first so
// matchable_amount is accurate
tx.write_order_auth(&order.id, auth)?;

let pool = matching_pool.clone();
if order_exists {
tx.update_order(&account_id, order)?;
self.publish_admin_order_update(
account_id,
order,
pool,
AdminOrderUpdateType::Updated,
);
} else {
tx.add_order(&account_id, order)?;
self.publish_admin_order_update(
account_id,
order,
pool,
AdminOrderUpdateType::Created,
);
}

// Write the order auth
tx.write_order_auth(&order.id, auth)?;

// Assign to the matching pool
tx.assign_order_to_matching_pool(&order.id, matching_pool)?;

let matchable_amount = tx.get_order_matchable_amount(&order.id)?.unwrap_or_default();
let update_type = if order_exists {
AdminOrderUpdateType::Updated
} else {
AdminOrderUpdateType::Created
};
self.publish_admin_order_update(account_id, order, pool, update_type, matchable_amount);
}

tx.commit()?;
Expand All @@ -342,6 +358,7 @@ impl StateApplicator {
&order,
matching_pool,
AdminOrderUpdateType::Cancelled,
0, // matchable_amount
);
}

Expand Down Expand Up @@ -371,12 +388,14 @@ impl StateApplicator {
order: &Order,
matching_pool: String,
update_type: AdminOrderUpdateType,
matchable_amount: Amount,
) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: if AdminOrderUpdateType is Cancelled and matchable_amount != 0, emit an error.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or, have matchable_amount be an Option<Amount>, for more precise semantics.

let msg = SystemBusMessage::AdminOrderUpdate {
account_id,
order: Box::new(order.clone()),
matching_pool,
update_type,
matchable_amount,
};
self.system_bus().publish(ADMIN_ORDER_UPDATES_TOPIC.to_string(), msg);
}
Expand Down
22 changes: 14 additions & 8 deletions crates/state/src/interface/account_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,21 @@ impl StateInner {
.await
}

/// Get all orders for an account with their matching pool
/// Get all orders for an account with their matching pool and matchable
/// amount
pub async fn get_account_orders_with_matching_pool(
&self,
account_id: &AccountId,
) -> Result<Vec<(Order, MatchingPoolName)>, StateError> {
) -> Result<Vec<(Order, MatchingPoolName, Amount)>, StateError> {
let account_id = *account_id;
self.with_read_tx(move |tx| {
let orders = tx.get_account_orders(&account_id)?;
let mut result = Vec::with_capacity(orders.len());
for order in orders {
let matching_pool = tx.get_matching_pool_for_order(&order.id)?;
result.push((order, matching_pool));
let matchable_amount =
tx.get_order_matchable_amount(&order.id)?.unwrap_or_default();
result.push((order, matching_pool, matchable_amount));
}
Ok(result)
})
Expand All @@ -244,15 +247,16 @@ impl StateInner {
.await
}

/// Get all orders across all accounts with their matching pool
/// Get all orders across all accounts with their matching pool and
/// matchable amount
///
/// Returns a vector of tuples containing (AccountId, Order,
/// MatchingPoolName) for each order in the state.
/// Returns a vector of tuples containing (Order, AccountId,
/// MatchingPoolName, Amount) for each order in the state.
///
/// Warning: this can be slow when the state has many orders
pub async fn get_all_orders_with_matching_pool(
&self,
) -> Result<Vec<(Order, AccountId, MatchingPoolName)>, StateError> {
) -> Result<Vec<(Order, AccountId, MatchingPoolName, Amount)>, StateError> {
self.with_read_tx(move |tx| {
let mut result = Vec::new();

Expand All @@ -264,7 +268,9 @@ impl StateInner {
let orders = tx.get_account_orders(&account_id)?;
for order in orders {
let matching_pool = tx.get_matching_pool_for_order(&order.id)?;
result.push((order, account_id, matching_pool));
let matchable_amount =
tx.get_order_matchable_amount(&order.id)?.unwrap_or_default();
result.push((order, account_id, matching_pool, matchable_amount));
}
}

Expand Down
3 changes: 3 additions & 0 deletions crates/system-primitives/system-bus/src/message.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Defines types broadcast onto the system bus and thereby websockets

use alloy::primitives::Address;
use circuit_types::Amount;
use darkpool_types::bounded_match_result::BoundedMatchResult;
use renegade_solidity_abi::v2::IDarkpoolV2::SettlementBundle;
use types_account::{
Expand Down Expand Up @@ -144,6 +145,8 @@ pub enum SystemBusMessage {
matching_pool: String,
/// The type of update
update_type: AdminOrderUpdateType,
/// The matchable amount: min(order.amount_in, backing_balance)
matchable_amount: Amount,
},
/// A message indicating that a balance has been updated, for admin
/// consumption
Expand Down
27 changes: 18 additions & 9 deletions crates/workers/api-server/src/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@ use account::{
CreateAccountHandler, GetAccountByIdHandler, GetAccountSeedsHandler, SyncAccountHandler,
};
use admin::{
AdminCreateMatchingPoolHandler, AdminCreateOrderInPoolHandler, AdminDestroyMatchingPoolHandler,
AdminGetAccountOrdersHandler, AdminGetDisabledAssetsHandler, AdminGetOrderByIdHandler,
AdminGetOrdersHandler, AdminGetTaskQueuePausedHandler, AdminRefreshMatchFeesHandler,
AdminRefreshTokenMappingHandler, AdminTriggerSnapshotHandler, IsLeaderHandler,
AdminAssignOrderToPoolHandler, AdminCreateMatchingPoolHandler, AdminCreateOrderInPoolHandler,
AdminDestroyMatchingPoolHandler, AdminGetAccountOrdersHandler, AdminGetDisabledAssetsHandler,
AdminGetOrderByIdHandler, AdminGetOrdersHandler, AdminGetTaskQueuePausedHandler,
AdminRefreshMatchFeesHandler, AdminRefreshTokenMappingHandler, AdminTriggerSnapshotHandler,
IsLeaderHandler,
};
use async_trait::async_trait;
use balance::{
Expand All @@ -34,11 +35,12 @@ use external_api::{
SYNC_ACCOUNT_ROUTE,
},
admin::{
ADMIN_CREATE_ORDER_IN_POOL_ROUTE, ADMIN_GET_ACCOUNT_ORDERS_ROUTE,
ADMIN_GET_DISABLED_ASSETS_ROUTE, ADMIN_GET_ORDER_BY_ID_ROUTE, ADMIN_GET_ORDERS_ROUTE,
ADMIN_GET_TASK_QUEUE_PAUSED_ROUTE, ADMIN_MATCHING_POOL_CREATE_ROUTE,
ADMIN_MATCHING_POOL_DESTROY_ROUTE, ADMIN_REFRESH_MATCH_FEES_ROUTE,
ADMIN_REFRESH_TOKEN_MAPPING_ROUTE, ADMIN_TRIGGER_SNAPSHOT_ROUTE, IS_LEADER_ROUTE,
ADMIN_ASSIGN_ORDER_TO_POOL_ROUTE, ADMIN_CREATE_ORDER_IN_POOL_ROUTE,
ADMIN_GET_ACCOUNT_ORDERS_ROUTE, ADMIN_GET_DISABLED_ASSETS_ROUTE,
ADMIN_GET_ORDER_BY_ID_ROUTE, ADMIN_GET_ORDERS_ROUTE, ADMIN_GET_TASK_QUEUE_PAUSED_ROUTE,
ADMIN_MATCHING_POOL_CREATE_ROUTE, ADMIN_MATCHING_POOL_DESTROY_ROUTE,
ADMIN_REFRESH_MATCH_FEES_ROUTE, ADMIN_REFRESH_TOKEN_MAPPING_ROUTE,
ADMIN_TRIGGER_SNAPSHOT_ROUTE, IS_LEADER_ROUTE,
},
balance::{
DEPOSIT_BALANCE_ROUTE, GET_BALANCE_BY_MINT_ROUTE, GET_BALANCES_ROUTE,
Expand Down Expand Up @@ -408,6 +410,13 @@ impl HttpServer {
AdminCreateOrderInPoolHandler::new(executor, state.clone(), task_queue.clone()),
);

// POST /v2/relayer-admin/orders/:order_id/assign-pool
router.add_admin_authenticated_route(
&Method::POST,
ADMIN_ASSIGN_ORDER_TO_POOL_ROUTE.to_string(),
AdminAssignOrderToPoolHandler::new(state.clone(), matching_engine_worker_queue.clone()),
);

Ok(router)
}

Expand Down
Loading