Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Storing quotes longer, in case an on-chain order placement is intended #414

Merged
merged 10 commits into from
Aug 10, 2022
Merged
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
27 changes: 23 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 68 additions & 3 deletions crates/database/src/quotes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ use sqlx::{

pub type QuoteId = i64;

#[derive(Clone, Debug, Default, PartialEq, sqlx::Type)]
#[sqlx(type_name = "QuoteKind")]
#[sqlx(rename_all = "lowercase")]
pub enum QuoteKind {
#[default]
Standard,
Eip1271OnchainOrder,
PreSignOnchainOrder,
}

/// One row in the `quotes` table.
#[derive(Clone, Debug, PartialEq, sqlx::FromRow)]
pub struct Quote {
Expand All @@ -20,6 +30,7 @@ pub struct Quote {
pub sell_token_price: f64,
pub order_kind: OrderKind,
pub expiration_timestamp: DateTime<Utc>,
pub quote_kind: QuoteKind,
}

/// Stores the quote and returns the id. The id of the quote parameter is not used.
Expand All @@ -34,9 +45,10 @@ INSERT INTO quotes (
gas_price,
sell_token_price,
order_kind,
expiration_timestamp
expiration_timestamp,
quote_kind
)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
RETURNING id
"#;
let (id,) = sqlx::query_as(QUERY)
Expand All @@ -49,6 +61,7 @@ RETURNING id
.bind(quote.sell_token_price)
.bind(quote.order_kind)
.bind(quote.expiration_timestamp)
.bind(&quote.quote_kind)
.fetch_one(ex)
.await?;
Ok(id)
Expand All @@ -73,6 +86,7 @@ pub struct QuoteSearchParameters {
pub buy_amount: BigDecimal,
pub kind: OrderKind,
pub expiration: DateTime<Utc>,
pub quote_kind: QuoteKind,
}

pub async fn find(
Expand All @@ -91,7 +105,8 @@ WHERE
(order_kind = 'buy' AND buy_amount = $5)
) AND
order_kind = $6 AND
expiration_timestamp >= $7
expiration_timestamp >= $7 AND
quote_kind = $8
ORDER BY gas_amount * gas_price * sell_token_price ASC
LIMIT 1
"#;
Expand All @@ -103,6 +118,7 @@ LIMIT 1
.bind(&params.buy_amount)
.bind(params.kind)
.bind(params.expiration)
.bind(&params.quote_kind)
.fetch_optional(ex)
.await
}
Expand Down Expand Up @@ -155,6 +171,7 @@ mod tests {
sell_token_price: 7.,
order_kind: OrderKind::Sell,
expiration_timestamp: now,
quote_kind: QuoteKind::Standard,
};
let id = save(&mut db, &quote).await.unwrap();
quote.id = id;
Expand Down Expand Up @@ -186,6 +203,7 @@ mod tests {
gas_price: 1.,
sell_token_price: 1.,
expiration_timestamp: now,
quote_kind: QuoteKind::Standard,
};

let token_b = ByteArray([2; 20]);
Expand All @@ -200,6 +218,7 @@ mod tests {
gas_price: 1.,
sell_token_price: 1.,
expiration_timestamp: now,
quote_kind: QuoteKind::Standard,
};

// Save two measurements for token_a
Expand Down Expand Up @@ -247,6 +266,7 @@ mod tests {
buy_amount: 1.into(),
kind: quote_a.order_kind,
expiration: now,
quote_kind: QuoteKind::Standard,
};
assert_eq!(
find(&mut db, &search_a).await.unwrap().unwrap(),
Expand Down Expand Up @@ -306,6 +326,7 @@ mod tests {
buy_amount: quote_b.buy_amount,
kind: quote_b.order_kind,
expiration: now,
quote_kind: QuoteKind::Standard,
};
assert_eq!(
find(&mut db, &search_b).await.unwrap().unwrap(),
Expand Down Expand Up @@ -345,4 +366,48 @@ mod tests {
assert_eq!(find(&mut db, &search_a).await.unwrap(), None);
assert_eq!(find(&mut db, &search_b).await.unwrap(), None);
}

#[tokio::test]
#[ignore]
async fn postgres_save_and_find_quote_and_differentiates_by_signing_scheme() {
let mut db = PgConnection::connect("postgresql://").await.unwrap();
let mut db = db.begin().await.unwrap();
crate::clear_DANGER_(&mut db).await.unwrap();

let now = low_precision_now();
let token_a = ByteArray([1; 20]);
let quote = {
let mut quote = Quote {
id: Default::default(),
sell_token: token_a,
buy_token: ByteArray([3; 20]),
sell_amount: 4.into(),
buy_amount: 5.into(),
gas_amount: 1.,
gas_price: 1.,
sell_token_price: 1.,
order_kind: OrderKind::Sell,
expiration_timestamp: now,
quote_kind: QuoteKind::Eip1271OnchainOrder,
};
let id = save(&mut db, &quote).await.unwrap();
quote.id = id;
quote
};
// Token A has readings valid until now and in 30s
let mut search_a = QuoteSearchParameters {
sell_token: quote.sell_token,
buy_token: quote.buy_token,
sell_amount_0: quote.sell_amount.clone(),
sell_amount_1: quote.sell_amount.clone(),
buy_amount: quote.buy_amount.clone(),
kind: quote.order_kind,
expiration: quote.expiration_timestamp,
quote_kind: quote.quote_kind.clone(),
};

assert_eq!(find(&mut db, &search_a).await.unwrap().unwrap(), quote,);
search_a.quote_kind = QuoteKind::Standard;
assert_eq!(find(&mut db, &search_a).await.unwrap(), None,);
}
}
1 change: 1 addition & 0 deletions crates/e2e/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ harness = false
anyhow = "1.0"
autopilot = { path = "../autopilot" }
contracts = { path = "../contracts" }
chrono = "0.4"
criterion = "0.3"
database = { path = "../database" }
ethcontract = { version = "0.17.0", default-features = false }
Expand Down
2 changes: 2 additions & 0 deletions crates/e2e/tests/e2e/services.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,8 @@ impl OrderbookServices {
..Default::default()
}),
api_db.clone(),
chrono::Duration::seconds(60i64),
chrono::Duration::seconds(60i64),
));
let balance_fetcher = Arc::new(Web3BalanceFetcher::new(
web3.clone(),
Expand Down
Loading