Skip to content

Commit

Permalink
feat: add active at query param to bonds and unbonds
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Dec 12, 2024
1 parent 2b8fcf5 commit 5cd431d
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 8 deletions.
12 changes: 12 additions & 0 deletions swagger.yml
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ paths:
type: integer
minimum: 1
description: Pagination parameter
- in: query
name: activeAt
schema:
type: integer
minimum: 0
description: Get all bonds that are active at this epoch
responses:
'200':
description: A list of bonds.
Expand Down Expand Up @@ -175,6 +181,12 @@ paths:
type: integer
minimum: 1
description: Pagination parameter
- in: query
name: activeAt
schema:
type: integer
minimum: 0
description: Get all unbonds that are active at this epoch( < )
responses:
'200':
description: A list of unbonds.
Expand Down
6 changes: 6 additions & 0 deletions webserver/src/dto/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,21 @@ pub enum MyValidatorKindDto {
}

#[derive(Clone, Serialize, Deserialize, Validate)]
#[serde(rename_all = "camelCase")]
pub struct BondsDto {
#[validate(range(min = 1, max = 10000))]
pub page: Option<u64>,
#[validate(range(min = 0))]
pub active_at: Option<i32>,
}

#[derive(Clone, Serialize, Deserialize, Validate)]
#[serde(rename_all = "camelCase")]
pub struct UnbondsDto {
#[validate(range(min = 1, max = 10000))]
pub page: Option<u64>,
#[validate(range(min = 0))]
pub active_at: Option<i32>,
}

#[derive(Clone, Serialize, Deserialize, Validate)]
Expand Down
4 changes: 2 additions & 2 deletions webserver/src/handler/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ pub async fn get_bonds(

let (bonds, total_pages, total_bonds) = state
.pos_service
.get_bonds_by_address(address, page)
.get_bonds_by_address(address, page, query.active_at)
.await?;

let response =
Expand Down Expand Up @@ -97,7 +97,7 @@ pub async fn get_unbonds(

let (unbonds, total_pages, total_unbonds) = state
.pos_service
.get_unbonds_by_address(address, page)
.get_unbonds_by_address(address, page, query.active_at)
.await?;

let response =
Expand Down
24 changes: 20 additions & 4 deletions webserver/src/repository/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,14 @@ pub trait PosRepositoryTrait {
&self,
address: String,
page: i64,
active_at: Option<i32>,
) -> Result<PaginatedResponseDb<(ValidatorDb, BondDb)>, String>;

async fn find_unbonds_by_address(
&self,
address: String,
page: i64,
active_at: Option<i32>,
) -> Result<PaginatedResponseDb<(ValidatorDb, UnbondDb)>, String>;

async fn find_merged_unbonds_by_address(
Expand Down Expand Up @@ -183,12 +185,19 @@ impl PosRepositoryTrait for PosRepository {
&self,
address: String,
page: i64,
active_at: Option<i32>,
) -> Result<PaginatedResponseDb<(ValidatorDb, BondDb)>, String> {
let conn = self.app_state.get_db_connection().await;

conn.interact(move |conn| {
validators::table
.inner_join(bonds::table)
let mut query =
validators::table.inner_join(bonds::table).into_boxed();

if let Some(at) = active_at {
query = query.filter(bonds::dsl::start.le(at));
}

query
.filter(bonds::dsl::address.eq(address))
.select((validators::all_columns, bonds::all_columns))
.paginate(page)
Expand Down Expand Up @@ -235,12 +244,19 @@ impl PosRepositoryTrait for PosRepository {
&self,
address: String,
page: i64,
active_at: Option<i32>,
) -> Result<PaginatedResponseDb<(ValidatorDb, UnbondDb)>, String> {
let conn = self.app_state.get_db_connection().await;

conn.interact(move |conn| {
validators::table
.inner_join(unbonds::table)
let mut query =
validators::table.inner_join(unbonds::table).into_boxed();

if let Some(at) = active_at {
query = query.filter(unbonds::dsl::withdraw_epoch.lt(at));
}

query
.filter(unbonds::dsl::address.eq(address))
.select((validators::all_columns, unbonds::all_columns))
.paginate(page)
Expand Down
6 changes: 4 additions & 2 deletions webserver/src/service/pos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ impl PosService {
&self,
address: String,
page: u64,
active_at: Option<i32>,
) -> Result<(Vec<Bond>, u64, u64), PoSError> {
let pos_state = self
.pos_repo
Expand All @@ -111,7 +112,7 @@ impl PosService {

let (db_bonds, total_pages, total_items) = self
.pos_repo
.find_bonds_by_address(address, page as i64)
.find_bonds_by_address(address, page as i64, active_at)
.await
.map_err(PoSError::Database)?;

Expand Down Expand Up @@ -154,10 +155,11 @@ impl PosService {
&self,
address: String,
page: u64,
active_at: Option<i32>,
) -> Result<(Vec<Unbond>, u64, u64), PoSError> {
let (db_unbonds, total_pages, total_items) = self
.pos_repo
.find_unbonds_by_address(address, page as i64)
.find_unbonds_by_address(address, page as i64, active_at)
.await
.map_err(PoSError::Database)?;

Expand Down

0 comments on commit 5cd431d

Please sign in to comment.