Skip to content

Commit 9aacb13

Browse files
authored
[FIL-361] Add a new filed client contract address for allocator (#234)
* Add a new filed client contract address for allocator --------- Co-authored-by: Filip Lelek <[email protected]>
1 parent d91b66b commit 9aacb13

File tree

9 files changed

+58
-28
lines changed

9 files changed

+58
-28
lines changed

fplus-database/src/database/allocators.rs

+18
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ pub async fn create_or_update_allocator(
7171
required_sps: Option<String>,
7272
required_replicas: Option<String>,
7373
registry_file_path: Option<String>,
74+
client_contract_address: Option<String>,
7475
) -> Result<AllocatorModel, sea_orm::DbErr> {
7576
let existing_allocator = get_allocator(&owner, &repo).await?;
7677
if let Some(allocator_model) = existing_allocator {
@@ -124,6 +125,16 @@ pub async fn create_or_update_allocator(
124125
allocator_active_model.registry_file_path = Set(registry_file_path);
125126
}
126127

128+
if let Some(client_contract_address) = client_contract_address {
129+
if !client_contract_address.is_empty() {
130+
allocator_active_model.client_contract_address = Set(Some(client_contract_address));
131+
} else {
132+
allocator_active_model.client_contract_address = Set(None);
133+
}
134+
} else {
135+
allocator_active_model.client_contract_address = Set(None);
136+
}
137+
127138
let updated_model = allocator_active_model.update(&conn).await?;
128139

129140
Ok(updated_model)
@@ -180,6 +191,13 @@ pub async fn create_or_update_allocator(
180191
new_allocator.registry_file_path = Set(registry_file_path);
181192
}
182193

194+
if let Some(client_contract_address) = client_contract_address {
195+
if !client_contract_address.is_empty() {
196+
new_allocator.client_contract_address = Set(Some(client_contract_address));
197+
} else {
198+
new_allocator.client_contract_address = Set(None);
199+
}
200+
}
183201
let conn = get_database_connection()
184202
.await
185203
.expect("Failed to get DB connection");

fplus-database/src/database/autoallocations.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use crate::get_database_connection;
2-
use crate::models::autoallocations::AddressWrapper;
32
use crate::models::autoallocations::{
43
Column, Entity as Autoallocations, Model as AutoallocationModel,
54
};
5+
use crate::types::AddressWrapper;
66
use alloy::primitives::Address;
77
use chrono::{DateTime, FixedOffset};
88
use sea_orm::{entity::*, query::*, DbBackend, DbErr};

fplus-database/src/lib.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ mod tests {
124124
let required_sps = Some("5+".to_string());
125125
let required_replicas = Some("5+".to_string());
126126
let registry_file_path = Some("Allocators/123.json".to_string());
127+
let client_contract_address = Some("f1owcbryeqlq3vl7kydzax7r75sbtyvgpnny7fswy".to_string());
127128

128129
let result = database::allocators::create_or_update_allocator(
129130
owner,
@@ -139,6 +140,7 @@ mod tests {
139140
required_sps,
140141
required_replicas,
141142
registry_file_path,
143+
client_contract_address,
142144
)
143145
.await;
144146
assert!(result.is_ok());
@@ -216,7 +218,7 @@ mod tests {
216218
let required_sps = Some("5+".to_string());
217219
let required_replicas = Some("5+".to_string());
218220
let registry_file_path = Some("Allocators/123.json".to_string());
219-
221+
let client_contract_address = Some("f1owcbryeqlq3vl7kydzax7r75sbtyvgpnny7fswy".to_string());
220222
let result = database::allocators::create_or_update_allocator(
221223
owner.clone(),
222224
repo.clone(),
@@ -231,6 +233,7 @@ mod tests {
231233
required_sps,
232234
required_replicas,
233235
registry_file_path,
236+
client_contract_address,
234237
)
235238
.await;
236239

fplus-database/src/models/allocators.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub struct Model {
2222
pub required_sps: Option<String>,
2323
pub required_replicas: Option<String>,
2424
pub registry_file_path: Option<String>,
25+
pub client_contract_address: Option<String>,
2526
}
2627

2728
#[derive(Copy, Clone, Debug, EnumIter)]
+2-25
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
use alloy::primitives::{Address, AddressError};
21
use chrono::{DateTime, FixedOffset};
32
use sea_orm::entity::prelude::*;
4-
use sea_orm_newtype::DeriveNewType;
53
use serde::{Deserialize, Serialize};
64

5+
use crate::types::AddressWrapper;
6+
77
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq, Serialize, Deserialize)]
88
#[sea_orm(table_name = "autoallocations")]
99
pub struct Model {
@@ -16,26 +16,3 @@ pub struct Model {
1616
pub enum Relation {}
1717

1818
impl ActiveModelBehavior for ActiveModel {}
19-
20-
#[derive(Clone, Debug, PartialEq, DeriveNewType, Eq, Serialize, Deserialize)]
21-
#[sea_orm_newtype(try_from_into = "String", primary_key)]
22-
pub struct AddressWrapper(pub Address);
23-
24-
impl TryFrom<String> for AddressWrapper {
25-
type Error = AddressError;
26-
fn try_from(value: String) -> Result<Self, Self::Error> {
27-
Ok(AddressWrapper(Address::parse_checksummed(value, None)?))
28-
}
29-
}
30-
31-
impl From<AddressWrapper> for String {
32-
fn from(value: AddressWrapper) -> Self {
33-
value.0.to_checksum(None)
34-
}
35-
}
36-
37-
impl From<Address> for AddressWrapper {
38-
fn from(value: Address) -> Self {
39-
AddressWrapper(value)
40-
}
41-
}

fplus-database/src/types.rs

+26-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
use serde::Deserialize;
1+
use alloy::primitives::{Address, AddressError};
2+
use sea_orm_newtype::DeriveNewType;
3+
use serde::{Deserialize, Serialize};
24

35
#[derive(Deserialize)]
46
pub(super) struct DbConnectParams {
@@ -24,3 +26,26 @@ impl DbConnectParams {
2426
)
2527
}
2628
}
29+
30+
#[derive(Clone, Debug, PartialEq, DeriveNewType, Eq, Serialize, Deserialize)]
31+
#[sea_orm_newtype(try_from_into = "String", primary_key)]
32+
pub struct AddressWrapper(pub Address);
33+
34+
impl TryFrom<String> for AddressWrapper {
35+
type Error = AddressError;
36+
fn try_from(value: String) -> Result<Self, Self::Error> {
37+
Ok(AddressWrapper(Address::parse_checksummed(value, None)?))
38+
}
39+
}
40+
41+
impl From<AddressWrapper> for String {
42+
fn from(value: AddressWrapper) -> Self {
43+
value.0.to_checksum(None)
44+
}
45+
}
46+
47+
impl From<Address> for AddressWrapper {
48+
fn from(value: Address) -> Self {
49+
AddressWrapper(value)
50+
}
51+
}

fplus-lib/src/core/allocator/file.rs

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ pub struct AllocatorModel {
88
pub owner: Option<String>,
99
pub repo: Option<String>,
1010
pub address: Option<String>,
11+
pub client_contract_address: Option<String>,
1112
}
1213

1314
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -26,6 +27,7 @@ pub struct Application {
2627
pub data_types: Vec<String>,
2728
pub required_sps: String,
2829
pub required_replicas: String,
30+
pub client_contract_address: Option<String>,
2931
}
3032

3133
#[derive(Serialize, Deserialize, Debug, Clone)]

fplus-lib/src/core/allocator/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,7 @@ pub async fn update_installation_ids_in_db(installation: InstallationRepositorie
384384
None,
385385
None,
386386
None,
387+
None,
387388
)
388389
.await;
389390
}
@@ -648,6 +649,7 @@ pub async fn create_allocator_from_file(files_changed: Vec<String>) -> Result<()
648649
Some(model.application.required_sps),
649650
Some(model.application.required_replicas),
650651
Some(file_name.to_owned()),
652+
model.application.client_contract_address,
651653
)
652654
.await
653655
.map_err(|e| LDNError::New(format!("Create or update allocator failed: {}", e)))?;

manual-migrations/2024-10-16.sql

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
ALTER TABLE IF EXISTS public.allocators
2+
ADD COLUMN client_contract_address text;

0 commit comments

Comments
 (0)