From 01e8c45a61a86a807c96a9996b8983fc43b3a29a Mon Sep 17 00:00:00 2001 From: Nicholas Rodrigues Lordello Date: Tue, 26 Oct 2021 14:02:36 +0200 Subject: [PATCH] Revert "remove legacy fee info (#1295)" This reverts commit 76f7739d130eb54135624d3d049be1158c687370. --- orderbook/openapi.yml | 44 +++++++++++++++++++ orderbook/src/api.rs | 2 + orderbook/src/api/get_fee_info.rs | 72 +++++++++++++++++++++++++++++++ 3 files changed, 118 insertions(+) diff --git a/orderbook/openapi.yml b/orderbook/openapi.yml index 641ce7dd5..0f0f999da 100644 --- a/orderbook/openapi.yml +++ b/orderbook/openapi.yml @@ -179,6 +179,27 @@ paths: type: array items: $ref: "#/components/schemas/Order" + /api/v1/tokens/{sellToken}/fee: + get: + description: | + The fee that is charged for placing an order. + The fee is described by a minimum fee - in order to cover the gas costs for onchain settling - and + a feeRatio charged to the users for using the service. + parameters: + - name: sellToken + in: path + required: true + schema: + $ref: "#/components/schemas/Address" + responses: + 200: + description: the fee + content: + application/json: + schema: + $ref: "#/components/schemas/LegacyFeeInformation" + 404: + description: sellToken non-existent /api/v1/trades: get: summary: Get existing Trades. @@ -506,6 +527,29 @@ components: required: - expirationDate - amount + LegacyFeeInformation: + description: | + Provides the information to calculate the fees. + type: object + properties: + expirationDate: + description: | + Expiration date of the offered fee. Order service might not accept + the fee after this expiration date. Encoded as ISO 8601 UTC. + type: string + example: "2020-12-03T18:35:18.814523Z" + minimalFee: + description: Absolute amount of minimal fee charged per order in specified sellToken + $ref: "#/components/schemas/TokenAmount" + feeRatio: + description: The fee ratio charged on a sellAmount. Denoted in basis points + example: 10 + type: number + format: int32 + required: + - expirationDate + - minimalFee + - feeRatio OrderType: description: Is this a buy order or sell order? type: string diff --git a/orderbook/src/api.rs b/orderbook/src/api.rs index feb999006..ada6fc684 100644 --- a/orderbook/src/api.rs +++ b/orderbook/src/api.rs @@ -33,6 +33,7 @@ pub fn handle_all_routes( ) -> impl Filter + Clone { let create_order = create_order::create_order(orderbook.clone()); let get_orders = get_orders::get_orders(orderbook.clone()); + let legacy_fee_info = get_fee_info::legacy_get_fee_info(quoter.fee_calculator.clone()); let fee_info = get_fee_info::get_fee_info(quoter.fee_calculator.clone()); let get_order = get_order_by_uid::get_order_by_uid(orderbook.clone()); let get_solvable_orders = get_solvable_orders::get_solvable_orders(orderbook.clone()); @@ -53,6 +54,7 @@ pub fn handle_all_routes( .and(create_order.with(handle_metrics("create_order")))) .or(warp::path!("api" / "v1" / ..).and(get_orders.with(handle_metrics("get_orders")))) .or(warp::path!("api" / "v1" / ..).and(fee_info.with(handle_metrics("fee_info")))) + .or(warp::path!("api" / "v1" / ..).and(legacy_fee_info.with(handle_metrics("legacy_fee_info")))) .or(warp::path!("api" / "v1" / ..).and(get_order.with(handle_metrics("get_order")))) .or(warp::path!("api" / "v1" / ..) .and(get_solvable_orders.with(handle_metrics("get_solvable_orders")))) diff --git a/orderbook/src/api/get_fee_info.rs b/orderbook/src/api/get_fee_info.rs index 005b6bb27..b897ccd1b 100644 --- a/orderbook/src/api/get_fee_info.rs +++ b/orderbook/src/api/get_fee_info.rs @@ -73,6 +73,55 @@ pub fn get_fee_info( }) } +// TODO remove legacy fee endpoint once frontend is updated + +#[derive(Debug, Serialize, Deserialize)] +#[serde(rename_all = "camelCase")] +struct LegacyFeeInfo { + pub expiration_date: DateTime, + #[serde(with = "u256_decimal")] + pub minimal_fee: U256, + pub fee_ratio: u32, +} + +pub fn legacy_get_fee_info_request() -> impl Filter + Clone { + warp::path!("tokens" / H160 / "fee").and(warp::get()) +} + +pub fn legacy_get_fee_info_response( + result: Result<(U256, DateTime), PriceEstimationError>, +) -> impl Reply { + match result { + Ok((minimal_fee, expiration_date)) => { + let fee_info = LegacyFeeInfo { + expiration_date, + minimal_fee, + fee_ratio: 0u32, + }; + Ok(reply::with_status(reply::json(&fee_info), StatusCode::OK)) + } + Err(err) => { + let (json, status_code) = price_estimation_error_to_warp_reply(err); + Ok(reply::with_status(json, status_code)) + } + } +} + +pub fn legacy_get_fee_info( + fee_calculator: Arc, +) -> impl Filter + Clone { + legacy_get_fee_info_request().and_then(move |token| { + let fee_calculator = fee_calculator.clone(); + async move { + Result::<_, Infallible>::Ok(legacy_get_fee_info_response( + fee_calculator + .compute_subsidized_min_fee(token, None, None, None, None) + .await, + )) + } + }) +} + #[cfg(test)] mod tests { use super::*; @@ -110,4 +159,27 @@ mod tests { assert_eq!(body.amount, U256::zero()); assert!(body.expiration_date.gt(&chrono::offset::Utc::now())) } + + #[tokio::test] + async fn legacy_get_fee_info_request_ok() { + let filter = legacy_get_fee_info_request(); + let token = String::from("0x0000000000000000000000000000000000000001"); + let path_string = format!("/tokens/{}/fee", token); + let request = request().path(&path_string).method("GET"); + let result = request.filter(&filter).await.unwrap(); + assert_eq!(result, H160::from_low_u64_be(1)); + } + + #[tokio::test] + async fn legacy_get_fee_info_response_() { + let response = + legacy_get_fee_info_response(Ok((U256::zero(), Utc::now() + FixedOffset::east(10)))) + .into_response(); + assert_eq!(response.status(), StatusCode::OK); + let body = response_body(response).await; + let body: LegacyFeeInfo = serde_json::from_slice(body.as_slice()).unwrap(); + assert_eq!(body.minimal_fee, U256::zero()); + assert_eq!(body.fee_ratio, 0); + assert!(body.expiration_date.gt(&chrono::offset::Utc::now())) + } }