Skip to content

Commit 7320744

Browse files
committed
fix: add in 68d63cf
1 parent bae0188 commit 7320744

File tree

4 files changed

+24
-9
lines changed

4 files changed

+24
-9
lines changed

webserver/src/dto/pos.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,3 +82,9 @@ pub struct WithdrawsDto {
8282
#[validate(range(min = 1, max = 10000))]
8383
pub epoch: Option<u64>,
8484
}
85+
86+
#[derive(Clone, Serialize, Deserialize, Validate)]
87+
pub struct RewardsDto {
88+
#[validate(range(min = 1, max = 10000))]
89+
pub epoch: Option<u64>,
90+
}

webserver/src/handler/pos.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ use axum_extra::extract::Query;
55
use axum_macros::debug_handler;
66

77
use crate::dto::pos::{
8-
AllValidatorsQueryParams, BondsDto, UnbondsDto, ValidatorQueryParams,
9-
ValidatorStateDto, WithdrawsDto,
8+
AllValidatorsQueryParams, BondsDto, RewardsDto, UnbondsDto,
9+
ValidatorQueryParams, ValidatorStateDto, WithdrawsDto,
1010
};
1111
use crate::error::api::ApiError;
1212
use crate::response::pos::{
@@ -149,10 +149,14 @@ pub async fn get_withdraws(
149149
#[debug_handler]
150150
pub async fn get_rewards(
151151
_headers: HeaderMap,
152+
query: Query<RewardsDto>,
152153
Path(address): Path<String>,
153154
State(state): State<CommonState>,
154155
) -> Result<Json<Vec<Reward>>, ApiError> {
155-
let rewards = state.pos_service.get_rewards_by_address(address).await?;
156+
let rewards = state
157+
.pos_service
158+
.get_rewards_by_address(address, query.epoch)
159+
.await?;
156160
Ok(Json(rewards))
157161
}
158162

webserver/src/repository/pos.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ pub trait PosRepositoryTrait {
9393
async fn find_rewards_by_address(
9494
&self,
9595
address: String,
96+
epoch: Option<u64>,
9697
) -> Result<Vec<PoSRewardDb>, String>;
9798

9899
async fn find_rewards_by_delegator_and_validator_and_epoch(
@@ -338,15 +339,18 @@ impl PosRepositoryTrait for PosRepository {
338339
async fn find_rewards_by_address(
339340
&self,
340341
address: String,
342+
epoch: Option<u64>,
341343
) -> Result<Vec<PoSRewardDb>, String> {
342344
let conn = self.app_state.get_db_connection().await;
343345

344346
conn.interact(move |conn| {
345-
let epoch = pos_rewards::table
346-
.select(diesel::dsl::max(pos_rewards::epoch))
347-
.first::<Option<_>>(conn)
348-
.unwrap_or(Some(0))
349-
.unwrap_or(0);
347+
let epoch = epoch.map(|e| e as i32).unwrap_or_else(|| {
348+
pos_rewards::table
349+
.select(diesel::dsl::max(pos_rewards::epoch))
350+
.first::<Option<_>>(conn)
351+
.unwrap_or(Some(0))
352+
.unwrap_or(0)
353+
});
350354

351355
pos_rewards::table
352356
.filter(pos_rewards::epoch.eq(&epoch))

webserver/src/service/pos.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,12 +273,13 @@ impl PosService {
273273
pub async fn get_rewards_by_address(
274274
&self,
275275
address: String,
276+
epoch: Option<u64>,
276277
) -> Result<Vec<Reward>, PoSError> {
277278
// TODO: could optimize and make a single query
278279

279280
let db_rewards = self
280281
.pos_repo
281-
.find_rewards_by_address(address)
282+
.find_rewards_by_address(address, epoch)
282283
.await
283284
.map_err(PoSError::Database)?;
284285

0 commit comments

Comments
 (0)