-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Description Fixes #2111 Defines the table for storing fee policies per `<auction_id, order_uid>`. Currently supports taking fees as: 1. cut from a surplus - the price difference between the executed price and limit_price. Optionally, for this type, we also have a CAP on max volume (expressed in the same way as (2) 2. percent of volume (or more precise, percent of the `executed_amount`, so refers to sell token for sell orders and buy token for buy orders). Will be merged at the very end of feature implementation, since db migrations are irreversible. # Changes <!-- List of detailed changes (how the change is accomplished) --> - [x] Created table for fee policies - [x] Implemented basic database function to read/write ## How to test Roundtrip UT --------- Co-authored-by: ilya <[email protected]> Co-authored-by: Martin Beckmann <[email protected]>
- Loading branch information
1 parent
5122abe
commit 398351d
Showing
16 changed files
with
286 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
use { | ||
crate::infra::persistence::dto, | ||
sqlx::{PgConnection, QueryBuilder}, | ||
}; | ||
|
||
pub async fn insert_batch( | ||
ex: &mut PgConnection, | ||
fee_policies: impl IntoIterator<Item = dto::FeePolicy>, | ||
) -> Result<(), sqlx::Error> { | ||
let mut query_builder = QueryBuilder::new( | ||
"INSERT INTO fee_policies (auction_id, order_uid, kind, surplus_factor, \ | ||
max_volume_factor, volume_factor) ", | ||
); | ||
|
||
query_builder.push_values(fee_policies, |mut b, fee_policy| { | ||
b.push_bind(fee_policy.auction_id) | ||
.push_bind(fee_policy.order_uid) | ||
.push_bind(fee_policy.kind) | ||
.push_bind(fee_policy.surplus_factor) | ||
.push_bind(fee_policy.max_volume_factor) | ||
.push_bind(fee_policy.volume_factor); | ||
}); | ||
|
||
query_builder.build().execute(ex).await.map(|_| ()) | ||
} | ||
|
||
pub async fn fetch( | ||
ex: &mut PgConnection, | ||
auction_id: dto::AuctionId, | ||
order_uid: database::OrderUid, | ||
) -> Result<Vec<dto::FeePolicy>, sqlx::Error> { | ||
const QUERY: &str = r#" | ||
SELECT * FROM fee_policies | ||
WHERE auction_id = $1 AND order_uid = $2 | ||
ORDER BY application_order | ||
"#; | ||
let rows = sqlx::query_as::<_, dto::FeePolicy>(QUERY) | ||
.bind(auction_id) | ||
.bind(order_uid) | ||
.fetch_all(ex) | ||
.await? | ||
.into_iter() | ||
.collect(); | ||
Ok(rows) | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use {super::*, database::byte_array::ByteArray, sqlx::Connection}; | ||
|
||
#[tokio::test] | ||
#[ignore] | ||
async fn postgres_roundtrip() { | ||
let mut db = PgConnection::connect("postgresql://").await.unwrap(); | ||
let mut db = db.begin().await.unwrap(); | ||
database::clear_DANGER_(&mut db).await.unwrap(); | ||
|
||
// same primary key for all fee policies | ||
let (auction_id, order_uid) = (1, ByteArray([1; 56])); | ||
|
||
// surplus fee policy without caps | ||
let fee_policy_1 = dto::FeePolicy { | ||
auction_id, | ||
order_uid, | ||
kind: dto::fee_policy::FeePolicyKind::Surplus, | ||
surplus_factor: Some(0.1), | ||
max_volume_factor: Some(1.0), | ||
volume_factor: None, | ||
}; | ||
// surplus fee policy with caps | ||
let fee_policy_2 = dto::FeePolicy { | ||
auction_id, | ||
order_uid, | ||
kind: dto::fee_policy::FeePolicyKind::Surplus, | ||
surplus_factor: Some(0.2), | ||
max_volume_factor: Some(0.05), | ||
volume_factor: None, | ||
}; | ||
// volume based fee policy | ||
let fee_policy_3 = dto::FeePolicy { | ||
auction_id, | ||
order_uid, | ||
kind: dto::fee_policy::FeePolicyKind::Volume, | ||
surplus_factor: None, | ||
max_volume_factor: None, | ||
volume_factor: Some(0.06), | ||
}; | ||
insert_batch( | ||
&mut db, | ||
vec![ | ||
fee_policy_1.clone(), | ||
fee_policy_2.clone(), | ||
fee_policy_3.clone(), | ||
], | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let output = fetch(&mut db, 1, order_uid).await.unwrap(); | ||
assert_eq!(output, vec![fee_policy_1, fee_policy_2, fee_policy_3]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use crate::{boundary, domain}; | ||
|
||
#[derive(Debug, Clone, PartialEq, sqlx::FromRow)] | ||
pub struct FeePolicy { | ||
pub auction_id: domain::AuctionId, | ||
pub order_uid: boundary::database::OrderUid, | ||
pub kind: FeePolicyKind, | ||
pub surplus_factor: Option<f64>, | ||
pub max_volume_factor: Option<f64>, | ||
pub volume_factor: Option<f64>, | ||
} | ||
|
||
impl FeePolicy { | ||
pub fn from_domain( | ||
auction_id: domain::AuctionId, | ||
order_uid: domain::OrderUid, | ||
policy: domain::fee::Policy, | ||
) -> Self { | ||
match policy { | ||
domain::fee::Policy::Surplus { | ||
factor, | ||
max_volume_factor, | ||
} => Self { | ||
auction_id, | ||
order_uid: boundary::database::byte_array::ByteArray(order_uid.0), | ||
kind: FeePolicyKind::Surplus, | ||
surplus_factor: Some(factor), | ||
max_volume_factor: Some(max_volume_factor), | ||
volume_factor: None, | ||
}, | ||
domain::fee::Policy::Volume { factor } => Self { | ||
auction_id, | ||
order_uid: boundary::database::byte_array::ByteArray(order_uid.0), | ||
kind: FeePolicyKind::Volume, | ||
surplus_factor: None, | ||
max_volume_factor: None, | ||
volume_factor: Some(factor), | ||
}, | ||
} | ||
} | ||
} | ||
|
||
impl From<FeePolicy> for domain::fee::Policy { | ||
fn from(row: FeePolicy) -> domain::fee::Policy { | ||
match row.kind { | ||
FeePolicyKind::Surplus => domain::fee::Policy::Surplus { | ||
factor: row.surplus_factor.expect("missing surplus factor"), | ||
max_volume_factor: row.max_volume_factor.expect("missing max volume factor"), | ||
}, | ||
FeePolicyKind::Volume => domain::fee::Policy::Volume { | ||
factor: row.volume_factor.expect("missing volume factor"), | ||
}, | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone, PartialEq, sqlx::Type)] | ||
#[sqlx(type_name = "PolicyKind", rename_all = "lowercase")] | ||
pub enum FeePolicyKind { | ||
Surplus, | ||
Volume, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
pub mod auction; | ||
pub mod fee_policy; | ||
pub mod order; | ||
pub mod quote; | ||
|
||
pub use auction::{Auction, AuctionId, AuctionWithId}; | ||
pub use { | ||
auction::{Auction, AuctionId, AuctionWithId}, | ||
fee_policy::FeePolicy, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.