-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: custom expiration for submarine swaps (#779)
- Loading branch information
1 parent
a47bd78
commit 5d16749
Showing
20 changed files
with
649 additions
and
167 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,31 @@ | ||
use crate::db::helpers::{BoxedCondition, QueryResponse}; | ||
use crate::db::models::Referral; | ||
use crate::db::schema::referrals; | ||
use crate::db::Pool; | ||
use diesel::{QueryDsl, RunQueryDsl, SelectableHelper}; | ||
|
||
pub type ReferralCondition = BoxedCondition<referrals::table>; | ||
|
||
pub trait ReferralHelper { | ||
fn get_all(&self, condition: ReferralCondition) -> QueryResponse<Vec<Referral>>; | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
pub struct ReferralHelperDatabase { | ||
pool: Pool, | ||
} | ||
|
||
impl ReferralHelperDatabase { | ||
pub fn new(pool: Pool) -> Self { | ||
Self { pool } | ||
} | ||
} | ||
|
||
impl ReferralHelper for ReferralHelperDatabase { | ||
fn get_all(&self, condition: ReferralCondition) -> QueryResponse<Vec<Referral>> { | ||
Ok(referrals::dsl::referrals | ||
.select(Referral::as_select()) | ||
.filter(condition) | ||
.load(&mut self.pool.get()?)?) | ||
} | ||
} |
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,158 @@ | ||
use crate::db::models::SwapType; | ||
use anyhow::anyhow; | ||
use diesel::{AsChangeset, Insertable, Queryable, Selectable}; | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
|
||
type CustomExpirations = HashMap<String, u64>; | ||
|
||
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] | ||
struct PairConfig { | ||
expirations: Option<CustomExpirations>, | ||
} | ||
|
||
impl PairConfig { | ||
fn get_expiration(&self, kind: SwapType) -> Option<u64> { | ||
self.expirations.as_ref().map(|expirations| { | ||
expirations | ||
.get(&<SwapType as Into<u64>>::into(kind).to_string()) | ||
.copied() | ||
})? | ||
} | ||
} | ||
|
||
#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)] | ||
struct Config { | ||
#[serde(flatten)] | ||
pub base: PairConfig, | ||
|
||
pairs: Option<HashMap<String, PairConfig>>, | ||
} | ||
|
||
impl Config { | ||
fn for_pair(&self, pair: &str) -> &PairConfig { | ||
self.pairs | ||
.as_ref() | ||
.map(|pairs| pairs.get(pair).unwrap_or(&self.base)) | ||
.unwrap_or(&self.base) | ||
} | ||
} | ||
|
||
#[derive(Queryable, Selectable, Insertable, AsChangeset, PartialEq, Clone, Debug)] | ||
#[diesel(table_name = crate::db::schema::referrals)] | ||
pub struct Referral { | ||
pub id: String, | ||
pub config: Option<serde_json::Value>, | ||
} | ||
|
||
impl Referral { | ||
pub fn custom_expiration_secs( | ||
&self, | ||
pair: &str, | ||
kind: SwapType, | ||
) -> anyhow::Result<Option<u64>> { | ||
if let Some(cfg) = self.parse_config()? { | ||
return Ok(match cfg.for_pair(pair).get_expiration(kind) { | ||
Some(expiration) => Some(expiration), | ||
None => cfg.base.get_expiration(kind), | ||
}); | ||
} | ||
|
||
Ok(None) | ||
} | ||
|
||
fn parse_config(&self) -> anyhow::Result<Option<Config>> { | ||
if let Some(cfg) = &self.config { | ||
return match serde_json::from_value(cfg.clone()) { | ||
Ok(config) => Ok(Some(config)), | ||
Err(err) => Err(anyhow!( | ||
"could not parse config of referral {}: {}", | ||
self.id, | ||
err | ||
)), | ||
}; | ||
} | ||
|
||
Ok(None) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use rstest::*; | ||
|
||
#[rstest] | ||
#[case("{\"expirations\": {\"0\": 600}}", SwapType::Submarine, Some(600))] | ||
#[case( | ||
"{\"expirations\": {\"0\": 600, \"1\": 123}}", | ||
SwapType::Reverse, | ||
Some(123) | ||
)] | ||
#[case( | ||
"{\"expirations\": {\"0\": 600, \"1\": 123}}", | ||
SwapType::Submarine, | ||
Some(600) | ||
)] | ||
#[case("{\"expirations\": {\"0\": 600, \"1\": 123}}", SwapType::Chain, None)] | ||
fn test_pair_config_get_expiration( | ||
#[case] json: &str, | ||
#[case] kind: SwapType, | ||
#[case] expected_expiration: Option<u64>, | ||
) { | ||
let config = serde_json::from_str::<PairConfig>(json).unwrap(); | ||
assert_eq!(config.get_expiration(kind), expected_expiration); | ||
} | ||
|
||
#[rstest] | ||
#[case("", "{\"expirations\": {\"0\": 123}}")] | ||
#[case("L-BTC/BTC", "{\"expirations\": {\"0\": 123}}")] | ||
#[case("BTC/BTC", "{\"expirations\": {\"1\": 2121}}")] | ||
fn test_config_for_pair(#[case] pair: &str, #[case] expected: &str) { | ||
let config = serde_json::from_str::<Config>( | ||
"{\"expirations\": {\"0\": 123}, \"pairs\": {\"BTC/BTC\": {\"expirations\": {\"1\": 2121}}}}", | ||
) | ||
.unwrap(); | ||
|
||
let expected = serde_json::from_str::<PairConfig>(expected).unwrap(); | ||
assert_eq!(config.for_pair(pair), &expected); | ||
} | ||
|
||
#[rstest] | ||
#[case("BTC/BTC", SwapType::Submarine, Some(600))] | ||
#[case("L-BTC/BTC", SwapType::Submarine, Some(123))] | ||
#[case("BTC/BTC", SwapType::Reverse, Some(2121))] | ||
#[case("L-BTC/BTC", SwapType::Reverse, Some(2121))] | ||
#[case("BTC/BTC", SwapType::Chain, None)] | ||
fn test_referral_custom_expiration( | ||
#[case] pair: &str, | ||
#[case] kind: SwapType, | ||
#[case] expected_expiration: Option<u64>, | ||
) { | ||
let config = serde_json::json!({ | ||
"expirations": { | ||
"0": 123, | ||
"1": 2121 | ||
}, | ||
"pairs": { | ||
"BTC/BTC": { | ||
"expirations": { | ||
"0": 600 | ||
} | ||
} | ||
} | ||
}); | ||
let referral = Referral { | ||
id: "id".to_string(), | ||
config: Some(config), | ||
}; | ||
|
||
let res = referral.custom_expiration_secs(pair, kind).unwrap(); | ||
|
||
if let Some(expected_expiration) = expected_expiration { | ||
assert_eq!(res.unwrap(), expected_expiration); | ||
} else { | ||
assert!(res.is_none()); | ||
} | ||
} | ||
} |
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.