Skip to content

Commit 980ba2a

Browse files
DewyerBarnabas Ratki
andauthored
feat: Remove generics from opportunity service (#505)
Co-authored-by: Barnabas Ratki <barna@dourolabs.xyz>
1 parent 771f51b commit 980ba2a

33 files changed

Lines changed: 212 additions & 410 deletions

auction-server/src/auction/service/mod.rs

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ use {
1212
db::DB,
1313
entities::ChainId,
1414
},
15-
opportunity::service as opportunity_service,
1615
},
1716
mockall_double::double,
1817
solana_client::{
@@ -94,7 +93,7 @@ pub struct Config {
9493
}
9594

9695
pub struct ServiceInner {
97-
opportunity_service: Arc<OpportunityService<opportunity_service::ChainTypeSvm>>,
96+
opportunity_service: Arc<OpportunityService>,
9897
config: Config,
9998
repo: Arc<Repository>,
10099
task_tracker: TaskTracker,
@@ -114,7 +113,7 @@ impl Service {
114113
pub fn new(
115114
db: DB,
116115
config: Config,
117-
opportunity_service: Arc<OpportunityService<ChainTypeSvm>>,
116+
opportunity_service: Arc<OpportunityService>,
118117
task_tracker: TaskTracker,
119118
event_sender: broadcast::Sender<UpdateEvent>,
120119
) -> Self {
@@ -133,7 +132,6 @@ pub enum ServiceEnum {
133132
Svm(Service),
134133
}
135134

136-
use crate::opportunity::service::ChainTypeSvm;
137135
#[cfg(test)]
138136
pub use {
139137
mock_service::MockService,
@@ -176,7 +174,7 @@ mod mock_service {
176174
pub fn new(
177175
db: DB,
178176
config: Config,
179-
opportunity_service: Arc<OpportunityService<ChainTypeSvm>>,
177+
opportunity_service: Arc<OpportunityService>,
180178
task_tracker: TaskTracker,
181179
event_sender: broadcast::Sender<UpdateEvent>,
182180
) -> Self;
@@ -266,10 +264,7 @@ pub mod tests {
266264
TracedSenderSvm,
267265
},
268266
},
269-
opportunity::service::{
270-
ChainTypeSvm,
271-
MockService as MockOpportunityService,
272-
},
267+
opportunity::service::MockService as MockOpportunityService,
273268
server::{
274269
get_submit_bid_instruction_account_positions,
275270
get_swap_instruction_account_positions,
@@ -289,7 +284,7 @@ pub mod tests {
289284
pub fn new_with_mocks_svm(
290285
chain_id: ChainId,
291286
db: impl Database,
292-
opportunity_service: MockOpportunityService<ChainTypeSvm>,
287+
opportunity_service: MockOpportunityService,
293288
rpc_client: MockRpcClient,
294289
broadcaster_client: MockRpcClient,
295290
) -> Self {

auction-server/src/auction/service/verification.rs

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,7 +1608,6 @@ mod tests {
16081608
generate_indicative_price_taker,
16091609
get_quote_virtual_permission_account,
16101610
},
1611-
ChainTypeSvm,
16121611
MockService,
16131612
},
16141613
},
@@ -1690,10 +1689,8 @@ mod tests {
16901689
pub with_minimum_lifetime: OpportunitySvm,
16911690
}
16921691

1693-
fn get_opportunity_service(
1694-
chain_id: ChainId,
1695-
) -> (MockService<ChainTypeSvm>, TestOpportunities) {
1696-
let mut opportunity_service = MockService::<ChainTypeSvm>::default();
1692+
fn get_opportunity_service(chain_id: ChainId) -> (MockService, TestOpportunities) {
1693+
let mut opportunity_service = MockService::default();
16971694
let now = OffsetDateTime::now_utc();
16981695
let router = Pubkey::new_unique();
16991696
let user_wallet_address = Pubkey::new_unique();
@@ -1769,7 +1766,7 @@ mod tests {
17691766
);
17701767

17711768
let opp_user_token_specified = OpportunitySvm {
1772-
core_fields: OpportunityCoreFields::<TokenAmountSvm> {
1769+
core_fields: OpportunityCoreFields {
17731770
id: Uuid::new_v4(),
17741771
permission_key: OpportunitySvm::get_permission_key(
17751772
BidPaymentInstructionType::Swap,
@@ -1798,7 +1795,7 @@ mod tests {
17981795
};
17991796

18001797
let opp_searcher_token_specified = OpportunitySvm {
1801-
core_fields: OpportunityCoreFields::<TokenAmountSvm> {
1798+
core_fields: OpportunityCoreFields {
18021799
id: Uuid::new_v4(),
18031800
permission_key: OpportunitySvm::get_permission_key(
18041801
BidPaymentInstructionType::Swap,
@@ -1827,7 +1824,7 @@ mod tests {
18271824
};
18281825

18291826
let opp_user_token_wsol = OpportunitySvm {
1830-
core_fields: OpportunityCoreFields::<TokenAmountSvm> {
1827+
core_fields: OpportunityCoreFields {
18311828
id: Uuid::new_v4(),
18321829
permission_key: OpportunitySvm::get_permission_key(
18331830
BidPaymentInstructionType::Swap,
@@ -1860,7 +1857,7 @@ mod tests {
18601857
};
18611858

18621859
let opp_searcher_token_wsol = OpportunitySvm {
1863-
core_fields: OpportunityCoreFields::<TokenAmountSvm> {
1860+
core_fields: OpportunityCoreFields {
18641861
id: Uuid::new_v4(),
18651862
permission_key: OpportunitySvm::get_permission_key(
18661863
BidPaymentInstructionType::Swap,
@@ -1897,7 +1894,7 @@ mod tests {
18971894
);
18981895

18991896
let opp_with_indicative_price_taker = OpportunitySvm {
1900-
core_fields: OpportunityCoreFields::<TokenAmountSvm> {
1897+
core_fields: OpportunityCoreFields {
19011898
id: Uuid::new_v4(),
19021899
permission_key: OpportunitySvm::get_permission_key(
19031900
BidPaymentInstructionType::Swap,
@@ -1927,7 +1924,7 @@ mod tests {
19271924

19281925

19291926
let opp_with_user_payer = OpportunitySvm {
1930-
core_fields: OpportunityCoreFields::<TokenAmountSvm> {
1927+
core_fields: OpportunityCoreFields {
19311928
id: Uuid::new_v4(),
19321929
permission_key: OpportunitySvm::get_permission_key(
19331930
BidPaymentInstructionType::Swap,
@@ -1961,7 +1958,7 @@ mod tests {
19611958
};
19621959

19631960
let opp_with_memo = OpportunitySvm {
1964-
core_fields: OpportunityCoreFields::<TokenAmountSvm> {
1961+
core_fields: OpportunityCoreFields {
19651962
id: Uuid::new_v4(),
19661963
permission_key: OpportunitySvm::get_permission_key(
19671964
BidPaymentInstructionType::Swap,
@@ -1991,7 +1988,7 @@ mod tests {
19911988
};
19921989

19931990
let opp_with_minimum_lifetime = OpportunitySvm {
1994-
core_fields: OpportunityCoreFields::<TokenAmountSvm> {
1991+
core_fields: OpportunityCoreFields {
19951992
id: Uuid::new_v4(),
19961993
permission_key: OpportunitySvm::get_permission_key(
19971994
BidPaymentInstructionType::Swap,

auction-server/src/kernel/entities.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,6 @@ impl Display for PermissionKeySvm {
2525
}
2626
}
2727

28-
#[derive(Debug, PartialEq, Clone)]
29-
pub enum ChainType {
30-
Evm,
31-
Svm,
32-
}
33-
34-
#[derive(Clone, Debug, PartialEq)]
35-
pub struct Evm;
36-
3728
#[derive(Clone, Debug, PartialEq)]
3829
pub struct Svm;
3930

auction-server/src/opportunity/entities/opportunity.rs

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,17 @@
11
use {
2-
super::token_amount::TokenAmount,
32
crate::{
43
api::RestError,
54
kernel::entities::{
65
ChainId,
76
PermissionKey,
87
},
9-
opportunity::repository,
8+
opportunity::{
9+
entities::TokenAmountSvm,
10+
repository,
11+
},
1012
},
1113
ethers::types::Bytes,
12-
express_relay_api_types::opportunity as api,
13-
std::{
14-
fmt::Debug,
15-
ops::Deref,
16-
},
14+
std::fmt::Debug,
1715
time::OffsetDateTime,
1816
uuid::Uuid,
1917
};
@@ -24,18 +22,18 @@ pub type OpportunityId = Uuid;
2422
pub struct OpportunityKey(pub ChainId, pub PermissionKey);
2523

2624
#[derive(Debug, Clone, PartialEq)]
27-
pub struct OpportunityCoreFields<T: TokenAmount> {
25+
pub struct OpportunityCoreFields {
2826
pub id: OpportunityId,
2927
pub permission_key: Bytes,
3028
pub chain_id: ChainId,
31-
pub sell_tokens: Vec<T>,
32-
pub buy_tokens: Vec<T>,
29+
pub sell_tokens: Vec<TokenAmountSvm>,
30+
pub buy_tokens: Vec<TokenAmountSvm>,
3331
pub creation_time: OffsetDateTime,
3432
pub refresh_time: OffsetDateTime,
3533
}
3634

37-
impl<T: TokenAmount> OpportunityCoreFields<T> {
38-
pub fn new_with_current_time(val: OpportunityCoreFieldsCreate<T>) -> Self {
35+
impl OpportunityCoreFields {
36+
pub fn new_with_current_time(val: OpportunityCoreFieldsCreate) -> Self {
3937
Self {
4038
id: Uuid::new_v4(),
4139
permission_key: val.permission_key,
@@ -49,11 +47,11 @@ impl<T: TokenAmount> OpportunityCoreFields<T> {
4947
}
5048

5149
#[derive(Debug, Clone, PartialEq)]
52-
pub struct OpportunityCoreFieldsCreate<T: TokenAmount> {
50+
pub struct OpportunityCoreFieldsCreate {
5351
pub permission_key: Bytes,
5452
pub chain_id: ChainId,
55-
pub sell_tokens: Vec<T>,
56-
pub buy_tokens: Vec<T>,
53+
pub sell_tokens: Vec<TokenAmountSvm>,
54+
pub buy_tokens: Vec<TokenAmountSvm>,
5755
}
5856

5957
#[derive(Debug, Clone)]
@@ -63,39 +61,6 @@ pub enum OpportunityComparison {
6361
NeedsRefresh,
6462
}
6563

66-
// TODO Think more about structure. Isn't it better to have a generic Opportunity struct with a field of type OpportunityParams?
67-
pub trait Opportunity:
68-
Debug
69-
+ Clone
70-
+ Deref<Target = OpportunityCoreFields<<Self as Opportunity>::TokenAmount>>
71-
+ PartialEq
72-
+ Into<api::Opportunity>
73-
+ Into<Self::OpportunityCreate>
74-
+ TryFrom<repository::Opportunity<Self::ModelMetadata>>
75-
+ Send
76-
+ Sync
77-
{
78-
type TokenAmount: TokenAmount;
79-
type ModelMetadata: repository::OpportunityMetadata;
80-
type OpportunityCreate: OpportunityCreate;
81-
82-
fn new_with_current_time(val: Self::OpportunityCreate) -> Self;
83-
fn get_models_metadata(&self) -> Self::ModelMetadata;
84-
fn get_opportunity_delete(&self) -> api::OpportunityDelete;
85-
fn get_key(&self) -> OpportunityKey {
86-
OpportunityKey(self.chain_id.clone(), self.permission_key.clone())
87-
}
88-
89-
fn compare(&self, other: &Self::OpportunityCreate) -> OpportunityComparison;
90-
fn refresh(&mut self);
91-
}
92-
93-
pub trait OpportunityCreate: Debug + Clone + From<Self::ApiOpportunityCreate> + PartialEq {
94-
type ApiOpportunityCreate;
95-
96-
fn get_key(&self) -> OpportunityKey;
97-
}
98-
9964
#[derive(Debug)]
10065
pub enum OpportunityRemovalReason {
10166
Expired,

0 commit comments

Comments
 (0)