Skip to content

Commit

Permalink
Merge pull request #445 from Concordium/account-baker-query
Browse files Browse the repository at this point in the history
Add account baker query
  • Loading branch information
DOBEN authored Jan 30, 2025
2 parents dc1d2ef + 6e46853 commit 5c29c70
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
14 changes: 8 additions & 6 deletions backend-rust/src/graphql_api/account.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{
get_config, get_pool, token::AccountToken, AccountStatementEntryType, ApiError, ApiResult,
ConnectionQuery,
baker::Baker, get_config, get_pool, token::AccountToken, AccountStatementEntryType, ApiError,
ApiResult, ConnectionQuery,
};
use crate::{
address::AccountAddress,
Expand Down Expand Up @@ -291,7 +291,7 @@ impl AccountReleaseScheduleItem {
}

pub struct Account {
// release_schedule: AccountReleaseSchedule,
/// Index of the account.
pub index: i64,
/// Index of the transaction creating this account.
/// Only `None` for genesis accounts.
Expand All @@ -307,9 +307,7 @@ pub struct Account {
pub num_txs: i64,

pub delegated_restake_earnings: Option<bool>,
pub delegated_target_baker_id: Option<i64>, /* Get baker information if this account is
* baking.
* baker: Option<Baker>, */
pub delegated_target_baker_id: Option<i64>,
}
impl Account {
pub async fn query_by_index(pool: &PgPool, index: AccountIndex) -> ApiResult<Option<Self>> {
Expand Down Expand Up @@ -351,6 +349,10 @@ impl Account {
/// The total amount of CCD hold by the account.
pub async fn amount(&self) -> ApiResult<Amount> { Ok(self.amount.try_into()?) }

pub async fn baker(&self, ctx: &Context<'_>) -> ApiResult<Option<Baker>> {
Ok(Baker::query_by_id(get_pool(ctx)?, self.index).await?)
}

async fn delegation(&self) -> ApiResult<Option<Delegation>> {
let staked_amount = self.delegated_stake.try_into()?;
Ok(self.delegated_restake_earnings.map(|restake_earnings| Delegation {
Expand Down
15 changes: 7 additions & 8 deletions backend-rust/src/graphql_api/baker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ pub struct QueryBaker;
#[Object]
impl QueryBaker {
async fn baker<'a>(&self, ctx: &Context<'a>, id: types::ID) -> ApiResult<Baker> {
let id = IdBaker::try_from(id)?.baker_id;
Baker::query_by_id(get_pool(ctx)?, id).await
let id = IdBaker::try_from(id)?.baker_id.into();
Baker::query_by_id(get_pool(ctx)?, id).await?.ok_or(ApiError::NotFound)
}

async fn baker_by_baker_id<'a>(
&self,
ctx: &Context<'a>,
baker_id: BakerId,
) -> ApiResult<Baker> {
Baker::query_by_id(get_pool(ctx)?, baker_id).await
Baker::query_by_id(get_pool(ctx)?, baker_id.into()).await?.ok_or(ApiError::NotFound)
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -82,8 +82,8 @@ pub struct Baker {
finalization_commission: Option<i64>,
}
impl Baker {
async fn query_by_id(pool: &PgPool, baker_id: BakerId) -> ApiResult<Self> {
sqlx::query_as!(
pub async fn query_by_id(pool: &PgPool, baker_id: i64) -> ApiResult<Option<Self>> {
Ok(sqlx::query_as!(
Baker,
r#"
SELECT
Expand All @@ -98,11 +98,10 @@ impl Baker {
FROM bakers
WHERE id = $1
"#,
i64::from(baker_id)
baker_id
)
.fetch_optional(pool)
.await?
.ok_or(ApiError::NotFound)
.await?)
}
}
#[Object]
Expand Down

0 comments on commit 5c29c70

Please sign in to comment.