Skip to content
This repository was archived by the owner on Apr 28, 2022. It is now read-only.

Commit

Permalink
implement post_qoute with fee as exists in current fee route
Browse files Browse the repository at this point in the history
  • Loading branch information
bh2smith committed Sep 27, 2021
1 parent 8427b7d commit 420bb25
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 61 deletions.
4 changes: 2 additions & 2 deletions orderbook/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ pub fn handle_all_routes(
let get_fee_and_quote_sell =
get_fee_and_quote::get_fee_and_quote_sell(fee_calculator.clone(), price_estimator.clone());
let get_fee_and_quote_buy =
get_fee_and_quote::get_fee_and_quote_buy(fee_calculator, price_estimator.clone());
get_fee_and_quote::get_fee_and_quote_buy(fee_calculator.clone(), price_estimator.clone());
let get_user_orders = get_user_orders::get_user_orders(orderbook);
let post_quote = post_quote::post_quote();
let post_quote = post_quote::post_quote(fee_calculator, price_estimator.clone());
let cors = warp::cors()
.allow_any_origin()
.allow_methods(vec!["GET", "POST", "DELETE", "OPTIONS", "PUT", "PATCH"])
Expand Down
102 changes: 57 additions & 45 deletions orderbook/src/api/get_fee_and_quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,70 +8,104 @@ use serde::{Deserialize, Serialize};
use shared::price_estimation::{self, PriceEstimating, PriceEstimationError};
use std::convert::Infallible;
use std::sync::Arc;
use warp::reply::Json;
use warp::{hyper::StatusCode, reply, Filter, Rejection, Reply};

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct Fee {
pub struct Fee {
#[serde(with = "u256_decimal")]
amount: U256,
expiration_date: DateTime<Utc>,
pub amount: U256,
pub expiration_date: DateTime<Utc>,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct SellQuery {
pub struct SellQuery {
#[serde(with = "h160_hexadecimal")]
sell_token: H160,
pub sell_token: H160,
#[serde(with = "h160_hexadecimal")]
buy_token: H160,
pub buy_token: H160,
// The total amount to be sold from which the fee will be deducted.
#[serde(with = "u256_decimal")]
sell_amount_before_fee: U256,
pub sell_amount_before_fee: U256,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct SellResponse {
pub struct SellResponse {
// The fee that is deducted from sell_amount_before_fee. The sell amount that is traded is
// sell_amount_before_fee - fee_in_sell_token.
fee: Fee,
pub fee: Fee,
// The expected buy amount for the traded sell amount.
#[serde(with = "u256_decimal")]
buy_amount_after_fee: U256,
pub buy_amount_after_fee: U256,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct BuyQuery {
pub struct BuyQuery {
#[serde(with = "h160_hexadecimal")]
sell_token: H160,
pub sell_token: H160,
#[serde(with = "h160_hexadecimal")]
buy_token: H160,
pub buy_token: H160,
// The total amount to be bought.
#[serde(with = "u256_decimal")]
buy_amount_after_fee: U256,
pub buy_amount_after_fee: U256,
}

#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
struct BuyResponse {
pub struct BuyResponse {
// The fee that is deducted from sell_amount_before_fee. The sell amount that is traded is
// sell_amount_before_fee - fee_in_sell_token.
fee: Fee,
pub fee: Fee,
#[serde(with = "u256_decimal")]
sell_amount_before_fee: U256,
pub sell_amount_before_fee: U256,
}

#[derive(Debug)]
enum Error {
pub enum Error {
NoLiquidity,
UnsupportedToken(H160),
AmountIsZero,
SellAmountDoesNotCoverFee,
Other(anyhow::Error),
}

impl Error {
pub fn convert_to_reply(self) -> (Json, StatusCode) {
match self {
Error::NoLiquidity => (
super::error("NoLiquidity", "not enough liquidity"),
StatusCode::NOT_FOUND,
),
Error::UnsupportedToken(token) => (
super::error("UnsupportedToken", format!("Token address {:?}", token)),
StatusCode::BAD_REQUEST,
),
Error::AmountIsZero => (
super::error(
"AmountIsZero",
"The input amount must be greater than zero.".to_string(),
),
StatusCode::BAD_REQUEST,
),
Error::SellAmountDoesNotCoverFee => (
super::error(
"SellAmountDoesNotCoverFee",
"The sell amount for the sell order is lower than the fee.".to_string(),
),
StatusCode::BAD_REQUEST,
),
Error::Other(err) => {
tracing::error!(?err, "get_fee_and_price error");
(super::internal_error(), StatusCode::INTERNAL_SERVER_ERROR)
}
}
}
}

impl From<PriceEstimationError> for Error {
fn from(other: PriceEstimationError) -> Self {
match other {
Expand All @@ -82,7 +116,7 @@ impl From<PriceEstimationError> for Error {
}
}

async fn calculate_sell(
pub async fn calculate_sell(
fee_calculator: Arc<dyn MinFeeCalculating>,
price_estimator: Arc<dyn PriceEstimating>,
query: SellQuery,
Expand Down Expand Up @@ -124,7 +158,7 @@ async fn calculate_sell(
})
}

async fn calculate_buy(
pub async fn calculate_buy(
fee_calculator: Arc<dyn MinFeeCalculating>,
price_estimator: Arc<dyn PriceEstimating>,
query: BuyQuery,
Expand Down Expand Up @@ -179,31 +213,9 @@ fn buy_request() -> impl Filter<Extract = (BuyQuery,), Error = Rejection> + Clon
fn response<T: Serialize>(result: Result<T, Error>) -> impl Reply {
match result {
Ok(response) => reply::with_status(reply::json(&response), StatusCode::OK),
Err(Error::NoLiquidity) => reply::with_status(
super::error("NoLiquidity", "not enough liquidity"),
StatusCode::NOT_FOUND,
),
Err(Error::UnsupportedToken(token)) => reply::with_status(
super::error("UnsupportedToken", format!("Token address {:?}", token)),
StatusCode::BAD_REQUEST,
),
Err(Error::AmountIsZero) => reply::with_status(
super::error(
"AmountIsZero",
"The input amount must be greater than zero.".to_string(),
),
StatusCode::BAD_REQUEST,
),
Err(Error::SellAmountDoesNotCoverFee) => reply::with_status(
super::error(
"SellAmountDoesNotCoverFee",
"The sell amount for the sell order is lower than the fee.".to_string(),
),
StatusCode::BAD_REQUEST,
),
Err(Error::Other(err)) => {
tracing::error!(?err, "get_fee_and_price error");
reply::with_status(super::internal_error(), StatusCode::INTERNAL_SERVER_ERROR)
Err(error) => {
let (reply, status) = error.convert_to_reply();
reply::with_status(reply, status)
}
}
}
Expand Down
Loading

0 comments on commit 420bb25

Please sign in to comment.